How to query gold amount?

Hi! Very quick question ...

How might I query the amount of gold a player has in a script call? And how might I save that number to a variable?

Many thanks!! :LZSsmile: :LZSjoy: :LZSsmile:
You don't need a script call. Control Variables will let you put the gold amount into a variable.

Control Variables > Game Data > Other > Gold
You don't need a script call. Control Variables will let you put the gold amount into a variable.

Control Variables > Game Data > Other > Gold

Thanks Shaz! But I'm actually doing a bit of a 4th wall breaking thing where I'd like to tell the player the script call to query the gold 🙂

A bit of a weird question I suppose, but I'd love to include it!
Thanks Shaz! But I'm actually doing a bit of a 4th wall breaking thing where I'd like to tell the player the script call to query the gold 🙂

A bit of a weird question I suppose, but I'd love to include it!

The script call to query gold is $gameParty.gold()


And how might I save that number to a variable?

You can do any of the following, which will create a variable named "gold":
  • var gold = $gameParty.gold(); looks either oldschool or amateurish.
  • let gold = $gameParty.gold(); the more modern and recommended approach.
  • const gold = $gameParty.gold(); creates a constant named "gold", instead of a variable. The value of a constant can not be changed afterwards.
If you want to go a bit further with your 4th wall breaking, you could use the const option to store the party's gold into the gold variable, then you could try to overwrite the variable using for example: gold = 0; or gold = 42; or similar, and then showing the following error message: Uncaught TypeError: Assignment to constant variable.

If you want to save the gold to an RPG Maker variable instead, use: $gameVariables.setValue(1, $gameParty.gold());, which will save the gold value to variable #0001. To use a different variable, just replace the 1 with any other positive integer greater than 0. Avoid using numbers with leading zeroes, such as 0010. Most people won't notice, but this is actually octal notation in JavaScript (0010 in the octal system is 8 in our decimal system). If you want to be particularly nerdy, you can use a binary number like 0b10 (which is 2 in our decimal system), or a hexadecimal number like 0xff (which is 255 in our decimal system).

Personally, I'd recommend using one of the former options, because $gameVariables is needlessly obscure and doesn't really provide any benefit to your players, whether they know how to code or not.
@Nolonar Thank you so much for taking the time to provide such an informative answer. Very much appreciated.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-query-gold-amount.140316/