Lose exp without losing levels

Hi, I've been struggling with the "change exp" option on the event commands.


Basically, I have an event that according to some decisions, adds or remove exp from the player, the adding part works great by lvling up the player if enough exp is added, but I would like the removing part to not lvl down the player, if being possible, I would like it to work like, if you get enough exp removed to lvl down, you simply remain with 0 exp for that level, as if you just lvl up from the lvl before.


I've tried  just using the event commands, using variables with the player lvl and exp, but I can't achieve what I want that way. I've also tried changing the base scripts of the engine, for example, I've found some change_exp definitions, on game_interpreter line 1202, there's this:

Spoiler
  #--------------------------------------------------------------------------
  # * Change EXP
  #--------------------------------------------------------------------------
  def command_315
    value = operate_value(@params[2], @params[3], @params[4])
    iterate_actor_var(@params[0], @params[1]) do |actor|
      actor.change_exp(actor.exp + value, @params[5])
    end
  end

and on game_actor line 405, there's this:

Spoiler
  #--------------------------------------------------------------------------
  # * Change Experience
  #     show : Level up display flag
  #--------------------------------------------------------------------------
  def change_exp(exp, show)
    @exp[@class_id] = [exp, 0].max
    last_level = @level
    last_skills = skills
    level_up while !max_level? && self.exp >= next_level_exp
    level_down while self.exp < current_level_exp
    display_level_up(skills - last_skills) if show && @level > last_level
    refresh
  end

Now, I think that I should put an "if" somewhere that basically says something like "if the exp amount removed is enough to lvl down, restart the exp for that level", but honestly I don't know exactly how to do that nor on which of those parts put it, if I have to put it on those parts.


If anyone can tell me any lead or actually know something about this topic, I would appreciate the help, as I feel somewhat lost here really.
I've also tried changing the base scripts of the engine

I hope you're not modifying those scripts directly. That's not something you should do under any circumstance.
Anyway, I wrote a quick script snippet that should address the problem you're having. You were on the right track.


Add this below the materials and above main.
 

Spoiler
Code:
#==============================================================================
# ** Game_Actor
#------------------------------------------------------------------------------
#  This class handles actors. It is used within the Game_Actors class
# ($game_actors) and is also referenced from the Game_Party class ($game_party).
#==============================================================================
class Game_Actor < Game_Battler
  #--------------------------------------------------------------------------
  # * Overwrite Method: Change Experience
  #     show : Level up display flag
  #--------------------------------------------------------------------------
  def change_exp(exp, show)
    @exp[@class_id] = [exp, 0].max
    last_level = @level
    last_skills = skills
    level_up while !max_level? && self.exp >= next_level_exp
    @exp[@class_id] = current_level_exp if self.exp < current_level_exp
    display_level_up(skills - last_skills) if show && @level > last_level
    refresh
  end # change exp()
end # Game Actor < Game_Battler
Thanks Rinobi! that snippet does make it work exactly as I wanted, I would appreciate if you could try to at least explain to me barely how this works, as that may be useful knowledge to understand ruby better. But if you can't or don't have the time there's no problem.


Also, I didn't really modify the scripts, I tried changing some stuff but after not getting good results I revert it all back. Anyway, thank you really.
I was on my way out when I wrote that, hence the late reply. I’ll try to explain it as simply as I can, but I can’t go into too much detail without writing out an entire RGSS3 course.


I basically used an overwrite method to change some behavior within the Game_Actor class. Once you’ve found the method you want to change, instead of changing it directly, copy that method into the materials section along with the class that it’s in. In this case it was the change_exp method within the Game_Actor class.


First locate the class.

how_to_exp_00.PNG
Then the method you want to change.


how_to_exp_01.PNG


I choose the change_exp method because it calls the level_down method here:


level_down while self.exp < current_level_exp



level_down is the method which decreases an actor’s level and current_level_exp is the method which returns the minimum experience value required for the actor’s current level. A while loop is used here to continue to decrease the actor’s level until it reaches a value that is no longer less than the minimum value for the current level.



Your problem was that the level of an actor would decrease if they lost enough experience. So logically, you don’t want the level_down method to be called when changing experience. You could simply remove that line, which would also prevent the actor from leveling down, however, the experience value would still be decreased beyond the minimum experience value for the actor’s current level. That may or may not cause issues, but you specially specified that you wanted to experience to remain at "0 for this level".


To achieve that I replaced the level_down line with.

Code:
@exp[@class_id] = current_level_exp if self.exp < current_level_exp

[SIZE=10pt][/SIZE]


Now when the current experience value is less than the current level’s minimum experience value, the current experience value is made equal to the current level’s minimum experience value. It's actually a lot more time consuming to explain this overwrite method than it does to actually write it.

Original thread: https://forums.rpgmakerweb.com/threads/lose-exp-without-losing-levels.63455/