How can I use a parameter from a callback?
#1

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.
Reply
#2

Variable[playerid] = value;

you can do it like that
Reply
#3

Quote:
Originally Posted by [WSF]ThA_Devil
Посмотреть сообщение
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
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.
like you thinking youre using a local variable while it's a global one. it could get confusing after some time
Reply
#4

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:
pawn Код:
if (Variable[playerid] == 0)
{
}
OR:
pawn Код:
if (Variable[playerid] == 1)
{
}
Reply
#5

Quote:
Originally Posted by [WSF]ThA_Devil
Посмотреть сообщение
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?
Reply
#6

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
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:
pawn Код:
if (Variable[playerid] == 0)
{
}
OR:
pawn Код:
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.
Reply
#7

Quote:
Originally Posted by TheProfessional
Посмотреть сообщение
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

Quote:
Originally Posted by TheProfessional
Посмотреть сообщение
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.
Reply
#8

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:
pawn Код:
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).
pawn Код:
if (killerid != INVALID_PLAYER_ID)
{
    if (Variable[killerid] == 0)
    {
        // some code..
    }
}
More informations about arrays:
https://sampwiki.blast.hk/wiki/Scripting_Basics#Arrays
https://sampforum.blast.hk/showthread.php?tid=318212
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)