Weird behavior with classes

Started by D4rkR420R, Jan 20, 2019, 04:12 PM

Previous topic - Next topic

D4rkR420R

Hello everyone, this is KuRuMi^, aka DarkRaZoR^. How're y'all doing today? I'm doing great if you may ask. Okay, so I was working in my server and I seem to notice that my player's class was being modified for every player, therefore I took into a look in the code and I runned an experiment to determine the cause of it. The cause was Squirrel tables stored into my player's class. Here's how the experiment worked out.
class Info
{
ID = 0;
Details = {
Name = "",
Age = 0,
Job = "Citizen"
};
}

infos <- array( GetMaxPlayers(), null );
infos[ player.ID ] = Info();
infos[ player.ID ].ID = player.ID;
infos[ player.ID ].Details.Name = player.Name;
if( player.Name != "TdZ.DarkRaZoR^" ) infos[ player.ID ].Details.Age = 15;
else infos[ player.ID ].Details.Age = 20;

So, I join and my data is set to the Squirrel table in the class, however, when another player joins with another nickname, their data is set to every player who has the same class. Could someone briefly explains this and a solution similar to what I want to do? Thank you for taking your time to read.

NicusorN5

#1
Use multiple arrays then for each member of the class then :D

D4rkR420R

Quote from: Athanatos on Jan 20, 2019, 04:20 PMUse multiple arrays then for each member of the class then :D

That looks ugly. :c C'mon Athanatos. I'm a person who wants such neat code and compact as possible.

NicusorN5

Try using SLC's plugin. Anyway, I also had a similar problem with string arrays in my mute system.

!

First of all the justification is not enough or either I am not able to understand the problem. Justify a bit more cause by looking at the example snippet there is nothing wrong and what do you mean by the assignment of same class to newly joined player PS make sure you destroyed the created class upon player leaving maybe this is what is the cause... happens sometimes if not destroyed properly.

Discord: zeus#5155

DizzasTeR

You need to use a constructor in your class and specifically set the class properties to the passed in variables.
class Info {
ID = null;
Details = null;

constructor(...) {
local args = vargv;
if(args.len() < 1)
throw("Invalid arguments passed!");

this.ID = args[0];

this.Details = {
Name = args[1],
Age = args[2],
Job = args[3]
};
}
}

function onScriptLoad() {
local playerA = Info(0, "Player1", 18, "Citizen");
local playerB = Info(1, "Player2", 20, "Cop");
print(format("ID: %i, Name: %s, Age: %i, Job: %s", playerA.ID, playerA.Details.Name, playerA.Details.Age, playerA.Details.Job));
print(format("ID: %i, Name: %s, Age: %i, Job: %s", playerB.ID, playerB.Details.Name, playerB.Details.Age, playerB.Details.Job));
}

Output:
[SCRIPT]  ID: 0, Name: Player1, Age: 18, Job: Citizen
[SCRIPT]  ID: 1, Name: Player2, Age: 20, Job: Cop

Mötley

As zeus was saying, there's nothing wrong with your code, Just make sure your doing what he said in his post.
infos <- array( GetMaxPlayers(), null );
 
class Info {
  ID = 0;
  Details = {
    Name = "Player",
    Age = 0,
    Job = "Citizen"
  };
}



// From a fast test:
if("Name" in Info().Details) {
  print("yep");
  print("Player Name: " + Info().Details.Name )
}

if("Age" in Info().Details) {
  print("yep");
  print("Age: " + Info().Details.Age )
}

if("Job" in Info().Details) {
  print("yep");
  print("Job Name: " + Info().Details.Job )
}

//**********************************************
print("Starting test 2! \n\n")
// second test
infos[0] = Info();
infos[ 0 ].Details.Name = "Motley";
infos[ 0 ].Details.Age = 28;


if(infos[0].Details.Name == "Motley") {
  print("yep");
  print("Player Name: " + infos[0].Details.Name )
}

if(infos[0].Details.Age == 28) {
  print("yep");
  print("Age: " + infos[0].Details.Age )
}

infos[1] = Info();
infos[ 1 ].Details.Name = "Doom";
infos[ 1 ].Details.Age = 20;
print("Player Name: " + infos[1].Details.Name )

print("Age: " + infos[1].Details.Age )


D4rkR420R

Quote from: Doom_Kill3R on Jan 22, 2019, 12:55 PMYou need to use a constructor in your class and specifically set the class properties to the passed in variables.
class Info {
ID = null;
Details = null;

constructor(...) {
local args = vargv;
if(args.len() < 1)
throw("Invalid arguments passed!");

this.ID = args[0];

this.Details = {
Name = args[1],
Age = args[2],
Job = args[3]
};
}
}

function onScriptLoad() {
local playerA = Info(0, "Player1", 18, "Citizen");
local playerB = Info(1, "Player2", 20, "Cop");
print(format("ID: %i, Name: %s, Age: %i, Job: %s", playerA.ID, playerA.Details.Name, playerA.Details.Age, playerA.Details.Job));
print(format("ID: %i, Name: %s, Age: %i, Job: %s", playerB.ID, playerB.Details.Name, playerB.Details.Age, playerB.Details.Job));
}

Output:
[SCRIPT]  ID: 0, Name: Player1, Age: 18, Job: Citizen
[SCRIPT]  ID: 1, Name: Player2, Age: 20, Job: Cop

DoomKill3r is the only one who understands my situation. Golly. Thanks BTW. :)