1 Replies - 63 Views - Last Post: 12 July 2019 - 08:35 PM

#1 wikisi9   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-July 19

Code worked until I changed an empty variable into an empty object

Posted 12 July 2019 - 07:36 PM

Hi folks.
I'm a newbie. Yeah my title sucks, I'm not sure how to describe my problem in just a few words.

So my code worked fine until I changed one little thing. Let me show my code and I'll explain...

var waitingPlayer = null; // initially when server starts there's no one connected so it's null

io.on('connection', (sock) => {

    if (waitingPlayer) { // if there's a player waiting, start a game
    new game(waitingPlayer, sock);
    waitingPlayer = null; // we matched two players so now waitingplayer is null again
    } else { // if there's nobody waiting, then sock becomes a waitingplayer
        waitingPlayer = sock;
        waitingPlayer.emit('message', 'Waiting for an opponent'); // let the player know he's waiting for an opponent.
    }
    
    sock.on('message', (text) => {
        io.emit('message', text); // io.emit sends to every connected user whereas sock.emit sends to one user
    });
});


The above code is in server.js and works for 2 players. But I want to increase the number of players to 8. So I changed the line

var waitingPlayer = null;

to
var waitingPlayer = {};

to create en empty object.

But simply changing such a little thing threw the following error:

TypeError: player.emit is not a function


It didn't do that before. Also, player.emit is in a seperate file called game.js

Any idea what is happening here?

Is This A Good Question/Topic? 0
  • +

Replies To: Code worked until I changed an empty variable into an empty object

#2 wikisi9   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-July 19

Re: Code worked until I changed an empty variable into an empty object

Posted 12 July 2019 - 08:34 PM

Never mind. The following doesn't throw an error

var waitingPlayers = Object.create(null);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1