MZ Can you store and recall a State using a script call?

Hello!
I just happenend to stumble on an official tutorial today for a rewind time skill that I thought was pretty cool.

And while it goes very in depth for ways to store and recall the players hp/mp and items I noticed that it didn't seem to take into account that the player may already have a state applied to them upon entering battle. For example you may still be poisoned from a previous battle, or perhaps you have an item or skill that applies a beneficial state before entering a battle.

I imagine for those who understand scripting there's probably a fairly simple line of code that would take care of this issue but I'm unfortunately inept in this department lol. As it is this is a neat QOL option to consider but without reapplying states it can end up causing tons of problems, especially when I'm using states to control the way certain party members function.

To name a few examples, the protagonist of my game has various levels of Bloodlust applied via states. Each kill increases a variable and at specific amounts different states are applied that alter her stats and behavior. There's also an android character who has a state applied that changes the way he interacts with my games stamina (TP) system. Using the recover all command simply is not an efficient method here nor is the alternative of a long string of conditional branches with a variable for each state. Any help would be greatly appreciated!
You can try to add the state data alongside the hp/mp/tp when youre storing the actor data to the variable in the tutorial
Code:
[$gameActors.actor(ID).hp,
$gameActors.actor(ID).mp,
$gameActors.actor(ID).tp,
JSON.stringify($gameActors.actor(ID)._states),
JSON.stringify($gameActors.actor(ID)._stateTurns)]

and then when you use those values to reset the actor, set the actor's state data to the stored data alongside the other stuff
Code:
const a = $gameActors.actor(2);
const array = $gameVariables.value(1);
a.setHp(array[0])
a.setMp(array[1])
a.setTp(array[2])
a._states = JSON.parse(array[3])
a._stateTurns = JSON.parse(array[4])
Thanks so much! I stuck your code in and it seems to be working flawlessly. I haven't tried with more than 2 states applied to an actor but I assume if it works with 1 and 2 then any number should be fine.

Now I'll just have to see if I can get it to reset my games press turns to actually be fully usable in my combat system haha.
You can try to add the state data alongside the hp/mp/tp when youre storing the actor data to the variable in the tutorial
Code:
[$gameActors.actor(ID).hp,
$gameActors.actor(ID).mp,
$gameActors.actor(ID).tp,
JSON.stringify($gameActors.actor(ID)._states),
JSON.stringify($gameActors.actor(ID)._stateTurns)]

and then when you use those values to reset the actor, set the actor's state data to the stored data alongside the other stuff
Code:
const a = $gameActors.actor(2);
const array = $gameVariables.value(1);
a.setHp(array[0])
a.setMp(array[1])
a.setTp(array[2])
a._states = JSON.parse(array[3])
a._stateTurns = JSON.parse(array[4])
You mentioned a variable you're using for bloodlust – it sounds like you may want to store and recall that too?

JavaScript:
[$gameActors.actor(ID).hp,
$gameActors.actor(ID).mp,
$gameActors.actor(ID).tp,
JSON.stringify($gameActors.actor(ID)._states),
JSON.stringify($gameActors.actor(ID)._stateTurns),
$gameVariables.value(42)]



const a = $gameActors.actor(2);
const array = $gameVariables.value(1);
a.setHp(array[0])
a.setMp(array[1])
a.setTp(array[2])
a._states = JSON.parse(array[3])
a._stateTurns = JSON.parse(array[4])
$gameVariables.setValue(42, array[5])

Original thread: https://forums.rpgmakerweb.com/threads/can-you-store-and-recall-a-state-using-a-script-call.179036/