Vice City: Multiplayer

VC:MP Discussion => Support => Topic started by: DarkRaZoR^ on January 20th, 2019, 04:12 PM

Title: Weird behavior with classes
Post by: DarkRaZoR^ on January 20th, 2019, 04:12 PM
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.
Title: Re: Weird behavior with classes
Post by: Athanatos on January 20th, 2019, 04:20 PM
Use multiple arrays then for each member of the class then :D
Title: Re: Weird behavior with classes
Post by: DarkRaZoR^ on January 20th, 2019, 06:45 PM
Quote from Athanatos on January 20th, 2019, 04:20 PM
Use 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.
Title: Re: Weird behavior with classes
Post by: Athanatos on January 21st, 2019, 02:24 PM
Try using SLC's plugin. Anyway, I also had a similar problem with string arrays in my mute system.
Title: Re: Weird behavior with classes
Post by: ! on January 21st, 2019, 03:07 PM
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.
Title: Re: Weird behavior with classes
Post by: DizzasTeR 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
Title: Re: Weird behavior with classes
Post by: Bob on January 23rd, 2019, 05:07 AM
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 )

Title: Re: Weird behavior with classes
Post by: DarkRaZoR^ on January 24th, 2019, 02:59 AM
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. :)