PDA

View Full Version : Question about Variables


CoDeZ
02/08/2012, 11:17 AM
Hello guys , if i have a variable under ZCMD for example

CMD:duel(playerid,params[])
{
new Money,targetid[24];
if(sscanf(params,"ud",targetid,Money)) return SendClientMessage(playerid,-1,"usage:blablabla");
return 1;
}
OnPlayerDeath(playerid,killerid,reason);
{
if(killerid!=INVALID_PLAYER_ID)
{
new Money;
givePlayerMoney(killerid,Money);
}
return 1;
}

question is , will the variable value will get saved and if i typed for example:

/duel 0 500

Will the variable get saved and give 500$ to killer id?

SEnergy
02/08/2012, 11:18 AM
-next reply

CoDeZ
02/08/2012, 11:22 AM
What's the difference then?
static remembers the value and saves it?

SEnergy
02/08/2012, 11:24 AM
What's the difference then?
static remembers the value and saves it?

edit: nope, I got them wrong :X you have to create global variables

Dan.
02/08/2012, 11:25 AM
Targetid is a variable in your code, so change:
Targetid[24];
to:
Targetid;

oKzrh
02/08/2012, 11:25 AM
If your variable is inside your command (as you've showed to us) Than it won't be a global variable that you can actually use in your script.
In the top of your mod, under all of your includes, defines, etc... Where you probably have variables there, just add the "Money" variable, and then you'll be able to use it in your whole script, without using stating it as a new ("new") variable each time :).

CoDeZ
02/08/2012, 11:27 AM
yes, local variables (declared within a function) are destroyed after function ends, if you create new variable with same name it will remember it's value from previous declaration, therefore you don't need to have global variables but you can create static variables everytime you want to use them
Alright thanks. RPed+.
Targetid is a variable in your code, so change:
Targetid[24];
to:
Targetid;
Thanks for fixing this one i got it fixed on my script was just giving an example here

If your variable is inside your command (as you've showed to us) Than it won't be a global variable that you can actually use in your script.
In the top of your mod, under all of your includes, defines, etc... Where you probably have variables there, just add the "Money" variable, and then you'll be able to use it in your whole script, without using stating it as a new ("new") variable each time :).

Thank you , i'll add them in the top of my script because i've got a lot of variables though , converting them to static would take some time.

RPed+

oKzrh
02/08/2012, 11:31 AM
Alright thanks. RPed+.
Thank you , i'll add them in the top of my script because i've got a lot of variables though , converting them to static would take some time.

RPed+

Glad you're happy :).
Thanks for the Rep :cool:!

Misiur
02/08/2012, 11:45 AM
Oh my fck sides

if you create new variable with same name it will remember it's value from previous declaration, therefore you don't need to have global variables but you can create static variables everytime you want to use them

Mr SEnergy, you just won the contest for most retarded sentence in the history of the Internet!

http://wiki.sa-mp.com/wiki/Scripting_Basics#Scope - read this. Once, twice, or even infinite times, I don't care - just as much as you need to understand basic concept of scope and the word "local". When you get this right, you can start helping others.

End of rant