Learn scripting: Change currency with shop call

Hello everyone,

I am trying to learn to script, with my next script I want to change my currency (Amount + name), open a shop and change back to the default currency.

In my game I have a colosseum to earn BP (Battle Points) that got be stored in a variable ID 52.
For my shop to use BP I call a common event with a script:
Spoiler
// Set ID for variable
var currencyID = 52;
var newCurrencyName = 'BP';

// Shop items
var goods = [];
goods.push([0, 7, 1, 500]);
goods.push([0, 10, 1, 2000]);

// Store your default currency
var storeGold = $gameParty.gold();
var storeCurrencyName = $dataSystem.currencyUnit;
$gameParty.loseGold($gameParty.gold());

// Change gold to new currency
$gameParty.gainGold($gameVariables.value(currencyID));
$dataSystem.currencyUnit = newCurrencyName;

// Open shop
SceneManager.push(Scene_Shop);
SceneManager.prepareNextScene(goods, true);


// Store your currency
$gameVariables.setValue(currencyID, $gameParty.gold());
$gameParty.loseGold($gameParty.gold());

// Change back to default currency
$gameParty.gainGold(storeGold);
$dataSystem.currencyUnit = storeCurrencyName;
All code in green works as it needs to be but the red code is "wrong".
As far as I know/understand, the code in red is executed before the shop close.

I wanna know how to execute the "red code" after the shop is closed.
that is not as easy, because basically it needs to be in a different script attached to a different instance of the interpreter in the main game loop, with that instance waiting until the shop closes.

It is much simpler to use the existing event structure instead of the scripts - the event commands handle the timing in the background.
It's one of the disadvantages of using script functions: While you have much more control than using the event commands, it eleminates all event limitations and thereby causing coordination problems as soon as you need to adapt timing into the main loop.

basically place the scripts in two script commands before and after a regular shop processing command, that would be enough going that way.
basically place the scripts in two script commands before and after a regular shop processing command, that would be enough going that way.
I tried that one too but the variable "currencyID" and "storeCurrencyName" aren't recogniced (Not defined) in the second call.
As what I can do is to store it in Gamevariables with the first script, call a regular store and call the second script that reads again all the variables.

Because I am still learning, I thought there was a solution to make the script wait till the store get closed.

Thanks for your reply !
I thought there was a solution to make the script wait till the store get closed.
of course there is - it's just not something easily explained, and most people using scripts don't bother learning that part.

I think if you make it a specific topic (how to add timed instances to the game loop?) then you will probably get an answer after a while.
aren't recogniced (Not defined) in the second call.
the reason for that is because the script command is sandboxed and all its own variables get deleted when the script command is complete. you'll have to define variables as global, then they survive.
But of course that requires some more thinking to prevent memory leaks...
Thanks for your fast reply, most scripting I learned from caethyril her replies to others.
I'm reading a lot in this forum, try it all out and see what happen.
Changing things here and there and see what happen again.
I make a lot of mistakes but it teach me a lot too 🙂
// Store your default currency
var storeGold = $gameParty.gold();
var storeCurrencyName = $dataSystem.currencyUnit;
$gameParty.loseGold($gameParty.gold());
I would do this differently.

I would do it this way if you want different currency.
JavaScript:
// Store your default currency into a variable for both.
let gold = $gameVariables.setValue(30, []);
$gameVariables.value(30)[0] = $gameParty.gold());
$gameVariables.value(30)[1] = $dataSystem.currencyUnit;

$gameParty.loseGold($gameParty.gold());

at least similar, this way, you can put back the gold and currency from the variable.
or read them from it.
Thanks for your reply, I did (and do again) this way.
But I was trying something new, apperently no solution for now.
I am still learning all this stuff 🙂

Original thread: https://forums.rpgmakerweb.com/threads/learn-scripting-change-currency-with-shop-call.176836/