This topic was marked solved by its starter, on January 24th, 2019, 02:59 AM
Weird behavior with classes

DarkRaZoR^

  • Full Member
  • VC:MP Battlefield Head Developer/KF Leader
  • Posts: 228
Weird behavior with classes
«  »
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.
Code: [Select]
class Info
{
ID = 0;
Details = {
Name = "",
Age = 0,
Job = "Citizen"
};
}

Code: [Select]
infos <- array( GetMaxPlayers(), null );

Code: [Select]
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.

Athanatos

  • Hero Member
  • sin^2(x)+cos^2(x) = 1
  • Posts: 930
Re: Weird behavior with classes
« Reply #1,  »Last edited
Use multiple arrays then for each member of the class then :D
Infected City Survival Main Dev

DarkRaZoR^

  • Full Member
  • VC:MP Battlefield Head Developer/KF Leader
  • Posts: 228

Athanatos

  • Hero Member
  • sin^2(x)+cos^2(x) = 1
  • Posts: 930
Re: Weird behavior with classes
« Reply #3,  »
Try using SLC's plugin. Anyway, I also had a similar problem with string arrays in my mute system.
Infected City Survival Main Dev

!

  • Sr. Member
  • Don't feel shy while zooming on h*r... >>zeus#5155
  • Posts: 380
Re: Weird behavior with classes
« Reply #4,  »
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

  • VC:MP Beta Tester
  • Infinite loops defy infinity by overflowing memory
  • Posts: 1,156
Re: Weird behavior with classes
« Reply #5,  »
You need to use a constructor in your class and specifically set the class properties to the passed in variables.
Code: [Select]
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:
Code: [Select]
[SCRIPT]  ID: 0, Name: Player1, Age: 18, Job: Citizen
[SCRIPT]  ID: 1, Name: Player2, Age: 20, Job: Cop

Bob

  • Hero Member
  • Posts: 777
Re: Weird behavior with classes
« Reply #6,  »
As zeus was saying, there's nothing wrong with your code, Just make sure your doing what he said in his post.
Code: [Select]
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 )


DarkRaZoR^

  • Full Member
  • VC:MP Battlefield Head Developer/KF Leader
  • Posts: 228
Re: Weird behavior with classes
« Reply #7,  »
Quote from Doom_Kill3R on January 22nd, 2019, 12:55 PM
You need to use a constructor in your class and specifically set the class properties to the passed in variables.
Code: [Select]
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:
Code: [Select]
[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. :)