edqx / amongus-protocol

An implementation of the Among Us protocol in typescript.
MIT License
55 stars 5 forks source link

TypeError: Cannot read property 'PlayerControl' of undefined #24

Open Rohit3523 opened 3 years ago

Rohit3523 commented 3 years ago

When i host a game via your module and i try to join the lobby with my phone i get this error

Complete Error: (node:3380) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'PlayerControl' of undefined at Game. (file:///C:/Users/Rohit/Desktop/Other/AmongUS2/node_modules/amongus-protocol/js/lib/struct/Game.js:168:65) at Generator.next () at file:///C:/Users/Rohit/Desktop/Other/AmongUS2/node_modules/amongus-protocol/js/lib/struct/Game.js:7:71 at new Promise () at __awaiter (file:///C:/Users/Rohit/Desktop/Other/AmongUS2/node_modules/amongus-protocol/js/lib/struct/Game.js:3:12) at Game.setStartCounter (file:///C:/Users/Rohit/Desktop/Other/AmongUS2/node_modules/amongus-protocol/js/lib/struct/Game.js:156:16) at Game. (file:///C:/Users/Rohit/Desktop/Other/AmongUS2/node_modules/amongus-protocol/js/lib/struct/Game.js:189:22) at Generator.next () at file:///C:/Users/Rohit/Desktop/Other/AmongUS2/node_modules/amongus-protocol/js/lib/struct/Game.js:7:71

2van commented 3 years ago

cus u didn't spawn host, and the host Player is null. if u spawn host Player, u have to recieve Message with SpawnID.GameData.

Rohit3523 commented 3 years ago

Can you give an example

2van commented 3 years ago

Unfortunately, it's impossible to create a host Player Object as far as I know.

5GameMaker commented 3 years ago

Same error. Code:

const cp = require('child_process');

async function sleep(ms) {
    return new Promise(res => {
        setTimeout(() => res(), ms);
    });
}

async function main() {
    console.log("Loading libraries...");

    const ap = await import('amongus-protocol');

    console.log("Initializing client...");

    const client = new ap.AmongusClient({
        debug: false,
    });

    /**
     * @param {[string, number][]} master
     */
    async function connectTo(master) {
        for (let i = 0; i < master.length; i++) {
            try {
                console.log(`Connecting to ${master[i][0]}:${master[i][1]}`);
                const promise1 = client.connect(master[i][0], master[i][1], "godmode");

                let connected = false;

                promise1.then(() => connected = true);

                await sleep(1000);

                if (connected) return;
                else {
                    console.log("Server down");
                }
            } catch (e) {}
        }
        console.log(`Failed to connect`);
        process.exit(1);
    }

    await connectTo(ap.MasterServers.AS);
    console.log(`Connected to ${client.ip}:${client.port}`);
    console.log(`Starting game...`);

    const code = (await client.host({
        language: ap.LanguageID.English,
    })).code

    console.log(`Server is running on ${cp.execSync(`./intToCode ${code}`).toString('utf8')}`);
    console.log(`Joining server...`);

    const game = await client.join(code, {
        doSpawn: true,
    });

    // game.me.on('spawn', () => {
    //     game.me.setColor(ap.ColourID.Black);
    //     game.me.setName("godmode");
    // });
}
main().catch(console.error);