[HELP] Pickup problem.
#1

So im tryng to create and destroy random pickups that respawn after some time with timers.

public OnGameModeInit()
{
SetTimer("officeobj1timer",1000,false);
SetTimer("offceobj2timer",1000,false);
SetTimer("officeobj3timer",1000,false);
SetTimer("officeobj4timer",1000,false);
}

public officeobj1timer()
{
new RandomOficinaObj1 = random( 19 );
switch( RandomOficinaObj1 )
{
case 0:
{
oficinaobj1 = CreatePickup(3027, 19, -279.4040,2602.6125,5.4469, -1); //Cigarettes
Eoficinaobj1 = 0;
}
case 1:
{
oficinaobj1 = CreatePickup(1212, 19, -279.4040,2602.6125,5.4469, -1); //Money
Eoficinaobj1 = 1;
}
case 2:
{
oficinaobj1 = CreatePickup(1951, 19, -279.4040,2602.6125,5.4469, -1); //Health
Eoficinaobj1 = 2;

}
}

public OnPlayerPickUpPickup(playerid, pickupid)
{
new string[256];
new jname[MAX_PLAYERS];
GetPlayerName(playerid, jname, sizeof (jname));

if(pickupid == oficinaobj1)
{
if(gTeam[playerid] == TEAM_ZOMBIES) return 0;

if(Eoficinaobj1 == 99)
{
return 0;
}
else if(Eoficinaobj1 == 1)
{
SetPlayerSpecialAction(playerid,SPECIAL_ACTION_SMO KE_CIGGY);
}
if(Eoficinaobj1 == 2)
{
GivePlayerMoney(playerid, 5000);
}
if(Eoficinaobj1 == 3)
{
SetPlayerHealth(playerid, 100);
}
Eoficinaobj1 = 99;
SetTimer("oficinaobj1timer",RANDOM_TIME,false); //random time defined as 1 minute (60000ms)
return 0;
}

Everything works fine but when i pickup all the pickups in a short time, it gets bugged and sometimes the pickups doesnt works, and in example, the money pickup doesnt give me anything, but then i pickup the health icon and it gives me the money. It seems like the pickups mixes when they respawn in diferent order.

Is this a SAMP problem or my script is wrong? Any ideas?

Thanks.
Reply
#2

Please use [ pawn ] tags and indent..

The problem is the variables retain the old values, and the pickups take those values. Allow me to try and explain.

new pickup1, pickup2;

pickup1 = CreatePickup(blah); // 0
pickup2 = CreatePickup(blah); // 1

If you then destroy pickup1 and create a new pickup, that new pickup will take ID 0, and pickup1 will still be 0, so it will think you're picking up the old one. The same goes for vehicle IDs, textdraws, labels etc.

To fix simply set the variable to -1 or something after you destroy it. You could also hook DestroyPickup to make this happen automatically:

pawn Код:
stock RPV_DestroyPickup(&pickupid) // Parameter is a variable reference
{
    DestroyPickup(pickupid); // Not sure if needed
    pickupid = -1; // Set the reference variable 'pickupid' to -1
    return 1;
}
#if defined _ALS_DestroyPickup
    #undef DestroyPickup
#else
    #define _ALS_DestroyPickup
#endif
#define DestroyPickup RPV_DestroyPickup
(Must be defined before all functions etc.)

NOTE: This function wouldn't work if you passed a literal number to the function, but you don't ever need to do that so it's not a problem. If you DID need to do that, you could bypass it like so:

pawn Код:
new pickupid = 69;
DestroyPickup(pickupid);
But as I said you should never use that as you should never ASSUME IDs.
Reply
#3

Oh, okay ill do it next time.

It seems like it works and i understand now, thanks man !
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)