Reset structured array with integer/float/string fields
#1

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?
Reply
#2

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;
}
Reply
#3

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);
Reply
#4

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.
Reply
#5

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.
Reply
#6

myArray[playerID][myVar5][0] = '\0';
Reply
#7

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)