SA-MP Forums Archive
Taknig a number away (minus) - 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: Taknig a number away (minus) (/showthread.php?tid=526891)



Taknig a number away (minus) - Cole_William - 19.07.2014

So i been looking all over for a way to minus a number in a users file like


Ignore indenting, it looked all messed up with it when posting.
pawn Код:
if(PlayerInfo[playerid][pCash] < 500)
{
SendClientMessage(playerid, COLOR_GREY, "You don't have enough money");
RemovePlayerFromVehicle(playerid);
}
GivePlayerMoney(playerid, -500);
PlayerInfo[playerid][pRenting] = 1;
SendClientMessage(playerid, COLOR_GREEN, "Enjoy your ride!");
PlayerInfo[playerid][pCash] = 1;
I want PlayerInfo[playerid][pCash] = 1; to minus 500 from their current amount, how do we do this?


Re: Taknig a number away (minus) - Hanger - 19.07.2014

Код:
Operator	 Meaning						 Usage
==	 	 Left is equal to Right				 if (Left == Right)
!=		 Left is not equal to Right			 if (Left != Right)
>		 Left is greater than Right			 if (Left > Right)
>=		 Left is greater than or equal to Right	 	 if (Left >= Right)
<		 Left is less than Right			 if (Left < Right)
<=		 Left is less than or equal to Right		 if (Left <= Right)

-=		 Minus		 		 		 Variable -= 10
+=		 Plus		 		 		 Variable += 10
=		 Assign a value to variable	 		 Variable = 10



Re: Taknig a number away (minus) - sammp - 19.07.2014

The method of doing this is really simple.

You might want to make this on your OnPlayerUpdate: Easy way of stopping money hacks

pawn Код:
if(GetPlayerMoney(playerid) != Player[playerid][Money])
        {
            ResetPlayerMoney(playerid);
            GivePlayerMoney(playerid, Player[playerid][Money]);
        }
        if(GetPlayerScore(playerid) != Player[playerid][Level])
        {
            SetPlayerScore(playerid, 0);
            SetPlayerScore(playerid, Player[playerid][Level]);
        }]
Change the variables, obviously.

Then, rather than your method you can do this:

pawn Код:
if(PlayerInfo[playerid][pCash] < 500) return SendClientMessage(playerid, COLOR_GREY, "You don't have enough money");
PlayerInfo[playerid][pRenting] = 1;
SendClientMessage(playerid, COLOR_GREEN, "Enjoy your ride!");
PlayerInfo[playerid][pCash] -= 500;
Using the -= operator we can take a value away from a variable.

Don't do this, an exception will occur:

pawn Код:
PlayerInfo[playerid][pCash] - 500;



Re: Taknig a number away (minus) - Cole_William - 19.07.2014

Quote:
Originally Posted by Hanger
Посмотреть сообщение
So i just do

PlayerInfo[playerid][pCash] = -1; ?? But if i do that it just sets their money to -1 instead of minus 1.


Re : Taknig a number away (minus) - S4t3K - 19.07.2014

No.

To take a number away from a variable, use the "-=" operator.

pawn Код:
main()
{
      new var = 84:
      var -= 3;
      printf("%d", var);
}
Output : 81.


Re: Taknig a number away (minus) - sammp - 19.07.2014

Quote:
Originally Posted by Cole_William
Посмотреть сообщение
So i just do

PlayerInfo[playerid][pCash] = -1; ?? But if i do that it just sets their money to -1 instead of minus 1.
look at my comment


Re: Taknig a number away (minus) - Hanger - 19.07.2014

Edited my part, look again @ second post


Re: Taknig a number away (minus) - Cole_William - 19.07.2014

Quote:
Originally Posted by sammp
Посмотреть сообщение
The method of doing this is really simple.

You might want to make this on your OnPlayerUpdate: Easy way of stopping money hacks

pawn Код:
if(GetPlayerMoney(playerid) != Player[playerid][Money])
        {
            ResetPlayerMoney(playerid);
            GivePlayerMoney(playerid, Player[playerid][Money]);
        }
        if(GetPlayerScore(playerid) != Player[playerid][Level])
        {
            SetPlayerScore(playerid, 0);
            SetPlayerScore(playerid, Player[playerid][Level]);
        }]
Change the variables, obviously.

Then, rather than your method you can do this:

pawn Код:
if(PlayerInfo[playerid][pCash] < 500) return SendClientMessage(playerid, COLOR_GREY, "You don't have enough money");
PlayerInfo[playerid][pRenting] = 1;
SendClientMessage(playerid, COLOR_GREEN, "Enjoy your ride!");
PlayerInfo[playerid][pCash] -= 500;
Using the -= operator we can take a value away from a variable.

Don't do this, an exception will occur:

pawn Код:
PlayerInfo[playerid][pCash] - 500;
Thanks for the help!


Re: Taknig a number away (minus) - sammp - 19.07.2014

Quote:
Originally Posted by Hanger
Посмотреть сообщение
Edited my part, look again @ second post
Just saying += doesn't just mean "add"

It means addition assignment.

Things like +=, -=, *= etc are all called Compound assignment operators.
Things like +, -, * etc are all called Arithmetic operators.