method for swapping out states

I want a method for checking if an object has one of several states and if so checks their current TP and modifies that state dependent on the current TP value. the objects can only have one of the states at a time they need to replace eachother. I want this code to run one time per round so it should figure out a value and set a state then exit.

I was thinking something along the lines of..

Code:
def tp_state_update
 if state?(25||26||27)
   case
     when self.tp <= 30
     set_state = 25
     when self.tp >= 31 && self.tp <= 80
     set_state = 26
     when self.tp >= 81 && self.tp <=100
     set_state = 27
   end
  
end

case
    when state?(25) && set_state !=25
      self.remove_state(25)
      self.add_state(set_state)
    when state?(26) && set_state !=26
      self.remove_state(26)
      self.add_state(set_state)
    when state?(27) && set_state !=27
      self.remove_state(27)
      self.add_state(set_state)
end
end


edit update: fixed some errors but still not functioning properly. after applying state 25 and increasing TP nothing changes.

edit2: reached the second case statement but having trouble applying the state now..

edit3: nearly there but still finding a type error with certain tp values.. not sure why yet.

edit4: removed the ranges to try to solve the type error.. seems to be related to state rather than the ranges.

edit5: attempted to move from state 26 back to state 25 by reducing TP.. same error.
any thoughts?
Hey there,
There are still a few misconceptions in your code, unless I'm horribly mistaken:

---

if state?(25||26||27)

If you want to check multiple states, you'd have to call the state? method multiple times. The way it is written currently, 25||26||27 is handled as one expression and is equal to 25 (since 25 is regarded as true by the OR-operation).
So you could either write

if state?(25) || state?(26) || state?(27)

or a bit closer to your original idea something like

if [25, 26, 27].any? { |id| state?(id) }

(array.any? checks if at least one of its elements fulfills the condition set up in the following block)

---

Edit: I never used Case-Statements without an object to compare each branch to, but apparently this is also legal in Ruby. So the following is probably obsolete... 🙂
Spoiler
Case-Statements in Ruby are used to compare different objects against one element. One example would be

case self.tp
when 0..30
# Invokes (0..30) === self.tp, checking if TP is between 0 and 30
when 31..80 # TP between 31 and 80
when 81, 83, 95..100 # TP is 81, 83 or between 95 and 100
else # None of the above apply
end

Unlike in some other languages, only the first matching branch of a case-Statement will get executed.
if you don't have a common element and just want a chain of conditions, you can use if-Statements with "elsif":

if self.tp <= 30 # TP is less/equal 30
elsif self.tp <= 80 # first branch does not apply, but TP is less/equal 80
elsif self.tp <= 100 # second branch does not apply either, but TP is less/equal 100
else # None of the above apply
end
---

One thing to note about the standard state behaviour is that if you remove a state using "remove_state(id)", that battler then becomes immune to that state until you clear their action result object ("result.clear").
I don't know what the idea behind this behaviour is but, since your code contains a lot of removing and adding, it might be worth mentioning here.
very good information thankyou for taking the time to reply. using your help I've updated the script..

Code:
def tp_state_update
 
 
 if state?(25) or state?(26) or state?(27)
   
   case
     when self.tp.between?(0,30)
     set_state = 25
     when self.tp.between?(31,60)
     set_state = 26
     when self.tp.between?(61,100)
     set_state = 27
   end
   

      if state?(25) && set_state != 25
        self.remove_state(25)
        self.add_state(set_state)
      elsif state?(26) && set_state != 26
        self.remove_state(26)
        self.add_state(set_state)
      elsif state?(27) && set_state != 27
        self.remove_state(27)
        self.add_state(set_state)
end
end
end

when is result.clear called? Im intending to put this to run at the end of every round after everything else in on_turn_end.

Original thread: https://forums.rpgmakerweb.com/threads/method-for-swapping-out-states.115035/