Variable.. - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Variable.. (
/showthread.php?tid=509232)
Variable.. -
Snoopythekill - 26.04.2014
This sum score:
pawn Код:
PlayerInfo[playerid][score]++;
What but how do I add a number ? Example +10
What would be as well ?
pawn Код:
PlayerInfo[playerid][score]+10;
Re: Variable.. -
PrinceKumar - 26.04.2014
What you wanna to do plz explain it briefly
Re: Variable.. -
blewert - 26.04.2014
PHP код:
//Add 10 to the player's score.
PlayerInfo[playerid][score] += 10;
which is the same as
PHP код:
//Add 10 to the player's score.
PlayerInfo[playerid][score] = (PlayerInfo[playerid][score] + 10);
So,
a += 1 or
a = (a + 1) is the same as
a++!
The difference here is that
a + b is an arithmetic expression and not a statement.
a + b will yield the result of
a + b, but
a += b will assign the value of
a + b to
a. This means "+=" just adds the value to the variable on the left hand side and stores it.
An example of it in use:
PHP код:
//Declare a and b
new a = 5;
new b = 3;
//Add 3 to 5, put in a
a += b;
//This would print out 8.
printf("%d", a);
Another thing to note about this operator is that like most other operators, it can be "chained" in sequence, which comes in handy sometimes:
PHP код:
//Declare a, b and c.
new a = 5,
b = 3,
c = 6;
//Add C to B, and add B to A
a += b += c;
//Print out result. We should get 14 (5 + (6 + 3))
printf("%d", a);
which is the same as
PHP код:
//Declare a, b and c.
new a = 5,
b = 3,
c = 6;
//Set a to (a + b + c)
a = (a + b + c);
//Print out a. We should get 14 (5 + 3 + 6).
printf("%d", a);
Respuesta: Re: Variable.. -
Snoopythekill - 26.04.2014
Quote:
Originally Posted by blewert
PHP код:
//Add 10 to the player's score.
PlayerInfo[playerid][score] += 10;
which is the same as
PHP код:
//Add 10 to the player's score.
PlayerInfo[playerid][score] = (PlayerInfo[playerid][score] + 10);
So, a += 1 or a = (a + 1) is the same as a++!
The difference here is that a + b is an arithmetic expression and not a statement. a + b will yield the result of a + b, but a += b will assign the value of a + b to a. This means "+=" just adds the value to the variable on the left hand side and stores it.
An example of it in use:
PHP код:
//Declare a and b
new a = 5;
new b = 3;
//Add 3 to 5, put in a
a += b;
//This would print out 8.
printf("%d", a);
Another thing to note about this operator is that like most other operators, it can be "chained" in sequence, which comes in handy sometimes:
PHP код:
//Declare a, b and c.
new a = 5,
b = 3,
c = 6;
//Add C to B, and add B to A
a += b += c;
//Print out result. We should get 14 (5 + (6 + 3))
printf("%d", a);
which is the same as
PHP код:
//Declare a, b and c.
new a = 5,
b = 3,
c = 6;
//Set a to (a + b + c)
a = (a + b + c);
//Print out a. We should get 14 (5 + 3 + 6).
printf("%d", a);
|
Thanks