How to change state moment MP hits 0?

Hey I'm trying to make it so a state is applied to an enemy the moment their MP is 0.

I tried a lot of different setups with variables and got it working to apply the state at 0 but only at the end of the turn, is there any way with or without scripts to do it on the moment?
I'm unsure how you've set up your game. If it were an situation like a mini-cut scene with using magic then you could just use events and do it that way. Similarly, you could call that event during combat. You just apply it for the troops in the database. The following is a tutorial on setting up enemy states. It should work in most situations.



If you have a more custom situation then I also have this solution. This is a small script for changing the enemy state to sleep at 0 MP. It has been a long time since I've used Ruby, it might be pretty rough.


Spoiler: Script
Code:
#==============================================================================
# ■ Enemy MP Zero → Sleep State
#------------------------------------------------------------------------------
# Automatically applies the Sleep state when an enemy's MP reaches 0.
# Works during battle and checks whenever MP changes.
#==============================================================================

module MP_ZERO_SLEEP
  SLEEP_STATE_ID = 2  # << Change this to your game's Sleep state ID
end

class Game_Enemy < Game_Battler
  alias lj_mp_zero_sleep_change_mp change_mp
  def change_mp(value, *args)
    old_mp = @mp
    lj_mp_zero_sleep_change_mp(value, *args)
    # If MP has just hit 0 and the enemy is alive, apply Sleep
    if old_mp > 0 && @mp == 0 && alive?
      add_state(MP_ZERO_SLEEP::SLEEP_STATE_ID)
    end
  end
end
I feel like there must be a simple way to do this, but I can't think of one. How many attacks that cost the enemy MP do they have? Is it a recurring enemy, or a one-time boss? You could add a common event to those magical attacks/skills in which you'd check if after they're done, the MP is 0 and then apply the state, but I don't know how time-consuming that would be for you.

I'd assume there would be a script that could do something like this. Maybe try looking at Yanfly's work, if there's anything that could help?

Note: Wrote this before DrFreeman posted their reply, so maybe it's not relevant any more :LZSlol:
I'm unsure how you've set up your game. If it were an situation like a mini-cut scene with using magic then you could just use events and do it that way. Similarly, you could call that event during combat. You just apply it for the troops in the database. The following is a tutorial on setting up enemy states. It should work in most situations.



If you have a more custom situation then I also have this solution. This is a small script for changing the enemy state to sleep at 0 MP. It has been a long time since I've used Ruby, it might be pretty rough.


Spoiler: Script
Code:
#==============================================================================
# ■ Enemy MP Zero → Sleep State
#------------------------------------------------------------------------------
# Automatically applies the Sleep state when an enemy's MP reaches 0.
# Works during battle and checks whenever MP changes.
#==============================================================================

module MP_ZERO_SLEEP
  SLEEP_STATE_ID = 2  # << Change this to your game's Sleep state ID
end

class Game_Enemy < Game_Battler
  alias lj_mp_zero_sleep_change_mp change_mp
  def change_mp(value, *args)
    old_mp = @mp
    lj_mp_zero_sleep_change_mp(value, *args)
    # If MP has just hit 0 and the enemy is alive, apply Sleep
    if old_mp > 0 && @mp == 0 && alive?
      add_state(MP_ZERO_SLEEP::SLEEP_STATE_ID)
    end
  end
end

I feel like there must be a simple way to do this, but I can't think of one. How many attacks that cost the enemy MP do they have? Is it a recurring enemy, or a one-time boss? You could add a common event to those magical attacks/skills in which you'd check if after they're done, the MP is 0 and then apply the state, but I don't know how time-consuming that would be for you.

I'd assume there would be a script that could do something like this. Maybe try looking at Yanfly's work, if there's anything that could help?

Note: Wrote this before DrFreeman posted their reply, so maybe it's not relevant any more :LZSlol:

Thanks for the replies, that script unfortunately causes a crash even when I try it on a test game with 0 other scripts in it, and the video was not quite what I'm looking for

