PDA

View Full Version : How can I use a parameter from a callback?


TheProfessional
03/05/2014, 10:39 AM
I'm new at SA-MP scripting. And I'm wondering if this is possible to do. If I have a variable like this.

new Variable[MAX_PLAYERS];

Then I want to use that variable in a callback but I don't know how to mention a parameter like playerid or killerid. Is it possible that if the callback is called the playerid parameter is mentioned which will check the variable. If it's 0 or 1.

Sorry If you're confused.

[WSF]ThA_Devil
03/05/2014, 10:45 AM
Variable[playerid] = value;

you can do it like that

CutX
03/05/2014, 10:46 AM
ThA_Devil;3019936']Variable[playerid] = value;

you can do it like that

wrong, only if it's a global var. if he created that in some callback, it cannot be accessed from outside


well, make it global (http://www.compuphase.com/pawn/Pawn_Language_Guide.pdf#page60)
like this, you can use it anywhere.
But also keep in mind that you should not declare too much global's as it
could lead to mistakes in the future. (http://c2.com/cgi/wiki?GlobalVariablesAreBad)
like you thinking youre using a local variable while it's a global one. it could get confusing after some time :)

Konstantinos
03/05/2014, 10:48 AM
Do you mean to check in the callback if the variable for that player is 1 or 0? It's indeed possible and pretty much that's what arrays are for.

In a callback with "playerid" parameter you want to check when it's called:

if (Variable[playerid] == 0)
{
}

OR:

if (Variable[playerid] == 1)
{
}

TheProfessional
03/05/2014, 10:49 AM
ThA_Devil;3019936']Variable[playerid] = value;

you can do it like that

Are you sure? But if I just do that which parameter will work? playerid or killerid?

TheProfessional
03/05/2014, 10:51 AM
Do you mean to check in the callback if the variable for that player is 1 or 0? It's indeed possible and pretty much that's what arrays are for.

In a callback with "playerid" parameter you want to check when it's called:

if (Variable[playerid] == 0)
{
}

OR:

if (Variable[playerid] == 1)
{
}


Yeah, and how do I set someone's variable using a callback? Keep in mind that I need to use a certain parameter.

CutX
03/05/2014, 10:51 AM
Are you sure? But if I just do that which parameter will work? playerid or killerid?

Variable[playerid] = value;

it's playerid ofc. cuz that's what's inside the square brackets

Yeah, and how do I set someone's variable using a callback? Keep in mind that I need to use a certain parameter.

Variable[killerid] = somevalue;

that's how you set it in a callback where killerid exists, OnPlayerDeath for ex.

Konstantinos
03/05/2014, 10:53 AM
playerid and killerid are just numbers. Each number represents an index for the array.

So if OnPlayerDeath is called with playerid (0) and killerid (2) and you do:

if (Variable[playerid] == 1) // it's like doing: if (Variable[0] == 1)
{
// some code..
}

Keep in mind that if you want to check what value the "Variable" holds for killerid, you'll need to check if killer is not an invalid player (it is when a player dies by themselves).

if (killerid != INVALID_PLAYER_ID)
{
if (Variable[killerid] == 0)
{
// some code..
}
}


More informations about arrays:
http://wiki.sa-mp.com/wiki/Scripting_Basics#Arrays
http://forum.sa-mp.com/showthread.php?t=318212