MZ Anyone know what would be required to enable saving in mid-battle?

I spotted this thread trending and it reminded me that I too am interested in what it takes to save mid-battle, but that thread pertains to a different engine than I'm using, so I decided to open a new one.

Though I don't have any particular plans at the moment to actually let the player save mid-battle, there is a mechanic I was considering where proper serialization of the battle state would be an asset: I wanted to create a "time travel" skill that rewinds the battle by N turns.
It's been three days without a single response, so I'm going to bump this…
It's not impossible. But it can be VERY complicated depending on the demands of your specific project.
And because of that, making a general use plugin would be next to impossible.

To that end, I'd suggest considering commissioning this task to see if someone will do it for your project.
I guess you could use the plugin NRP Parallel Common Event in Battle to check for your save button press during battle, run a for each script to go through all the enemies and actors and save their HP/MP/TP/States in an array, then quit to title screen. Then on load start a battle and set all enemies and actors to their saved values.

Still requires some intermediate/advanced scripting to pull off

Somewhat relevant:
A rewind time skill was exactly what I was thinking of for this, so that post is very helpful in enumerating what would be needed… but does it seem like the post may have missed anything? Is HP, MP, TP, and items really the only things that would need to be restored to restart a battle?

That said, while it's quite helpful, it doesn't quite make it to covering what's needed for a rewind time skill that steps back eg 5 turns, rather than to battle start. And it's also not helpful if you actually want to be able to save and load the game in the middle of a battle, though in my case that wasn't the end goal (I just saw it as a means to the goal).

Still… I'm thinking I may try implementing a first draft of the rewind time skill using that post as a reference.
I already told you what to do in the first part of my post which you ignored. It involves intermediate-advanced scripting.
Oh, I must have glossed over that a bit, sorry. Still, most of what you said was also mentioned in that blog post, though the post didn't mention states. Other than that, there's also buffs and debuffs… but I do wonder if this is the wrong angle to take here.

After all, if the thread linked from my opening post can be taken as a guideline (despite being for an older version of the engine that isn't even written in the same language), the $gameTroop might even be saved already. It certainly is one of the "serializable" global objects, as opposed to the "data" objects, though that doesn't by itself mean it is serialized. (I don't think $gameTemp is serialized for example? Though I could be wrong.)

Everything we've discussed so far – HP, MP, TP, items, states – and a few things we haven't mentioned, such as buffs and debuffs – that's all stored in one of the game objects. In fact, for actors at least, all of that is definitely serialized already, so if those are the only things you'd need to worry about, then simply serializing the $gameTroop would surely be enough (if it's not already).

But what about things that aren't stored in a game object? There's some state stored directly in the BattleManager, and there might be state stored in the Scene_Battle…
I must have glossed over that a bit, sorry. Still, most of what you said was also mentioned in that blog post, though the post didn't mention states.
it didnt, but you can add anything else you can think of. youve provided code before, so you should be able to look in it for any property that comes to mind and add them as you go. adding bits to save states and buffs should be just as easy as saving their hp.

It certainly is one of the "serializable" global objects, as opposed to the "data" objects, though that doesn't by itself mean it is serialized.
you wouldnt need to touch any of the data items. theyre not normally altered midgame, so thered be little point in saving and reverting them

But what about things that aren't stored in a game object? There's some state stored directly in the BattleManager[/icode[, and there might be state stored in the [icode]Scene_Battle…
what within those would be an issue to store? i cant think of anything off the top of my head youd need to revert besides the turn count. most of what youd have to revert would be on the battlers, and you can deep copy the battlers if needs be.
what within those would be an issue to store? i cant think of anything off the top of my head youd need to revert besides the turn count. most of what youd have to revert would be on the battlers, and you can deep copy the battlers if needs be.
Yeah, I'm not sure. The main thing that comes to mind is whose turn it is, which is significantly more relevant in TPB than in the default battle system. I think that's stored in the BattleManager somewhere, if I recall correctly. I assume the action timer at least is stored on the battler, though I haven't looked. The action that each actor is planning to perform is stored on the actor though, so saving and restoring in the middle of the action might be less work than it sounds… though I wouldn't bother. If I wanted to save during battle it'd only be at the actor or party command window.
Not that familiar with MZ's system, but from what I have seen the BattleManager.initMembers function should give you an overview over the used properties. Scene_Battle seems to just handle sprites and input windows (but will still put battlers into battle at the beginning and out of battle at the end).

You would have to store $gameTroop in addition to the other game data if I remember correctly, but the page flag issue from the VXAce version should no longer be an issue.

If you choose to clone $gameMap as part of your snapshot, you would likely still need to find a way to update BattleManager._eventCallback to the new interpreter instance.

though I wouldn't bother. If I wanted to save during battle it'd only be at the actor or party command window.
I am not 100% sure, but I think it might be possible that one actor starts its input the same frame as another battler starts its action, even in non-active-tpb mode.

--

One change compared to previous versions would be that MZ's JsonEx.makeDeepCopy does not support shared references by default (each reference will become its own object):
JavaScript:
let actor = $gameActors.actor(2);
let a = [actor, actor];
console.log(a[0] === a[1]);  // "true"
let b = JsonEx.makeDeepCopy(a);
console.log(b[0] === b[1]);  // "false" (unless actor is null)
If you don't need to consider possible other third party plugins, you could possibly work around that by replacing BattleManagers actor references in your save data, if you don't want to implement support for this.

(Hope this helps somehow, it is kind of late)

Original thread: https://forums.rpgmakerweb.com/threads/anyone-know-what-would-be-required-to-enable-saving-in-mid-battle.178056/