method for swapping out states

Countyoungblood

Sleeping Dragon
Veteran
Joined
Dec 9, 2017
Messages
622
Reaction score
403
First Language
English
Primarily Uses
RMVXA
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?
 
Last edited:

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
564
Reaction score
274
First Language
German
Primarily Uses
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... :)
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.
 
Last edited:

Countyoungblood

Sleeping Dragon
Veteran
Joined
Dec 9, 2017
Messages
622
Reaction score
403
First Language
English
Primarily Uses
RMVXA
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.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Tomorrow is Thanksgiving, and I'm so excited! I've been exploring MZ a little, and I just downloaded Effekseer to make new animations. I have experience in 3D animation software, so I'll be pretty good at the whole "particle" thing. My first game on MZ will be Fantasy Force--starring a griffin, a dragon, and a pegasus fighting against the Dragon of Darkness.
currently listening to Driftwood Gaming streaming some MZ dev time and it's delightful! had no idea they were this entertaining, love it
In quarantine, will be tested soon. This year is really something... special.
I was at another orientation yesterday and I realized I'll be working with an old friend! We met at the local water park a few years back and it was so nice to see her again! We had a chat, and next week when I officially start, I'll see if maybe we can trade our Zoom IDs so we can talk!
I made some sideview closets for MV!
You can find them in my MV resource thread.

Forum statistics

Threads
105,514
Messages
1,014,343
Members
137,198
Latest member
mmc1980
Top