[C#][RAGE] How to take money from player?
-
I'm working on a mod where cars consume fuel and where cars can be refueled on gas stations. My problem is I don't know how to make the player pay for refueling. It's boring to get the fuel for free.
Does anybody know the function or method in C# to take money from the player?
-
This post is deleted!
-
You can use
GTA.Player.Money
to set or get how much money does the player have, so for example if you want to sell the player something for $40 you'd do:GTA.Player.Money - GTA.Player.Money(40);
-
@Miawwe Thank you for replying. I already tried Game.LocalPlayer.Character.Money but it doesn't work. When I use the Game.DisplayNotification the Money Value shows me 0 although my character has over 700k for example.
GTA.Player.Money - GTA.Player.Money(40); doesn't work either. Can it be that this works just in C++? I'm using C# and the ..Character.Money value seems to be useless.
-
@Magu Try these natives, I could confirm that these work without any problem.
SET_PED_MONEY = This sets the money you wish to have with Ped
GET_PED_MONEY = This gets the money what ped has.Here an Usage/Example :
let's say I want to charge the player $50 for something.
Simple function would do the trick.private void MoneyFuntion() { Ped Player = GTA.Game.Player.Character; int PlayerMoney = Function.Call<int>(Hash.GET_PED_MONEY, Player); if (PlayerMoney >= 100) { Function.Call(Hash.SET_PED_MONEY, Player, (PlayerMoney -= 50)); UI.Notify("You have been charged with $50") } }
-
I tried your code but what do I need to make it run? I fit the code a little bit to make the compiler accept it.
There still is one thing the compiler is moaning about: "Hash.SET_PED_MONEY".
I guess a using-directive is missing or something else.public void moneyFunction (){
if (Game.IsKeyDown(Keys.Left)) { Ped player = Game.LocalPlayer.Character; int PlayerMoney = NativeFunction.Natives.CallByHash<int>(**Hash**.GET_PED_MONEY, player); NativeFunction.Natives.Call(**Hash**.Natives.SET_PED_MONEY, player, (PlayerMoney -= 50)); Game.DisplayNotification("You have been charged with $50"); } }
Do you have any idea what is wrong?
-
@Magu He showed you the Script Hook V .NET equivalent, which still wouldn't work as the player's money isn't stored in the PED_MONEY - it's stored in a STAT.
I would think RPH would know to set the player's money (I'm not familiar with the API), but if it can't then you'll have to convert this code to RPH: https://github.com/crosire/scripthookvdotnet/blob/421a83571afa7934f9d8f8ec3592c9b327f99ea6/source/scripting/World/Player.cs#L77
This is what you would convert the native function calls to using RPH: http://docs.ragepluginhook.net/?topic=html/T_Rage_Native_DynamicNativeFunction.htm
-
@Jitnaught I searched in Native DB and found these methods about STAT:
BOOL STAT_GET_INT(Hash statHash, int *outValue, int p2)
BOOL STAT_SET_INT(Hash statName, int value, BOOL save)That example below is just to get the cash value. I'll try to set the value after the get method works:
public void moneyfunction (){
if (Game.IsKeyDown(Keys.Left)) { int* val; Hash hash = NativeFunction.Natives.GET_HASH_KEY("SP0_TOTAL_CASH"); NativeFunction.Natives.STAT_GET_INT(hash, &val, -1); Game.DisplayNotification("Money: " + &val); } }
Something is wrong with the second parameter (int* val) . It seems to be a pointer but I have no idea how to handle pointers. The compiler doesn't accept that syntax I tried
I think that's the last missing puzzle piece
-
int* val;
This needs to be.
int val;
&val means you are storing the value by reference into a normal variable not a variable pointer.
-
@Dreamlncode The method "BOOL STAT_GET_INT(Hash statHash, int *outValue, int p2)" expects an "int *outValue". What is the *outValue?
-
@Magu When you use & on a variable it turns the variable into a pointer (a variable that references another variable). So if you had an int variable and used & on it it would return an int*, which is what the native function is looking for.
Your code should look very similar to the SHVDN code I posted above, namely int* val should be int val and you need to surround your unsafe code (&val) with unsafe { }
If that still doesn't work, it may be because RPH doesn't support pointers (again, I'm not familiar with the API), and you might have to use a NativeArgument instead http://docs.ragepluginhook.net/?topic=html/M_Rage_Native_NativeArgument__ctor_10.htm
-
@Jitnaught Unfortunately I don't know how to use NativeArgument. I hope someone in the forums has an idea.
-
After hours of experimenting the code finally works
The problem was the hash value. It was always empty so I put the hash value directly as a argument.For those who want to know how to add cash to the player, here's an example:
public void moneyfunction ( ){
if (Game.IsKeyDown(Keys.Left)) { NativePointer nativePointer = new NativePointer(); int money = 0; NativeFunction.Natives.STAT_GET_INT(0x324C31D, nativePointer, -1); // SP0_TATAL_CASH = 0x324C31D Michael's Cash nativePointer.SetValue(nativePointer.GetValue<int>() + 2000); // Cash value + 2000 NativeFunction.Natives.STAT_SET_INT<int>(0x324C31D, nativePointer.GetValue<int>(), -1); // Add 2000$ }
}
I know this code needs to be optimized but it will do for now
Thank you very much for your help, without your help I would never find it out
-
@Magu i know its been 2 years but im tryna add money to a player the same way as you and was wondering if you figured out a way to add it to the character the player is currently using and not only michael