Trying to store pickup ids to delete them later in a dynamic pickup creator
#1

Solved, thank you!
Reply
#2

You are using streamer, so what I would suggest you to do is to use streamers internal "Extra ID" functionality, together with storing ID boundaries. You won't even need to store any data because you can obtain it using streamer's functions. You won't have your "custom pickups id" then, unless you will implement them on your own (also using streamer's extra_id)
That way you won't have any limit and you will save a lot of memory (since any arrays won't be needed, and you can implement limits on your own using just one variable and incrementing/decrementing it)
So, this is how I would do it:

Player's enum/data:
Code:
enum pData
{
    pickupsCreated,
    pickupsIDBounds[2] // i'd suggest to set default values for both cells to -1 so [pickupsIDBounds] = {-1, -1}
}; // just be sure to reset this data on player connect/disconnect, or use PVars (which I don't recommend if you are obtaining/setting data very frequently)
Creating pickups:
Code:
new pid = CreateDynamicPickup(...);
// set EXTRA_ID to our player's id so we know that he created it
Streamer_SetIntData(STREAMER_TYPE_PICKUP, pid, E_STREAMER_EXTRA_ID, playerid);
// update ID boundaries, assuming pickupsIDBounds is set by default to {-1, -1}
if(pInfo[playerid][pickupsIDBounds][0] == -1 || pid < pInfo[playerid][pickupsIDBounds][0])
    pInfo[playerid][pickupsIDBounds][0] = pid;
if(pInfo[playerid][pickupIDBounds][1] == -1 || pid > pInfo[playerid][pickupIDBounds][1])
    pInfo[playerid][pickupIDBounds][1] = pid;
   
    pInfo[playerid][pickupsCreated]++; // using pickupsCreated you can put an per player limit
On creation, show player the ID from CreateDynamicPickup, or implement your own IDs and also store them in pickup's EXTRA_ID.
Deleting a single pickup (player gives us ID from CreateDynamicPickup, so no need to do any looping just direct memory acces to confirm he created it)
Code:
    new pid = strval(params); // get ID from cmd params or whatever
    if(!IsValidDynamicPickup(pid) || Streamer_GetIntData(STREAMER_TYPE_PICKUP, pid, E_STREAMER_EXTRA_ID) != playerid)
         return SendClientMessage(playerid, -1, "This isn't your pickup dude, or it doesn't exist");

    DestroyDynamicPickup(pid);
    pInfo[playerid][pickupsCreated]--;
    // we don't need to update pickupIDBounds here because they still contain all the created pickups, although we could check if deleted pickup is first or last and update the values to new ones, but it doesn't matter that much
Deleting all player's pickups
Code:
new deletedPickups = 0; // so we can stop if we hit pickupsCreated number, kind of optimization
for(new i = pInfo[playerid][pickupsIDBounds][0]; i <= pInfo[playerid][pickupsIDBounds][1] && deletedPickups < pInfo[playerid][pickupsCreated]; i++) // very important to use less or equal (<=) not just < as it wouldn't delete the last pickup
{
    if(IsValidDynamicPickup(i) &&  Streamer_GetIntData(STREAMER_TYPE_PICKUP, i, E_STREAMER_EXTRA_ID) == playerid)
    {
        DestroyDynamicPickup(i);
        deletedPickups++;
    }
}
// also important to reset his ID Bounds and pickupsCreated
pInfo[playerid][pickupsIDBounds] = {-1, -1};
pInfo[playerid][pickupsCreated] = 0;
This is a much better approach, as it saves a lot of memory, gives us theoretically no limits, and if we wan't limits we can flexibly set them as we want even at runtime.

EDIT
I'm not sure what value streamer is using by default in EXTRA_ID, but if it's 0 then you would need to set data to playerid+1 and check data for playerid+1, otherwise player with id 0 could unintentionally delete normal pickups that don't have any EXTRA_ID set so it's 0. You should check what Streamer_GetIntData(..) returns for an freshly created pickup to see what the default value is.
Reply
#3

Thanks for such a quick answer, but It could be used on a different streamer later, thats why I'd prefer using the basic function, to edit it later. I still tried your code and and both codes creates many pickups when run the /createpickup command. It seems that something is wrong with my /createpickup command
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)