MV Detecting "game state change"

I'm trying to build a plugin that creates auto passive states, and am struggling to find a good way to detect that "something significant" changed. I'm only interested in "mechanical" changes, not "cosmetic" changes.

I've noticed that Game_BattlerBase.prototype.refresh covers a great many cases. It does not, however, cover at least the following cases:

  • Taking a step on the map (need Game_Actor.prototype.onPlayerWalk)
  • Transferring to another map (seems like I'd need Game_Player.prototype.performTransfer)
  • Starting or ending battle (it would sometimes cover these, as TP or EXP is likely to change at the start or end of battle, and that calls refresh… but that's only partial coverage)
  • Probably a bunch of things that events can do (some would lead to a refresh, others might not)
  • Is that all? I likely missed something…

Enumerating every possible case is certainly a possibility; I'd prefer to narrow it down to as few cases as possible though.

Another possibility I considered is hooking into scene update and just bulldozing through the checks on every frame. That might have been barely sufficient if I only needed to worry about the map and battle scenes, but unfortunately, there are quite a few changes that can occur in other scenes – using an item at the menu, equipping or unequipping an item, changing formation order, buying or selling at a shop, and probably more. Hooking Scene_Base.prototype.update also won't do – there are too many scenes where you can't rely on a map existing. In any case, running the checks on every frame seems excessive – most frames have no "mechanical" changes in them, only "cosmetic" changes.

As an additional note, the reason I'm not just overriding Game_BattlerBase.prototype.states is because I want the State Resist trait to be able to cancel out a passive trait. But if I call isStateResist from state, I get a stack overflow. The overflow can be solved relatively easily by introducing a recursion guard, but that is only a bandaid solution – for every active plugin that uses this approach in the traits system, the stack grows deeper and performance degrades a tiny bit. I ran into some quite noticeable performance drops due to having only 4 or 5 such plugins. (Admittedly, one of the 5 was worse than the others, but still.)
Mh, unless someone has a better idea I think you'd have to patch those methods separately.

As far as I'm aware, event commands generally call refresh as needed, so that part shouldn't be a problem...? Depends on exactly what qualifies a change as "something significant".
Significant meaning that there is a concrete effect on the game that's more than just cosmetic.

For example, I just thought of another case – when a variable or switch changes value.

Given plugins, I suspect it may not actually be possible to exhaustively enumerate every case… though "when the player takes a step" probably covers the vast majority of things, anything done in a menu or by pressing a key would potentially be missed.
I too am not sure what distinguishes a "significant" change from a "cosmetic" one, and how that relates to your auto passive states.
For example, if you want your plugin to use arbitrary Javascript code for state conditions and want "significant" to mean "any change that could cause that arbitrary Javascript code to return a different result" then pretty much anything could feasibly be significant.

If you want to check the conditions every frame, you could hook into Scene_Base.prototype.update instead. Although that may leave you with 1-frame stretches where your state is not yet updated.
(Edit: From what I remember the game should always load a dummy map, even if no save state has been loaded yet)(Edit2: Nevermind, I think I remembered it wrong, that would probably need to be checked)

If it is about state conditions, you could add support for common significant changes, and allow the user to add its own conditions if they need them (Checking for updates would be the responsibility of the user then).
In addition to the ones you mentioned there could also be changes to money/inventory in general, changes in party constellation, play time or timer state, number of in-battle turns, screen distance to other events or the player trying to run into impassable terrain.

You might also need to be careful about infinite loops in this case (e.g. multiple auto passive states resisting each other)
I too am not sure what distinguishes a "significant" change from a "cosmetic" one, and how that relates to your auto passive states.
A "cosmetic" change means the screen looks different, but it won't affect what happens when you do any particular thing. For example, weather effects, screen shake, animations, and the like are all cosmetic.

It is true that it's a little difficult to draw the line between cosmetic and non-cosmetic changes. After going through the list of possible event commands for cases I'd missed, I'm left wondering whether or not pictures and the timer should be considered significant.

For example, if you want your plugin to use arbitrary Javascript code for state conditions and want "significant" to mean "any change that could cause that arbitrary Javascript code to return a different result" then pretty much anything could feasibly be significant.
Indeed, that's part of the issue here, but that's why I decided to focus on non-cosmetic changes.

If you want to check the conditions every frame, you could hook into Scene_Base.prototype.update instead. Although that may leave you with 1-frame stretches where your state is not yet updated.
I considered this, as mentioned in the opening post. But that'll run the update even on the title screen, which strikes me as really wrong (there probably isn't even a map yet?).

If it is about state conditions, you could add support for common significant changes, and allow the user to add its own conditions if they need them (Checking for updates would be the responsibility of the user then).
I likely need to do something like this anyway to be able to support any possible plugin.

In addition to the ones you mentioned there could also be changes to money/inventory, changes in party constellation, play time or timer state, number of in-battle turns, screen distance to other events or the player trying to run into impassable terrain.
Hmm, the player trying to run into impassable terrain is a case I didn't think of. Not sure if I have it covered yet.

I already thought of money, inventory, and "party constellation" (interesting term there). I also thought of the timer and am a bit unsure as to how that should work. Play time doesn't seem significant to me. Number of in-battle turns is significant, but should be covered by hooking "end of turn".

I think screen distance to other events should be covered by updating when events move… though that seems like it could be a new performance issue, but perhaps I can use the strategy of Game_Map here – all the hooked events only set a flag, which is then checked in update to do the actual update.

I guess with that approach it could be fine to hook Scene_Base.prototype.update, as the flag will always be false on the title screen and thus no update will happen.

You might also need to be careful about infinite loops in this case (e.g. multiple auto passive states resisting each other)
Oh my, I didn't think of passive states resisting each other. I don't think my approach would create an infinite loop in that case, but it could maybe lead to a "flicker" where one frame adds the first state and removes the second and the next frame adds the second state and removes the first. I'll have to test it out.




I've been working on this since my last post and now have a big list of things. I think some of these may be a little overkill, but I suppose I'll see.

  • Game_Battler: onAllActionsEnd, onTurnEnd, onBattleStart, onBattleEnd, refresh
  • Game_Actor: learnSkill, forgetSkill
  • Game_Enemy: appear
  • Game_Party: gainGold, gainItem
  • Game_Player: refresh (this covers map transfers), getOnOffVehicle
  • Game_Event: refresh
  • Game_CharacterBase: increaseSteps, setPosition, copyPosition
  • Game_Switches, Game_Variables, Game_SelfSwitches: onChange
  • Game_Timer: start, stop, maybe onExpire? Not quite sure on this one.

I also considered refreshing upon changing an actor's text too, but while that is clearly significant, I think there's a pretty low chance that someone would want a state to change as a result of it.

I'm probably missing a few things that can change in battle as well (escape ratio, for example?). I was focusing more on map or event changes so far.
if you want your plugin to use arbitrary Javascript code for state conditions and want "significant" to mean "any change that could cause that arbitrary Javascript code to return a different result" then pretty much anything could feasibly be significant.
This!

...I'm not just overriding Game_BattlerBase.prototype.states...
I don't know what kind of protection you implemented against infinite recursion, but (plugin conflicts aside) I don't imagine it would cause a big problem?

In case it helps, I would personally consider something like this (untested):
JavaScript:
;void (function() {
"use strict";
  const P = "_passiveAntiLoop";
  const alias = Game_BattlerBase.prototype.states;
  Game_BattlerBase.prototype.states = function() {
    // this[P] = true;  // whoops, wrong place
    const a = alias.apply(this, arguments);
    if (P in this)
      return a;  // source traits only from non-passives
    this[P] = true;
    const r = this.stateResistSet();
    const b = this._passiveStates.filter(function(id) { return !r.contains(id); });
    delete this[P];
    return a.concat(b);
  };
})();
This assumes passives are added/removed in a finite and clearly-defined number of ways, e.g. on item equip/unequip, so you can implement a simple "passive states" array and check for things like passive-passive resistance when they are added/removed.

Also, I used the stateResistSet thing here instead of a potentially more cross-compatible isStateResist call, because doing so avoids reconstructing the trait objects array for every passive state.

[Edit: corrected location of the line this[P] = true in my code. 😴]
Hmm, that's an interesting idea. I wonder if it has an advantage over directly adding the passive states to the regular states list?
(I made a small edit to the code in my previous post: the flag was being set too early. 😴)

At root, it just means that anything that directly checks _states (rather than going through the states method) will ignore the passives. So, naturally, you'll have to manually process any relevant things for passives in your own code~

As-is, it also groups them: "normal" states first, then passives. There might be other differences.
That's still quite similar to the version I had from a few years ago, but using statesSet instead of isStateResist is an optimization I hadn't thought of.

I am not sure if it's worth trying to go back to this method when I've already got the other method working. But thanks for the ideas – I may apply them elsewhere.

Original thread: https://forums.rpgmakerweb.com/threads/detecting-game-state-change.178463/