Get number of active battlers?

Hey there, I was curious if there was a way to get the number of battlers active built in to the engine, or if a work around would be needed?


I.E., you're fighting a troop of Wolf, Wolf, Slime. The Slime is dead, so the # of active battlers is 2.  Wolf, Wolf, Slime, but Slime isn't dead would be 3.
Control Variables > Script: $game_troop.alive_members


will tell you how many of the enemy troop are still alive


Control Variables > Script: $game_party.alive_members


will tell you how many of your party's battlers (first 4) are alive if you run the command while in battle, or how many of your total party members are alive if you run the command outside of battle.


The above will put the number into the variable you choose.  If you just want to use it in a conditional branch and not store it, just put $game_troop.alive_members >= whatever_number_you_want into the Script box of a Conditional Branch instead of using Control Variables.
Is $game_troop an array that can be accessed? The end goal is a "plague" state that "spreads" from enemy to enemy. Right now, I've created a loop that checks if an enemy has appeared, doesn't have the death state, and doesn't have the "plague" state.

So right now, it checks if Battler 1 has Plague state. Then, random # 1-8, checks a loop to see if the corresponding enemy has appeared, doesn't have death state, and doesn't have the Plague. If all that comes back true, it applies the Plague state to that enemy, and breaks loop. But, this has to currently be run for every single battler 1-8, and that's a TON of logic to run.


I'm wondering if I'm over-complicating this, or if I might be worrying too much about the stress of that logic on the average computer. I don't know how much more efficient scripting is vs. eventing for this kind of thing. Sorry for the lack of detail in the first post, at the time I was thinking along the lines of "get the number of active battlers, and then parse through them." But, with an array I could skip getting the number of active battlers.

I think so, anyway. I'm pretty dumb at this stuff.
$game_troop is not an array - it is an object that contains all of the troop information, including an array of the enemies in the troop (which you access through the members or alive_members functions).


Sounds like you're overcomplicating it.


If you want this to happen once per turn or once every 3 turns or whatever, just set up a troop event page with the appropriate span and conditions, and do something like this (assuming 'plague' is state 18, so change it to whatever state is correct for you):

Code:
Conditional Branch: Script: $game_troop.alive_members.any?{|m| m.state?(18)}
  Script: m = $game_troop.alive_members.select{|m| !m.state?(18)}.sample
          m.add_state(18) if m
Branch End

The conditional branch on the first line simply checks to see if any living enemy has the plague state.  If they do, the 2-line script is called, which first randomly chooses one living enemy who does not have the plague state, and if it gets one it applies that state to them.
Thank you, Shaz. You're, as always, a life saver. Or a plague-spreader, in this particular instance.
That'd have to be the most disturbing compliment I've ever received 😀


Did you try it?  Did it work?
Yup! Just implemented it with great success. I'll probably use SceneManager.log_window.add_text to add some flavorful spin on it, like "Such and such's disease is spreading!", but that's for the future. Booting up a troop and watching this state just spread is.. it's just beautiful.

And you gave that to me! And to them! 🙂
haha!  Yeah, I thought a message might be good.  If you want to do that, it'll need to be reformatted slightly - something like this will work:

Code:
Conditional Branch: Script: $game_troop.alive_members.any?{|m| m.state?(18)}
  Script: m = $game_troop.alive_members.select{|m| !m.state?(18)}.sample
          if m
            m.add_state(18)
            # do other stuff
          end
Branch End

The other thing is that it only infects one enemy each time.  If you wanted it to spread more quickly, like if you have 2 with the state, then 2 will be affected, you'd need to get the count of how many are infected, and then choose that many when you sample, and pass the state on in a loop.


Anyhow - have fun with it.  Sounds like you already are 😉
Works beautifully. The final call ended up looking like this:


m = $game_troop.alive_members.select{|m| !m.state?(135)}.sample
if m
m.add_state(18)
SceneManager.scene.log_window.add_text(m.name + "'s Plague is spreading!")
SceneManager.scene.log_window.wait_and_clear
end




It plays wonderfully, works great, and is super flexible. At this point, the only thing I'm even curious about is how to change the text color. Normally, the \c[##] marker would do it, but this is scripting, so.. not sure. But anywho! That's off topic. Thank you again, Shaz 🙂
I don't know if you CAN change the log window text colour.


Also, you're looking for one state but adding a different one - I think there's another 18 that needs to be changed to 135 there (m.add_state).


And the other issue is that the message will show the enemy's name who is GETTING the state, not the one it's coming from.  So if you had a rat and a slime, and the rat had the plague, on that turn the slime would get the plague, but the message would say Slime's Plague is spreading, rather than Rat's Plague is spreading.  To fix that, you'd have to have a second variable that you put that member into (when you're looking to see if anyone has the state on the first line) and use that in the message, instead of the enemy you're adding the state to.
Log window can have color changes, just not sure how to do it through script.

Spoiler
example.png



Thanks for pointing that out, was a copying error.


Thanks for pointing that out as well! Easy enough to do, but I decided to go with writing it as " is afflicted with Plague!", which works with them receiving it. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/get-number-of-active-battlers.72900/