To explain more thoroughly what I'm trying to do, I'm trying to make it in every fight that if an enemy hits 0 MP than they stun that moment and go into a weakened state (like a stagger) then fully recover their MP at the end of the next turn, so while I was able to get it working at turns end I'd really like the weak state to apply the moment their MP drops to 0

I tried setting enemy MP to a variable that turns a switch on at 0 to activate a battle page to apply the state but I couldnt figure out how to get it running properly

I've also already looked through Yanflys scripts (Though not the lunatic ones thoroughly, there might be something in there to do it) and couldn't see a simple one to get the job done

And if theres just a script that could achieve something similar to the mechanic I'm looking for that could be great too
Hello,

that script unfortunately causes a crash even when I try it on a test game with 0 other scripts in it
The script provided by @DrFreeman crashes because there is no change_mp method, instead mp= should do the trick. Here is a modified (untested) version:
Ruby:
#==============================================================================
# ■ Enemy MP Zero → Sleep State
#------------------------------------------------------------------------------
# Automatically applies the Sleep state when an enemy's MP reaches 0.
# Works during battle and checks whenever MP changes.
#==============================================================================

module MP_ZERO_SLEEP
  SLEEP_STATE_ID = 2  # << Change this to your game's Sleep state ID
end

class Game_Enemy < Game_Battler
  #--------------------------------------------------------------------------
  # alias method: mp=
  #--------------------------------------------------------------------------
  alias lj_mp_zero_sleep_change_mp mp=
  def mp=(mp)
    old_mp = @mp
    lj_mp_zero_sleep_change_mp(mp)
    if old_mp > 0 && @mp == 0 && alive?
      add_state(MP_ZERO_SLEEP::SLEEP_STATE_ID)
    end
  end
end

To explain more thoroughly what I'm trying to do, I'm trying to make it in every fight that if an enemy hits 0 MP than they stun that moment and go into a weakened state (like a stagger) then fully recover their MP at the end of the next turn, so while I was able to get it working at turns end I'd really like the weak state to apply the moment their MP drops to 0
Using the script above should give them the state whenever they reach 0 mp.
You can control the removal conditions of the state in your state parameters, then maybe a script like Shaz's state add/remove commands can do the trick to heal the mp?

Putting in the state notetag should make the enemy's mp reach the maximum amount:
onremove: mp = mmp
Hello,


The script provided by @DrFreeman crashes because there is no change_mp method, instead mp= should do the trick. Here is a modified (untested) version:
Ruby:
#==============================================================================
# ■ Enemy MP Zero → Sleep State
#------------------------------------------------------------------------------
# Automatically applies the Sleep state when an enemy's MP reaches 0.
# Works during battle and checks whenever MP changes.
#==============================================================================

module MP_ZERO_SLEEP
  SLEEP_STATE_ID = 2  # << Change this to your game's Sleep state ID
end

class Game_Enemy < Game_Battler
  #--------------------------------------------------------------------------
  # alias method: mp=
  #--------------------------------------------------------------------------
  alias lj_mp_zero_sleep_change_mp mp=
  def mp=(mp)
    old_mp = @mp
    lj_mp_zero_sleep_change_mp(mp)
    if old_mp > 0 && @mp == 0 && alive?
      add_state(MP_ZERO_SLEEP::SLEEP_STATE_ID)
    end
  end
end


Using the script above should give them the state whenever they reach 0 mp.
You can control the removal conditions of the state in your state parameters, then maybe a script like Shaz's state add/remove commands can do the trick to heal the mp?

Putting in the state notetag should make the enemy's mp reach the maximum amount:
onremove: mp = mmp
Hey this fixes it, and everything seems to be working just how I want it too, thanks a whole lot everyone for the help!
And I was able to make the MP recover at the end of the next turn by just setting a variable to equal enemy MP and then a once a turn conditional to recover MP if the variable was 0, which is working fine now, but I'll remember that script too in case I need it later

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-change-state-moment-mp-hits-0.180882/