storing enemy member names in variables (appear halfway enemies)

This scripting noob really needs help with something. I've been learning basics lately and have edited this part in Game_Troop


Code:
  def enemy_names
    names = []
    members.each do |enemy|
      if enemy.alive?
        names.push(enemy.original_name)
        $game_variables[170] = names[0]
        $game_variables[171] = names[1]
        $game_variables[172] = names[2]
        $game_variables[173] = names[3]
        $game_variables[174] = names[4]
      else
        $game_variables[170] = names[0]
        $game_variables[171] = names[1]
        $game_variables[172] = names[2]
        $game_variables[173] = names[3]
        $game_variables[174] = names[4]
      end
    end
    names
  end

In effect this stores the names of the enemies in a variable to be used later. It works great... generally.

I have set up my troops to start with an invissible enemy (to prevent auto victory) and the actual troop 'appear halfway'. Every troop holds a run on turn 0 event that checks how many party members are in the party and then randomizing the number of troops to appear based on the party size (max 4). Then it destroys the invissible enemy.

The problem I'm now experiencing is that the script does not store 'appear halfway' enemy names. Is there any scripting wizard who could walk me through how to fix this?

Thanks in advance ^^

I've moved this thread to Learning Ruby. Thank you.

I thought I was in the correct forum after reading the description. But as I lost my post due to the movement I did notice Shaz's post saying 'This forum is only for completed scripts'. I'm sorry, I'll be more alert next time.
In game_troop line 69 it says member.hidden
so maybe you could use
Code:
  def enemy_names
    names = []
    members.each do |enemy|
      if enemy.alive?
        names.push(enemy.original_name)
        $game_variables[170] = names[0]
        $game_variables[171] = names[1]
        $game_variables[172] = names[2]
        $game_variables[173] = names[3]
        $game_variables[174] = names[4]
      elsif member.hidden
        names.push(enemy.original_name)
        $game_variables[170] = names[0]
        $game_variables[171] = names[1]
        $game_variables[172] = names[2]
        $game_variables[173] = names[3]
        $game_variables[174] = names[4]
      else
        $game_variables[170] = names[0]
        $game_variables[171] = names[1]
        $game_variables[172] = names[2]
        $game_variables[173] = names[3]
        $game_variables[174] = names[4]
      end
    end
    names
  end

Have no idea if that will work.
It gives an error when using member.hidden or enemy.hidden. Saying there is either an undefined method for 'member', 'enemy' or 'hidden'.

This is the original code:
Code:
  def enemy_names
    names = []
    members.each do |enemy|
      next unless enemy.alive?
      next if names.include?(enemy.original_name)
      names.push(enemy.original_name)
    end
    names
  end

Where 'next unless enemy.alive?' checks wether enemies are alive and then processes the rest if conditions are met. That's why I changed it to an if statement to be followed by the else option. Apparantly appear halfway enemies are not considered dead yet though, but not as alive either.

'next if names.include?(enemy.original_name)' causes to only process the same name once when displaying the 'enemy appeared' names at start of battle. Basically by removing this it displays double names now at start of battle if you have multiple of the same unit. I have tested this on a normal troop without my build in event. There it does exactly what I wanted it to do. I don't mind it displaying 'rat appeared' 3 times for instance, but the variables where correct as well as was my goal.

Last, 'names.push(enemy.original_name)' displays the names in the message at start of battle. I won't be using that for the hidden members. I have set up the invissible enemy with the name 'an enemy' so that every battle will start with the line 'an enemy appeared'. The battle field will look empty by then and only after pressing a key the event will actually show the troop. It's how it works by default, but I enjoyed it to work that way. Even the collapse SE of that invissible enemy gives the appearance of the others extra glamour.

All I wanted to be able to do is to set up the names of those other enemies in variables so I could use them later. I believe there is a difference between hidden and appear halfway enemies though, but I'm not sure.
You would simply need to add those names (of the halfway enemies) to the game variables the moment you add them... So it would more of depend on how you are adding them
Thanks. I have done the following due to your advice. I don't think it's exactly what you meant, but thinking more on how they're added got me further ^^ Thank you so much for that

Code:
    def enemy_names
    names = []
    members.each do |enemy|
      names.push(enemy.original_name)
      $game_variables[170] = names[0]
      $game_variables[171] = names[1]
      $game_variables[172] = names[2]
      $game_variables[173] = names[3]
      $game_variables[174] = names[4]
    end
    names = []
    members.each do |enemy|
      next unless enemy.alive?
      next if names.include?(enemy.original_name)
      names.push(enemy.original_name)
    end
    names
  end

In effect names.push(enemy.original_name) is causing the names to be stored. Which will be called for the variables. But as well when command names is run (that displays the pushed or stored names). I then reset the variable names and run the original script again.

Basically it now stores all enemies in the variables (What I did wrong before is not using the push command for appear halfway due to double displaying and overwriting the variables). Thanks to running the original script afterwards it still shows 'an enemy appeared' as 'an enemy' is the only enemy alive at that point.

I've set up a message window showing the variable values and it works fine now. All enemies alive and appear halfway are now stored accordingly ^^

This issue seems to be resolved.

Original thread: https://forums.rpgmakerweb.com/threads/storing-enemy-member-names-in-variables-appear-halfway-enemies.110790/