SA-MP Forums Archive
I need help with my loot system. - 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: I need help with my loot system. (/showthread.php?tid=493567)



I need help with my loot system. - Unfriendly - 08.02.2014

So, what I've got so far is a loot system that adds gas cans when you pickup the gas can objects.
And when a player dies, it drops a gas can, or two, or three, or four, depending on how many they have.

The problem is, I have to check through each playerid and each slot etc. etc.

The way my system works now I would have to type this all in 500 times for each item that was dropped and yeah... it would work, but it would kill my fingers. Surely there's an easier way?

Код:
new
	gascan[MAX_PLAYERS],
	gasdrop[MAX_PLAYERS][4]
;

public OnPlayerDeath(playerid, killerid, reason)
{
	GetPlayerPos(playerid, PosX[playerid], PosY[playerid], PosZ[playerid]);
	if(gascan[playerid] == 1)
	{
	gasdrop[playerid][0] = CreateDynamicObject(1650, PosX[playerid], PosY[playerid], PosZ[playerid] - 0.7, 0.0, 0.0, 0.0);
	GetObjectPos(gasdrop[playerid][0], dX[playerid][0], dY[playerid][0], dZ[playerid][0]);
	}
	if(gascan[playerid] == 2)
	{
	gasdrop[playerid][0] = CreateDynamicObject(1650, PosX[playerid], PosY[playerid], PosZ[playerid] - 0.7, 0.0, 0.0, 0.0);
	GetObjectPos(gasdrop[playerid][0], dX[playerid][0], dY[playerid][0], dZ[playerid][0]);
	gasdrop[playerid][1] = CreateDynamicObject(1650, PosX[playerid], PosY[playerid], PosZ[playerid] - 0.7, 0.0, 0.0, 0.0);
    GetObjectPos(gasdrop[playerid][1], dX[playerid][1], dY[playerid][1], dZ[playerid][1]);
	}
	if(gascan[playerid] == 3)
	{
	gasdrop[playerid][0] = CreateDynamicObject(1650, PosX[playerid], PosY[playerid], PosZ[playerid] - 0.7, 0.0, 0.0, 0.0);
	GetObjectPos(gasdrop[playerid][0], dX[playerid][0], dY[playerid][0], dZ[playerid][0]);
	gasdrop[playerid][1] = CreateDynamicObject(1650, PosX[playerid], PosY[playerid], PosZ[playerid] - 0.7, 0.0, 0.0, 0.0);
    GetObjectPos(gasdrop[playerid][1], dX[playerid][1], dY[playerid][1], dZ[playerid][1]);
	gasdrop[playerid][2] = CreateDynamicObject(1650, PosX[playerid], PosY[playerid], PosZ[playerid] - 0.7, 0.0, 0.0, 0.0);
    GetObjectPos(gasdrop[playerid][2], dX[playerid][2], dY[playerid][2], dZ[playerid][2]);
	}
	if(gascan[playerid] == 4)
	{
	gasdrop[playerid][0] = CreateDynamicObject(1650, PosX[playerid], PosY[playerid], PosZ[playerid] - 0.7, 0.0, 0.0, 0.0);
	GetObjectPos(gasdrop[playerid][0], dX[playerid][0], dY[playerid][0], dZ[playerid][0]);
	gasdrop[playerid][1] = CreateDynamicObject(1650, PosX[playerid], PosY[playerid], PosZ[playerid] - 0.7, 0.0, 0.0, 0.0);
    GetObjectPos(gasdrop[playerid][1], dX[playerid][1], dY[playerid][1], dZ[playerid][1]);
	gasdrop[playerid][2] = CreateDynamicObject(1650, PosX[playerid], PosY[playerid], PosZ[playerid] - 0.7, 0.0, 0.0, 0.0);
    GetObjectPos(gasdrop[playerid][2], dX[playerid][2], dY[playerid][2], dZ[playerid][2]);
	gasdrop[playerid][3] = CreateDynamicObject(1650, PosX[playerid], PosY[playerid], PosZ[playerid] - 0.7, 0.0, 0.0, 0.0);
    GetObjectPos(gasdrop[playerid][3], dX[playerid][3], dY[playerid][3], dZ[playerid][3]);
	}
	}
	return 1;
}

	if (PRESSED( KEY_CROUCH ))
    {
        if(IsValidDynamicObject(gasdrop[0][0]) && IsPlayerInRangeOfPoint(playerid,1.0,dX[0][0],dY[0][0],dZ[0][0]))
        {
			gascan[playerid] = gascan[playerid] +1;
            SendClientMessage(playerid, -1, "* 1 full gas can has been added to your inventory");
            DestroyDynamicObject(gasdrop[0][0]);
        }
        if(IsValidDynamicObject(gasdrop[0][1]) && IsPlayerInRangeOfPoint(playerid,1.0,dX[0][1],dY[0][1],dZ[0][1]))
        {
			gascan[playerid] = gascan[playerid] +1;
            SendClientMessage(playerid, -1, "* 1 full gas can has been added to your inventory");
            DestroyDynamicObject(gasdrop[0][1]);
        }
    }
	return 1;
}



Re: I need help with my loot system. - CuervO - 09.02.2014

You can use a loop.

pawn Код:
for(new i; i < 10;  i ++)
That declaration will create a variable named "i", and will run each time i is less than 10, ascending i by one every time it is run.

For players, you can loop like this

pawn Код:
for(new i = 0; i < MAX_PLAYERS; i ++)
{
     if(!IsPlayerConnected(i)) continue;
     //do your code
}
That will repeat the code for every player online, being 'i' the player.


pawn Код:
if (PRESSED( KEY_CROUCH ))
{
    for(new i = 0; i < MAX_PLAYERS; i ++)
    {
        if(!IsPlayerConnected(i)) continue;
       
        for(new a = 0; a < sizeof(gasdrop[]); a ++)
        {
            if(IsValidDynamicObject(gasdrop[i][a]) && IsPlayerInRangeOfPoint(playerid,1.0,dX[i][a],dY[i][a],dZ[i][a]))
            {
                gascan[playerid] = gascan[playerid] +1;
                SendClientMessage(playerid, -1, "* 1 full gas can has been added to your inventory");
                DestroyDynamicObject(gasdrop[i][a]);
            }
        }
    }
}
Voila, a loop within a loop also works, too, where 'i' are the players and 'a' are the different drops.


Re: I need help with my loot system. - Unfriendly - 10.02.2014

Quote:
Originally Posted by CuervO
Посмотреть сообщение
You can use a loop.
Voila, a loop within a loop also works, too, where 'i' are the players and 'a' are the different drops.
Thanks! +rep

If I store all the drop-able objects in an enum, how do I make the number go up on each drop?
Like, onplayerdeath, if player has a gascan, creates an object labeled ditems[playerid][gascan]+1;

Like how do I keep from overwriting the variable of the last one dropped?

edit:

I think I'm going to do it like this.
Код:
Onplayerdeath(playeridblahblah)
{
if(gascan[playerid] > 1)
{
if(isvaliddynamicobject(gasdrop[playerid])
{
new drop = gasdrop[playerid]+1;
drop=createdynamicobject(blahblah);
}
}
}