OnPlayerPickUpPickup
#1

Hi, i've been making a cookie system, and /dropcookie will create a pickup, and when you pickup the cookie pickup it should add a "cookie" to your /stats, but it only Works on the first cookie i drop. Let's say i type /dropcookie three times, and it will create three pickups, but it will only give me a cookie on the first one i pick up.

/dropcookie command:
pawn Код:
if (strcmp(cmd, "/dropcookie", true) == 0)
    {
        new Float:X,Float:Y,Float:Z;
        GetPlayerPos(playerid, X, Y, Z);
        CookiePickup = CreatePickup(1276, 19, X, Y, Z, -1);
        SendClientMessage(playerid, COLOR_RCON,"  You have placed a cookie on the ground.");
        return 1;
    }
OnPlayerPickUpPickup:
pawn Код:
if(pickupid == CookiePickup)
    {
        PlayerInfo[playerid][pCookie] += 1;
        SendClientMessage(playerid, COLOR_LIGHTRED, "* You picked up 1 cookie.");
        return 1;
    }
Reply
#2

Let's run this code in our heads:

1. We place first cookie, SA-MP gives it ID 1 and we put this ID into variable "CookiePickup"
2. We place second cooke, SA-MP gives it another ID 2 and we overwrite variable "CookiePickup" with it
3. We place the third cookie and "CookiePickup" now containes number 3.

And now someone picks up cookies (remember that "CookiePickup" contains number 3, the ID of the third cookie)
1. Player picked up first pickup with ID 1 - we compare it with "CookiePickup" (which is 3) and it fails
2. So as second (2 != 3)
3. But third cookie picks up (because "CookiePickup" has it's ID).

You should use an array to store all cookie pickup IDs. You can't store all cookies in one variable
Reply
#3

I don't really get it, can you help me?
Reply
#4

It means, every time you use the command, the pickupID gets replaced and replaced.
Use an array for the pickupID to store into, You can also block the player from dropping two or more cookies if they have already dropped one. (Still you need to use an array for this one).
Reply
#5

Also, i get that it will overwrite the last placed cookie, but i want to make that i can place like 10 or 50 Cookies at a time, so i forexample can hide them around, like an event or something
Reply
#6

The easiest way is to define an array with constant size:

Код:
// In the beginning of your script
define MAX_COOKIES (16) // Define max. number of cookies
new cookiePickups[ MAX_COOKIES ]; // Define array for 16 pickup IDs

// Initialization (when your gamemode starts) - fill an array with -1 (which means invalid pickup id so we know that this array item is not a pickup ID)
for ( new i=0; i<MAX_COOKIES; i++ ) {
  cookiePickups[ i ] = -1;
}

// Cookie creation command
if (strcmp(cmd, "/dropcookie", true) == 0)
{
  // First check if we have enough space for our new cookie
  new cookieArraySlot = -1;
  for ( new i=0; i<MAX_COOKIES; i++ ) {
    if ( cookiePickups[ i ] == -1 ) {
      // Found an empty array slot
      cookieArraySlot = i;
      break;
    }
  }

  if ( cookieArraySlot != -1 ) {
    // We have an empty array slot so we can create a new cookie
    new Float:X,Float:Y,Float:Z;
    GetPlayerPos(playerid, X, Y, Z);
    
    // Creating pickup and writing pickup ID into array
    cookiePickups[ cookieArraySlot ] = CreatePickup(1276, 19, X, Y, Z, -1);

    SendClientMessage(playerid, COLOR_RCON, "You have placed a cookie on the ground.");
    return 1;
  } else {
    // Not enough space in cookie array, we can't create it
    SendClientMessage(playerid, COLOR_RCON, "Not enough space for new cookie...");
    return 1;
  }
}

// Callback OnPlayerPickUpPickup
// Comparing pickupid with values of cookie array
public OnPlayerPickUpPickup( playerid, pickupid ) {
  // ...
  for ( new i=0; i<MAX_COOKIES; i++ ) {
    if ( pickupid == cookiePickups[ i ]) {
      // This pickupid is in cookie array
      PlayerInfo[playerid][pCookie] += 1;
      SendClientMessage(playerid, COLOR_LIGHTRED, "* You picked up 1 cookie.");
      return 1;
    }
  }
  SendClientMessage(playerid, COLOR_ERROR, "* That was not a cookie...");
  return 1;
  // ...
}
Reply
#7

Thanks alot, it Works, after i edited something in your command, rep+
Reply
#8

How do i make it Destroy the pickup when i pickup the cookie? the cookie pickup will appear Again if i leave the area, and gets back?
Reply
#9

PHP код:
public OnPlayerPickUpPickupplayeridpickupid ) {
  for ( new 
i=0i<MAX_COOKIESi++ ) {
    if ( 
pickupid == cookiePickups]) {
      
PlayerInfo[playerid][pCookie] += 1;
      
SendClientMessage(playeridCOLOR_LIGHTRED"* You picked up 1 cookie.");

      
// Add this vvv
      
DestroyPickuppickupid );
      
cookiePickups] = -1;

      return 
1;
    }
  }
  
SendClientMessage(playeridCOLOR_ERROR"* That was not a cookie...");
  return 
1;

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)