Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[resolved] Trouble setting a value in an array

I have an array which is made up of players. It is defined like so:

$players['' + username + ''] = { 'username': username, 'x': x, 'y': y, 'dir': dir, 'skin': skin, 'hair': hair, 'colour': colour, 'clothes': clothes, 'event_id': event_id };


console.log gives me this:

dIZPBBb.png


Which makes sense and is what I want.

The function is ran again and hits this line:

$players['' + username + ''] = { 'username': username, 'x': x, 'y': y, 'dir': dir, 'skin': skin, 'hair': hair, 'colour': colour, 'clothes': clothes, 'event_id': event_id };


But to check if event_id itself needs initializing, I do the following:

if ($players['' + username + ''] === undefined)
{
event_id = 1;
}
else
{
event_id = $players['' + username + '']['event_id'];
}


This is equivalent to:

value = value || 0;


Which I have been advised never to use because falsey and undefined are different things.

Now with console.log I still get this:

dIZPBBb.png


event_id is still 1. BUT, when I expand this line in the console, I get this:

oxpVyrG.png


event_id there is clearly 5. When I unexpand I get the line above where event_id is 1!

In a later function I set my event id like so:

$players['' + username + '']['event_id'] = Number($gameMap._lastSpawnEventId);

When I console.log $players['' + username + '']['event_id'] I get 5.

Now when I run the original function, the same log returns 1.

Can anyone make sense of this (or am I in fact creating a mess)?
 
Basically, to try and explain myself:

A list of players is obtained which looks like this:

username, 1, 2, 3, 4, 5, 6
username, 1, 2, 3, 4, 5, 6
username, 1, 2, 3, 4, 5, 6

Where the numbers are variables.

These values are all used to build an array,

[[u,1,2,3,4,5,6],[u,1,2,3,4,5,6],[u,1,2,3,4,5,6]]

Each player in there gets assigned an event_id:

[[u,1,2,3,4,5,6,a],[u,1,2,3,4,5,6,b],[u,1,2,3,4,5,6,c]]

The players list is obtained again:

username, 1, 2, 3, 4, 5, 6
username, 1, 2, 3, 4, 5, 6
username, 1, 2, 3, 4, 5, 6
username, 1, 2, 3, 4, 5, 6

If the player is new to the array, they get added to it:

[[u,1,2,3,4,5,6,a],[u,1,2,3,4,5,6,b],[u,1,2,3,4,5,6,c],[u,1,2,3,4,5,6,d]]

But if the player is already in the array, their values all get updated:

[1,2,3,4,5,6]

Apart from their event_id, which stays what it was originally assigned as.

(And then if the player has left the list they are removed from the array).
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top