Reset structured array with integer/float/string fields -
Programie - 04.03.2012
Hi,
is there a way to reset an array which has an enum with integer, float *AND* string fields?
Example code:
Код:
enum myStructure
{
myVar1,
myVar2,
Float:myVar3,
Float:myVar4,
myVar5[10],
myVar6[100],
myVar7[100]
}
myArray[MAX_PLAYERS][myStructure];
For example I set myVar1 to some player specific value (i.e. UserID read from database).
Then if the player disconnects I want to reset *ALL* variables in this array of the player so the next player starts with an "empty" array.
Another example: I set another player specific string to myVar5 and want to reset this variable in OnPlayerDisconnect.
I know I could do it using something like that:
Код:
myArray[playerID][myVar1] = 0;
myArray[playerID][myVar2] = 0;
myArray[playerID][myVar3] = 0.0;
myArray[playerID][myVar4] = 0.0;
strdel(myArray[playerID][myVar5], 0, 10);
strdel(myArray[playerID][myVar6], 0, 100);
strdel(myArray[playerID][myVar7], 0, 100);
But that's not the way I want to write my code.
Is there a way to reset the array using a loop?
Next problem: myVar2 my contains the playerID of another player which can be 0. How to check whether the variable is set (i.e. is 0 or any other number) or not?
Re: Reset structured array with integer/float/string fields - T0pAz - 04.03.2012
Something like this would work.
pawn Код:
enum myStructure
{
myVar1,
myVar2,
Float:myVar3,
Float:myVar4,
myVar5[10],
myVar6[100],
myVar7[100]
}
new myArray[MAX_PLAYERS][myStructure];
for ( new i; myStructure:i != myStructure; ++i )
{
myArray[playerid][myStructure:i] = 0;
}
Re: Reset structured array with integer/float/string fields - Zeex_ - 04.03.2012
With my
amx_memory include you can do it this way:
pawn Код:
stock ZeroMemory(addr, size) {
for (new i = 0; i < size; i++) {
w@(addr + i*4, 0);
}
}
// Usage:
ZeroMemory(ref(myArray[playerid]), _:myStructure);
Re: Reset structured array with integer/float/string fields - T0pAz - 04.03.2012
Quote:
Originally Posted by Zeex_
With my amx_memory include you can do it this way:
pawn Код:
stock ZeroMemory(addr, size) { for (new i = 0; i < size; i++) { w@(addr + i*4, 0); } }
// Usage: ZeroMemory(ref(myArray[playerid]), _:myStructure);
|
Nice one. Never knew it existed.
Re: Reset structured array with integer/float/string fields -
Programie - 04.03.2012
OK, seems to work.
But how could I check if the variable has been set?
I want to use the array as a replacement for PVars. With PVars I can check if a variable has been set using GetPVarType and test it for PVAR_TYPE_NONE.
How could I do that without using PVars?
There are some things why I don't want to use PVars:
- They are slower if I want to use them in a loop or fast timer.
- I't possible to have typos in the PVar name (i.e. "MyVar" instead of "MyPVar") -> In an array I would use the constants from the enum which are checked while compiling the script
EDIT: Testing ZeroMemory.
Re: Reset structured array with integer/float/string fields -
Jefff - 04.03.2012
myArray[playerID][myVar5][0] = '\0';
Re: Reset structured array with integer/float/string fields -
Programie - 05.03.2012
Quote:
Originally Posted by Jefff
myArray[playerID][myVar5][0] = '\0';
|
The resetting to zero works as I want it.
What I also want is a way to check if the variable has been set or unset (i.e. a difference between 0 and some other state of "zero"). But that's only a nice-to-have feature. I just have to set the value which contains a positive number starting with 0 to -1 and can use that as "unset state".
Thanks for the helpful replies.