Can someone explain to me class Game_Enemy/Game_Troop?

Hi all,

I know to many of you this is probably really basic stuff, but I've been trying to learn RGSS3 so I can start implementing some graphical ideas for battles and menus. Right now I'm following DP3's tutorials and messing around with Scene_Battle, but even the simplest of stuff (which he still explains very well) is getting the best of me. So I'll start with something simple.

Basically I'm trying to figure out when and how I can access attributes of the class Game_Enemy, which (allegedly) contains data for its id, stats, weaknesses, and so on. More specifically, I'm trying to show a graphic when an enemy acquires a certain state; in other words, I'm using Game_Enemy, Game_Battler, Game_Troop, and Spriteset_Battle to determine whether an enemy has a state, and then to show a graphic.

So I started by inserting a Game_Battler method into Victor's Element States script which runs a method to check for states to add when an enemy is hit by a certain element (this is because the state is acquired when an enemy is hit by a weakness, or "result.resist > 1.0").

Code:
  def element_state_change(id)
    regexp = /<ELEMENT STATE (ADD|REMOVE) #{id}: (\d+)(?:, *(\d+)%)?>/i
    get_all_notes.scan(regexp) do
      type = $1.downcase
      id   = $2.to_i
      rate = $3 ? $3.to_i / 100.0 : 1
      type == "add" ? add_state_normal(id) : remove_state(id) if rand < rate
      if type == "add" && id == 17
        phoe_create_cutin_graphic
      end
    end
  end

The conditional is where my own code begins. It's pretty simple: If the script is adding a state and that state happens to be 17 (the state I'm checking for), execute another method in Game_Battler:

Code:
  def phoe_create_cutin_graphic
    if !create_cutin == true && !dead?
      create_cutin = true
    else
      create_cutin = false
    end

Also very simple: If boolean create_cutin is not already true and the instance of Game_Enemy isn't dead (since we don't want to show the graphic if an enemy is dead), make the boolean true.

I then went into Game_Troop to access this boolean:

Code:
  def determine_cutin_eligibility
    if enemy.create_cutin == true
      @cutin_eligibility = true
    else
      @cutin_eligibility = false
    end
  end

More of the same... I think. If that instance of Game_Battler (Game_Enemy in this case) return variable create_cutin as true, then it saves a variable that can be accessed via "attr_reader," which will be used in Scene_Battle to determine whether or not to draw the graphic.

So I could keep going on a bit about how I made a new viewport in Spriteset_Battle in order to show the graphic and so on, but for now I'm concerned with the fact that I received an error for the Game_Troop method (undefined variable or method "enemy") which can't possibly be true since the variable is defined right here on line 68:

Code:
enemy = Game_Enemy.new(@enemies.size, member.enemy_id)

So why the heck does it return this error? I'm also confused as to when I can reference a class like "enemy.method." Does it have to be a universal variable or can it be run at any time as long as the class is running? Also since all the battle graphics are drawn through Spriteset_Battle, how can I connect Game_Troop and Spriteset_Battle so that the latter runs a method when a Game_Troop method returns true?

I think I'm understanding the idea of syntax, but the ability to connect classes and have them function alongside each other really confuses me. Hopefully someone can help me understand this!
I'm not going to look at Victor script and the like, but I will try to answer your question

I received an error for the Game_Troop method (undefined variable or method "enemy") which can't possibly be true since the variable is defined right here on line 68:
The variable 'enemy' defined at that line is local variable, thus not accessible outside the scope. You're trying to access it outside its scope. I think you haven't fully understand yet the iteration such as 'each', 'while', 'loop', 'until', etc.

Troop members are stored in variable @enemies as an array.
If you access @enemies[0] it will access a Game_Enemy on the first index
I'm not going to look at Victor script and the like, but I will try to answer your question


The variable 'enemy' defined at that line is local variable, thus not accessible outside the scope. You're trying to access it outside its scope. I think you haven't fully understand yet the iteration such as 'each', 'while', 'loop', 'until', etc.

Troop members are stored in variable @enemies as an array.
If you access @enemies[0] it will access a Game_Enemy on the first index

Thanks for the response, Theo.

So then local variables are only stored inside the method, not available to the entire class? Also if I'm understanding you correctly, since the variable @enemies is an array of local variable "enemy," it's basically the array that contains all instances of Game_Enemy? Lastly, how come other methods can reference "enemy.method" such as this snippet later on in Game_Troop:

Code:
  def make_unique_names
    members.each do |enemy|
      next unless enemy.alive?
      next unless enemy.letter.empty?
      n = @names_count[enemy.original_name] || 0
      enemy.letter = letter_table[n % letter_table.size]
      @names_count[enemy.original_name] = n + 1
    end
    members.each do |enemy|
      n = @names_count[enemy.original_name] || 0
      enemy.plural = true if n >= 2
    end
  end

It seems like my method was doing the same thing, referencing the variable "enemy." This one doesn't seem local because it's accessing the same data at line 68, even though it doesn't mention @enemies. Sorry to be asking so many questions, as you can tell I'm still thoroughly confused. :headshake: But what you said about local variables did help.
So then local variables are only stored inside the method, not available to the entire class?
Correct

----------------------------------------------------------------
Also if I'm understanding you correctly, since the variable @enemies is an array of local variable "enemy," it's basically the array that contains all instances of Game_Enemy?
Based on these lines, yes.
Code:
enemy = Game_Enemy.new(@enemies.size, member.enemy_id)
enemy.hide if member.hidden
enemy.screen_x = member.x
enemy.screen_y = member.y
@enemies.push(enemy)

----------------------------------------------------------------
Lastly, how come other methods can reference "enemy.method" such as this snippet later on in Game_Troop:
You see this line
Code:
   members.each do |enemy|
If you change it to
Code:
   members.each do |foe|
Then all the entire iteration would be
Code:
     next unless foe.alive?
     next unless foe.letter.empty?
     n = @names_count[foe.original_name] || 0
     foe.letter = letter_table[n % letter_table.size]
     @names_count[foe.original_name] = n + 1
----------------------------------------------------------------
So why the keyword 'members' is accessible?
It's a method available on Game_Troop, which basically is the same as calling @enemies manually

----------------------------------------------------------------
To make it simple, you have an array of integer
Code:
array = [1,2,3,4,5,6]
Now you want to iterate it. So, call it whatever you like, such as 'number'
Code:
array.each do |number|
  puts number
end
It will print 1,2,3,4,5,6 on separate line in console. Get it?

Original thread: https://forums.rpgmakerweb.com/threads/can-someone-explain-to-me-class-game_enemy-game_troop.96145/