Question about Variables
#1

Hello guys , if i have a variable under ZCMD for example
pawn Code:
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:
Code:
/duel 0 500
Will the variable get saved and give 500$ to killer id?
Reply
#2

-next reply
Reply
#3

What's the difference then?
static remembers the value and saves it?
Reply
#4

Quote:
Originally Posted by CoDeZ
View Post
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
Reply
#5

Targetid is a variable in your code, so change:
pawn Code:
Targetid[24];
to:
pawn Code:
Targetid;
Reply
#6

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

Quote:
Originally Posted by SEnergy
View Post
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+.
Quote:
Originally Posted by Dan.
View Post
Targetid is a variable in your code, so change:
pawn Code:
Targetid[24];
to:
pawn Code:
Targetid;
Thanks for fixing this one i got it fixed on my script was just giving an example here

Quote:
Originally Posted by oKzrh
View Post
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+
Reply
#8

Quote:
Originally Posted by CoDeZ
View Post
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 !
Reply
#9

Oh my fck sides

Quote:
Originally Posted by SEnergy
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!

https://sampwiki.blast.hk/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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)