Object and Array
#1

So, I'm making a bomb system.

I want to store each bomb created into an array for Delete that object later, but I wanna make possible plant more than 1 bomb at same time.

So I wanna save each Bomb Object in 1 variable.

pawn Code:
new BombObject[5][MAX_PLAYERS];

//In /plant CMD
BombObject[++][playerid] = CreateObject(1252,0,0,0,0,0,0,0);
їHow can store each object in the different 5 slots? For example, if I have 1 Bomb Planted, then next should save at second slot:

pawn Code:
BombObject[2][playerid] = CreateObject(1252,0,0,0,0,0,0,0);
And so on....

ї?

Thanks.
Reply
#2

pawn Code:
//On playerconnect
for(new i=0;i<5;i++)
{
BombObject[i][playerid] = -1;
}

//on placing:
for(new i=0;i<5;i++)
{
    if(BombObject[i][playerid] == -1){
      BombObject[i][playerid] = CreateObject(1252,0,0,0,0,0,0,0);
      return 1;
    }
    //Create a error message or dialog here to show that the slots are full
    //This part will only get reached if there is no free slot
}
NOT TESTED and right out of my head, should work though

DON'T forget to clean up the bomb/object on player disconnect.
Also i would recommend to use a streamer for these kind of functions.
check incognito's streamer for example.

(to many objects in the plain sa-mp server will not work or crash)

Edit:
Link for the streamer I use for a long time now (never had any trouble with it):
https://sampforum.blast.hk/showthread.php?tid=102865
Reply
#3

pawn Code:
#define MAX_BOMBS 20
#define INVALID_BOMB_ID -1

new gBombs[MAX_BOMBS][MAX_PLAYERS];

// OnPlayerConnect
for(new i = 0; i < MAX_BOMBS; i++)
    gBombs[i][playerid] = INVALID_BOMB_ID;

// OnPlayerDisconnect
for(new i = 0; i < MAX_BOMBS; i++)
    gBombs[i][playerid] = INVALID_BOMB_ID;

// When you create a new bomb, do something like:
CMD:bomb(playerid, params[])
{
    new bomb = GetFreeBombSlot(playerid);

    if(bomb != INVALID_BOMB_ID) // Make sure we actually have an empty slot
        gBombs[bomb][playerid] = CreateObject(...);
    return 1;
}

stock GetFreeBombSlot(playerid)
{
    for(new i = 0; i < MAX_BOMBS; i++)
    {
        if(gBombs[i][playerid] == INVALID_BOMB_ID) // We find an empty slot in the array
        {
            return i;
        }
    }
    return INVALID_BOMB_ID;
}

// When you delete the bomb, make sure you set the bomb slot to INVALID_BOMB_ID
gBombs[bombid][playerid] = INVALID_BOMB_ID;
Reply
#4

You only need to do this once when a player disconnects and in OnGameModeInit() using OnPlayerConnect servs no purpose.

pawn Code:
// OnPlayerConnect
for(new i = 0; i < MAX_BOMBS; i++)
    gBombs[i][playerid] = INVALID_BOMB_ID;

// OnPlayerDisconnect
for(new i = 0; i < MAX_BOMBS; i++)
    gBombs[i][playerid] = INVALID_BOMB_ID;
Reply
#5

Quote:
Originally Posted by [uL]Pottus
View Post
You only need to do this once when a player disconnects and in OnGameModeInit() using OnPlayerConnect servs no purpose.

pawn Code:
// OnPlayerConnect
for(new i = 0; i < MAX_BOMBS; i++)
    gBombs[i][playerid] = INVALID_BOMB_ID;

// OnPlayerDisconnect
for(new i = 0; i < MAX_BOMBS; i++)
    gBombs[i][playerid] = INVALID_BOMB_ID;
Okay, agreed.
But please DO NOT forget to destroy the bombs/objects on player disconnect else you will lose
the object ID and you will flood/fill the server with "uncontrollable" objects

@-Prodigy-: Making it a hobby to top my quick solutions?
Anyway, nice one
Reply
#6

Solved thanks. I made another way to do it.

Just counting the planted bombs of a.player in another variable, so I have a reference of what slot should use.

Thanks all.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)