PDA

View Full Version : Adding a new stock


Libra_PL
16/01/2012, 06:27 PM
Hey. I'll write this fastly, because I don't have much time. I have this:

for(new i = 0; i < MaxPariticipants; i++)
{
if(PlayersAtMinigame[i] != -1 etc...)
{

I have no idea how to add this into a new stock, LoopThroughParticipants (because I use this over 50 times, would be less code to do this then). The problem is that I don't know how to add with a few brackets. With no one, it would be easy, for example (if return 1 is needed, but I don't need that code below for now):


stock LoopThroughParticipants()
{
for(new i = 0; i < MaxPariticipants; i++)
return 1;
}

So, anyone can give a code of stock using those two things (loop and if...)? I'd be glad if someone would do it (if possible, I think it is).

Thanks

Vince
16/01/2012, 07:37 PM
Use a definition for that.

MP2
16/01/2012, 07:38 PM
Erm, not sure if this is what you're getting at:


stock LoopThroughParticipants()
{
for(new i = 0; i < MaxPariticipants; i++)
{
if(PlayersAtMinigame[i] != -1 etc.)
{
// Do stuff
}
}
return 1;
}

OPremium
16/01/2012, 08:16 PM
As Vince said, you need to use definitions:


#define LoopThroughParticipants() for(new i; i < MaxPariticipants; i++) if(PlayersAtMinigame[i] != -1)


The pre-processor will look for "LoopThroughParticipants()" and replace it with "for(new i; i < MaxPariticipants; i++) if(PlayersAtMinigame[i] != -1)"

So, you can do something like this:


public OnPlayerConnect(playerid)
{
LoopThroughParticipants()
{
//Do something
}
}


And it will be interpreted as:


public OnPlayerConnect(playerid)
{
for(new i; i < MaxPariticipants; i++) if(PlayersAtMinigame[i] != -1)
{
//Do something
}
}


http://forum.sa-mp.com/showthread.php?t=166680 :)