I got this repetitive block which goes through some arrays and picks the current player I'd like to have some sort of a function to allow me to access the Player object and edit certain attributes.
So I want this:
for (let i = 0; i < Room_Array.length; i++) {
if (Room_Array[i][0] === PlayerObject[socket.id].RoomName) {
for (let j = 0; j < Room_Array[i][2].length; j++) {
if (Room_Array[i][2][j].SocketID === PlayerObject[socket.id].SocketID) {
Room_Array[i][2][j].IsSuspended = PlayerObject[socket.id].IsSuspended;
break;
}
}
break;
}
}
To be usable like this:
PlayerPicked.IsSuspended = 0; // value is not important
Without having to write the block multiple times.
I found my own answer. This is the function:
function Player_Return_BySocketID (P_Room_Array, P_RoomName, P_SocketID)
{
for (let i = 0; i < P_Room_Array.length; i++)
{
if (P_Room_Array[i][0] === P_RoomName)
{
for (let j = 0; j < P_Room_Array[i][2].length; j++)
{
if (P_Room_Array[i][2][j].SocketID === P_SocketID)
{
return P_Room_Array[i][2][j];
}
}
}
}
}
And it is used like so:
PlayerPicked = Player_Return_BySocketID(Room_Array, PlayerObject[socket.id].RoomName, PlayerObject[socket.id].SocketID);
PlayerPicked.SuspendedStatus = PlayerObject[socket.id].SuspendedStatus;
The PlayerPicked therefore has access to all the attributes P_Room_Array[i][2][j]
has.