I'm new here and I really wanted to ask you whether it's possible to remove a specific object in an event. Actually, the game I'm programming has 2 main events: .onPlayerJoin and .onPlayerLeave. I set this so when a player joins, it creates an object with name, id and auth in an array of objects already declared.
When a player leaves I want the object to be deleted! I'm struggling with this because the main problem is to delete this object without knowing the position. Also, objects don't have names (except for the declaration of a new object inside .onPlayerJoin).
I'll leave you some code here:
var playerz = [];
room.onPlayerJoin = function (player) {
var newPlayer = {
name: player.name,
id: player.id,
auth: player.auth,
};
playerz.push(newPlayer);
};
room.onPlayerLeave = function (player) {
playerz.splice(playerz.indexOf(player.name));
};
This doesn't work because it just removes an object but not the one I want. If there are 5 players in a room and one leaves, I want the object of that person to be removed...
Oh, I remember you that player.name, player.id and player.auth are properties of the object player, I didn't have to declare because they're already built in the game. Also, player.name and the others are very variable, meaning that if you put
console.log(player.name);
both in .onPlayerJoin and .onPlayerLeave it will console the name of the players who join and the ones who leave, but if you set a .onplayerChat in which you put message === "hello", player.name will be the player who wrote hello.
I explained it the best way I could! Have a good day!