How To Track Amount Spent In Individual Stores?

I'm back with another one that I keep thinking should be easy, but is being stubborn.

I have an amusing idea where I need to know how much money is spent at each individual store and I keep running into trouble.

I started by setting the party's gold to a variable before the store window opens, then setting it to another after the store window closes and comparing the values (then setting the difference to a 3rd variable so I can reuse the first 2 each visit).

I've run into the problem of what happens if they sell stuff? I'd rather not have to make stores purchase only (maybe add a selling counter {regular shop with no inventory} in the store would work, but that's awkward).

On a side note, but definitely related, I am not sure how the system handles code placed after the "Shop Processing" command finishes. I did get to store the money before and after that command, so that does not seem to have an implicit "end event processing" thing involved, but I was unable to display any text messages after that command (like the shop keeper saying "Thanks, you spent \V[*] Poultroons!" (my original plan to see if the variables were working properly) (wound up using an npc to confirm).

Sorry to be so verbose, but that's just me. Any help would be appreciated!
There are a couple of things making this more complicated than you'd otherwise think, but I'm sure there's a way to pull it off and I'm looking into it now.

I'm curious, though, why it wouldn't work to track the party's money when they enter the store and then again after they leave it? If they sell items, that's also them spending less at the shop because it's the shop that has to give them the money for what they're selling, no?
Commands placed after Shop Processing should run as normal. Maybe you are using a plugin that changes that behaviour? Try it out in a new project:
Code:
◆Shop Processing:Potion
:               :Super Potion
◆Text:None, None, Window, Bottom
:    :Pleasure doing business with you!

Not ideal, but you could separate the buy/sell options into separate Shop Processing commands, e.g.
Spoiler: Event example
Code:
◆Loop
  ◆Show Choices:Buy, Sell, Goodbye (Window, Right, #1, #3)
  :When Buy
    ◆Comment:"Purchase only" enabled!
    ◆Shop Processing:Potion
    :               :Super Potion
    ◆
  :When Sell
    ◆Comment:No items to buy.
    ◆Shop Processing:?
    ◆
  :When Goodbye
    ◆Text:None, None, Window, Bottom
    :    :Bye!
    ◆Break Loop
    ◆
  :End
  ◆
:Repeat Above

Alternatively, I have an RMMZ plugin that offers (amongst other things) the ability to track buy/sell stats (quantity and/or price) automatically via variables:
With this, you could assign a variable to the Shop Buy Money+ plugin parameter, set the variable to 0 before entering the shop, then check its value afterwards. To keep track separately for separate stores, you could simply add the result to that store's total buy price variable.
You've probably thought through the single best way to do it without code, with the separate Buy and Sell counters.

For a more elegant solution you'd need to turn to a small plugin. I'll give you some hints on how to do it, and if it's not something you'd feel comfortable writing yourself, you could make a request for it in Plugin Requests.

Here is the main method you want to modify (in rmmz_scenes.js):
JavaScript:
Scene_Shop.prototype.doBuy = function(number) {
    $gameParty.loseGold(number * this.buyingPrice());
    $gameParty.gainItem(this._item, number);
};

Anywhere within that method, you'll want to modify a certain variable by the same amount of Gold being lost (number * this.buyingPrice()). So if you want to modify Variable #5, for instance, you'd add the line:
$gameVariables.setValue(5, $gameVariables.value(5) + (number * this.buyingPrice()));

Obviously we also need to know which variable we want to modify for a given shop. So what I would do is as soon as you talk to the shopkeeper, it changes another variable which tracks which variable we want to change for this shop (lol) - for example, Variable #2 tracks which shop we want to track Gold Spent for, and Variables #3 - #10 hold the values spent for each of the 8 shops in the game. When you visit the shop that uses Var #5, simply set the value of Variable #2 to '5'. Now, change the line above in your code to:
$gameVariables.setValue($gameVariables.value(2), $gameVariables.value($gameVariables.value(2)) + (number * this.buyingPrice()));

I think that should basically give you everything you need to offer you the message of Gold Spent afterwards, and if you don't set the variables back to 0, you can even keep track of the cumulative Gold spent at each shop throughout the game, which could be used for stuff like loyalty programs or whatever you want to get creative with! 🙂
There are a couple of things making this more complicated than you'd otherwise think, but I'm sure there's a way to pull it off and I'm looking into it now.

I'm curious, though, why it wouldn't work to track the party's money when they enter the store and then again after they leave it? If they sell items, that's also them spending less at the shop because it's the shop that has to give them the money for what they're selling, no?

The reason I wanted to keep the sold stuff out of the equation is purely story related.

There are a few small shops with regular + high quality things. But Wallace Martin opened up one of his Wallace Marts in town and has a lot of cheap things at reasonable quality, but nothing really high quality. High quality will cost more and be unaffordable early in the game, but if you spend mostly at Wallace Mart you will put the little guys out of business and lose access to some of the best equipment.

I hope that's not too political for this forum!

Commands placed after Shop Processing should run as normal. Maybe you are using a plugin that changes that behaviour? Try it out in a new project:
Code:
◆Shop Processing:Potion
:               :Super Potion
◆Text:None, None, Window, Bottom
:    :Pleasure doing business with you!

Not ideal, but you could separate the buy/sell options into separate Shop Processing commands, e.g.
Spoiler: Event example
Code:
◆Loop[/INDENT]
[INDENT]  ◆Show Choices:Buy, Sell, Goodbye (Window, Right, #1, #3)[/INDENT]
[INDENT]  :When Buy[/INDENT]
[INDENT]    ◆Comment:"Purchase only" enabled![/INDENT]
[INDENT]    ◆Shop Processing:Potion[/INDENT]
[INDENT]    :               :Super Potion[/INDENT]
[INDENT]    ◆[/INDENT]
[INDENT]  :When Sell[/INDENT]
[INDENT]    ◆Comment:No items to buy.[/INDENT]
[INDENT]    ◆Shop Processing:?[/INDENT]
[INDENT]    ◆[/INDENT]
[INDENT]  :When Goodbye[/INDENT]
[INDENT]    ◆Text:None, None, Window, Bottom[/INDENT]
[INDENT]    :    :Bye![/INDENT]
[INDENT]    ◆Break Loop[/INDENT]
[INDENT]    ◆[/INDENT]
[INDENT]  :End[/INDENT]
[INDENT]  ◆[/INDENT]
[INDENT]:Repeat Above

Alternatively, I have an RMMZ plugin that offers (amongst other things) the ability to track buy/sell stats (quantity and/or price) automatically via variables:
With this, you could assign a variable to the Shop Buy Money+ plugin parameter, set the variable to 0 before entering the shop, then check its value afterwards. To keep track separately for separate stores, you could simply add the result to that store's total buy price variable.


The plugin sounds like it may be the answer! I was trying to avoid plugins until I got the bulk of the work done on the story so I can tell if I run into conflicts easier. My last effort installed too many plugins and I never found the conflict, costing me a lot of work.

I feel safer holding off on all plugins until I have a lot more stuff that wont need them in place (I know, but I'm paranoid!).

You've probably thought through the single best way to do it without code, with the separate Buy and Sell counters.

For a more elegant solution you'd need to turn to a small plugin. I'll give you some hints on how to do it, and if it's not something you'd feel comfortable writing yourself, you could make a request for it in Plugin Requests.

Here is the main method you want to modify (in rmmz_scenes.js):
JavaScript:
Scene_Shop.prototype.doBuy = function(number) {
    $gameParty.loseGold(number * this.buyingPrice());
    $gameParty.gainItem(this._item, number);
};

Anywhere within that method, you'll want to modify a certain variable by the same amount of Gold being lost (number * this.buyingPrice()). So if you want to modify Variable #5, for instance, you'd add the line:
$gameVariables.setValue(5, $gameVariables.value(5) + (number * this.buyingPrice()));

Obviously we also need to know which variable we want to modify for a given shop. So what I would do is as soon as you talk to the shopkeeper, it changes another variable which tracks which variable we want to change for this shop (lol) - for example, Variable #2 tracks which shop we want to track Gold Spent for, and Variables #3 - #10 hold the values spent for each of the 8 shops in the game. When you visit the shop that uses Var #5, simply set the value of Variable #2 to '5'. Now, change the line above in your code to:
$gameVariables.setValue($gameVariables.value(2), $gameVariables.value($gameVariables.value(2)) + (number * this.buyingPrice()));

I think that should basically give you everything you need to offer you the message of Gold Spent afterwards, and if you don't set the variables back to 0, you can even keep track of the cumulative Gold spent at each shop throughout the game, which could be used for stuff like loyalty programs or whatever you want to get creative with! 🙂

Thanks for the mini tutorial! I need to learn how to script for this game and that is very helpful.

I've got programming experience, but it's old. I learned by punching cards in Fortran III and that sort of logic really doesn't apply much anymore...
Yeah, this effect is going to be impossible to implement without using plugins due to the way the shop scene works. You *need* to hook into the buy confirmation function to track the value.
Yeah, this effect is going to be impossible to implement without using plugins due to the way the shop scene works. You *need* to hook into the buy confirmation function to track the value.

Thanks for the confirmation.

I'll keep the 2 counter stores for now and look into plugins when I reach the point where I really need them.

I'm not good enough at rpgm stuff yet to know which plugins may cause conflicts so decided to add them only after the bulk of the story is in place.
You could try something like making any currency lost/gained in a store subtract or add the equivalent amount in a hidden item. It would have to have no limit, or one set to where you want it for a purpose (spend 100k gold in this store = unlock something). There could be a different hidden item for each store as well. Each store has their own tracker basically.

So you run the party's gold count before opening the shop, then add or subtract their gold amount after exiting and give or take that much of the hidden item.
You could try something like making any currency lost/gained in a store subtract or add the equivalent amount in a hidden item. It would have to have no limit, or one set to where you want it for a purpose (spend 100k gold in this store = unlock something). There could be a different hidden item for each store as well. Each store has their own tracker basically.

So you run the party's gold count before opening the shop, then add or subtract their gold amount after exiting and give or take that much of the hidden item.
That runs back into the problem of not taking into account money the party makes by selling items in the same shopping session.
You could try something like making any currency lost/gained in a store subtract or add the equivalent amount in a hidden item. It would have to have no limit, or one set to where you want it for a purpose (spend 100k gold in this store = unlock something). There could be a different hidden item for each store as well. Each store has their own tracker basically.

So you run the party's gold count before opening the shop, then add or subtract their gold amount after exiting and give or take that much of the hidden item.

That's what I was doing with variables, but as Trihan noticed, selling becomes problematic.

That runs back into the problem of not taking into account money the party makes by selling items in the same shopping session.

I'm not really sure if I should just ignore selling, or maybe stick with multiple in-store counters, or even a simple 2 choice introductory "Ya here to buy or sell?" followed by a branch with a store that only sells or only buys - a one counter solution with minimal headache.

Actually, that one sounds the least awkward. I didn't like the 2 counter system as much.

See? Just talking about these things spawns new solutions!

But if I know me, the actual end version will be none of the above...


EDIT: @caethyril actually suggested that last one earlier, but I read it wrong! I hadn't had my coffee yet 🙁

Sorry if I made it look like I ignored your post, I just read it wrong...

I may need to make Coffee a more important item in my game! Right now it just gives a "Haste" effect in battle, but in real life it is so much more than that 🙂

Original thread: https://forums.rpgmakerweb.com/threads/how-to-track-amount-spent-in-individual-stores.135109/