PDA

View Full Version : The problem with arrays...



Moczulak
3rd February 2013, 11:34
Hey all :)

I wrote a script that handles the array. The script only works on the last table, in my case I have 3 tables, and only the last one works as it should :(

Tables:



level.weapons = [];
level.weapons[0] = spawnStruct();
level.weapons[0].weapon = "thompson_mp";
level.weapons[0].numberimage = "1";

level.weapons[1] = spawnStruct();
level.weapons[1].weapon = "kar98k_mp";
level.weapons[1].numberimage = "2";

level.weapons[2] = spawnStruct();
level.weapons[2].weapon = "m1carbine_mp";
level.weapons[2].numberimage = "3";


Script:


if(response == "buttons")
{
for(i = 0; i < level.weapons.size; i++)
{
guid = self getGuid();
self thread files\_propertiesguid::load(guid); // Load self.oldweapon

if(self.oldweapon == level.weapons[i].weapon)
{
self setClientCvar(weaponButton, "3");
self setClientCvar(weaponImage, level.weapons[i].numberimage);
}
else
{
self setClientCvar(weaponButton, "2");
self setClientCvar(weaponImage, "0");
}
}
}


Last table, which is the third "m1carbine_mp" is working correctly, but the first two spoil the script ...
The first two arrays bad work on the script, if they read the script uses the "if" and "else" at the same time :(
Please Help me..

kung foo man
3rd February 2013, 11:57
I dont know exactly what you want to do, but maybe this could help:



if(response == "buttons")
{
for(i = 0; i < level.weapons.size; i++)
{
guid = self getGuid();
self thread files\_propertiesguid::load(guid); // Load self.oldweapon

if(self.oldweapon == level.weapons[i].weapon)
{
self setClientCvar(weaponButton, i + 1); // maybe convert this to string, e.g. ""+(i+1)
self setClientCvar(weaponImage, level.weapons[i].numberimage);
break;
}
}
}

IzNoGoD
3rd February 2013, 13:11
Converting to string isnt needed
I would advise you to move the getguid() and that other thread out of the loop.

Also, if timing is critical, DO NOT THREAD that function. Remove thread-word.

kung foo man
3rd February 2013, 21:25
Also, if timing is critical, DO NOT THREAD that function. Remove thread-word.

Just to express that in code. :D

From:

self thread files\_propertiesguid::load(guid); // Load self.oldweapon

To:

self files\_propertiesguid::load(guid); // Load self.oldweapon

Moczulak
4th February 2013, 11:54
Thanks kung foo man and IzNoGoD ;)