SA-MP Forums Archive
assign number automatically as array? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: assign number automatically as array? (/showthread.php?tid=655139)



assign number automatically as array? - Crystallize - 13.06.2018

Let's say I have a 2d array

new OBJECTS[MAX_PLAYERS][5];

and it works like this
objects[1]
objects[2]
objects[3]

Now this is for global.

But let's say I want to make it per player.
How do i assign the numbers automatically.

EG:
Code:
cmd:object(playerid,params[])
{

objects[assign automatically per player instead of always being 1,2,3,4,5] = createplayerobjectxxx

return 1;

}



Re: assign number automatically as array? - Ada32 - 14.06.2018

loop through it


Re: assign number automatically as array? - NaS - 14.06.2018

You'll need the playerid together with the object ID so storing it in an array that is indexed by a playerid would be a good idea, depending on how many slots there should be per player.


Re: assign number automatically as array? - jlalt - 14.06.2018

If your first index is always as playerid.
->
PHP Code:
new OBJECTS[MAX_PLAYERS][5];
#define OBJECTS[%0] OBJECTS[playerid][%0]
cmd:object(playerid,params[])
{
objects[0] = createplayerobjectxxx
// now compiler repaced it with objects[player][0] =... 
return 1;

[ I dont know if I misunderstood or no D:]


Re: assign number automatically as array? - Crystallize - 14.06.2018

Quote:
Originally Posted by Ada32
View Post
loop through it
Loop is the only way?


Btw jalt I know that but I need an automatic number instead of 1,2,3,4,5 however I fixed it temporary.I need a permanent solution.


Re: assign number automatically as array? - Ada32 - 14.06.2018

Quote:
Originally Posted by Crystallize
View Post
Loop is the only way?
what's wrong with looping?


Re: assign number automatically as array? - AmigaBlizzard - 14.06.2018

If you want to use that array per player to store 5 objects each per player:
Code:
new objects[MAX_PLAYERS][5];

cmd:object(playerid, params[])
{
    objects[playerid][0] = createplayerobjectxxx
    objects[playerid][1] = createplayerobjectxxx
    objects[playerid][2] = createplayerobjectxxx
    objects[playerid][3] = createplayerobjectxxx
    objects[playerid][4] = createplayerobjectxxx

    return 1;
}
When player 3 uses this command, the server will have the value "3" for the "playerid" variable and will simply use it as a reference to access the array.
So it will create all 5 objects for player 3 in this array as if you had typed this:
Code:
new objects[MAX_PLAYERS][5];

cmd:object(playerid, params[])
{
    objects[3][0] = createplayerobjectxxx
    objects[3][1] = createplayerobjectxxx
    objects[3][2] = createplayerobjectxxx
    objects[3][3] = createplayerobjectxxx
    objects[3][4] = createplayerobjectxxx

    return 1;
}
The same will happen for player 27.
The server will simply paste "27" as the first index of the array.