Taknig a number away (minus)
#1

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

Код:
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
Reply
#3

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;
Reply
#4

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

No.

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

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

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

Edited my part, look again @ second post
Reply
#8

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!
Reply
#9

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


Forum Jump:


Users browsing this thread: 1 Guest(s)