Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Operations on arrays

Is there any easy way to perform the same operation on every object in an array?

What I'm looking to do is have the battle command window for each actor display at the same time, which as far as I can work out, requires a seperate window object for each actor in the party (default it reuses the same window but just moves it along for each actor and changes the current actor variable) - and it makes sense to put them in an array.

So if I change @actor_command_window into an array of window objects (one per party member), how do I deal with @actor_command_window.dispose and @actor_command_window.update ?

I realise I could probably just do "for" statements for each time I want an operation to apply to every object in the array, like:
Code:
for i in 0...@actor_command_window.size
@actor_command_window[i].dispose
end
...I was just wondering if there was a quicker way.
 
Well you could make a Window_Array class that responds to every method of class Window, I have done in the beta version of my Advanced Battle Commands script to improve compatibility, but well here I have another example with Window_BattleStatus

Code:
class Window_BattleStatus
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    @data = []
    for i in 0...$game_party.actors.size
      actor = $game_party.actors[i]
      @data[i] = Window_ActorStatus.new(actor)
    end
  end
  #--------------------------------------------------------------------------
  # * Dispose
  #--------------------------------------------------------------------------
  def dispose
    for i in 0...$game_party.actors.size
      @data[i].dispose
    end
  end
  #--------------------------------------------------------------------------
  # * Set Level Up Flag
  #     actor_index : actor index
  #--------------------------------------------------------------------------
  def level_up(actor_index)
    @data[actor_index].level_up
  end
  #--------------------------------------------------------------------------
  # * Add Actor
  #--------------------------------------------------------------------------
  def add_actor
    @data << Window_ActorStatus.new($game_party.actors[-1])
  end
  #--------------------------------------------------------------------------
  # * Remove Actor
  #--------------------------------------------------------------------------
  def remove_actor(actor_id)
    @data.delete_at(actor_id)
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    for i in 0...$game_party.actors.size
      @data[i].refresh
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    for i in 0...$game_party.actors.size
      @data[i].update
    end
  end
  #--------------------------------------------------------------------------
  # * Set Visibility
  #--------------------------------------------------------------------------
  def visible=(bool)
    for window in @data
      window.visible = bool
    end
  end
end
#==============================================================================
# ** Window_ActorStatus
#------------------------------------------------------------------------------
#  This window displays the status of all party members on the battle screen.
#==============================================================================
class Window_ActorStatus < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #-------------------------------------------------------------------------
  def initialize(actor)
    @actor = actor
    x = (4 - $game_party.actors.size) * 80 + actor.index * 160 + 80  - 80
    y = 320
    width = 160
    height = 160
    super(x,y,width,height)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
    self.visible = (not Battler_Setup::Battle_Status_Hidden)
    self.back_opacity = 160
    @level_up = false
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    x = 4
    draw_actor_name(@actor, x, 0)
    draw_actor_hp(@actor, x, 32, 120)
    draw_actor_sp(@actor, x, 64, 120)
    if @level_up
      self.contents.font.color = normal_color
      self.contents.draw_text(x, 96, 120, 32, "LEVEL UP!")
    else
      draw_actor_state(@actor, x, 96)
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super
    return if not self.visible
    # Slightly lower opacity level during main phase
    if $game_temp.battle_main_phase
      self.contents_opacity -= 4 if self.contents_opacity > 191
    else
      self.contents_opacity += 4 if self.contents_opacity < 255
    end
  end
  #--------------------------------------------------------------------------
  # * Level Up
  #--------------------------------------------------------------------------
  def level_up
    @level_up = true
  end
end
 
So using the each method, my line would be
Code:
@actor_command_window.each { |Window_Command| Window_Command.update }
??

Are there any more specific examples of something similar in a script :P?
 

Jared

Member

Window_Command is a constant. Everything that begins with a uppercase letter is a constant.
array.each {|element| element.do_something}

example:

names = ["Peter", "Leif", "Thomas"]
names.each {|name| print("Hello " + name)}

This Code will be handled as follows:

name = "Peter"
print("Hello" + name) #=> "Hello Peter"
name = "Leif"
print("Hello" + name) #=> "Hello Leif"
name = "Thomas"
print("Hello" + name) #=> "Hello Thomas"

You see the local variable between | | gets the value of the item, which is currently iterated. You can give this local variable any name, but it must be a local variable.
And every variable that begins with a lowercase letter is a local variable.

The for-loop is intern handled like an each-iterator, but in my opinion it´s better if you use iterators instead of loops =P
 
Last question, is it possible to do more than one operation per each method?

Something like:
Code:
@actor_command_window.each { |window| window.active = false & window.visible = false }
?
 

Jared

Member

Code:
@actor_command_window.each { |window|
    window.active = false
    window.visible = false
}

or

Code:
@actor_command_window.each { |window| window.active = false; window.visible = false}

instead { and } you can also use do and end

Code:
@actor_command_window.each do |window|
    window.active = false
    window.visible = false
end
 
Jared said:
instead { and } you can also use do and end

Code:
@actor_command_window.each do |window|
    window.active = false
    window.visible = false
end
Yeah I ended up working that out, but I still didn't know about using the semi-colons. Thanks for that.
 
To Ruby, ; is the equivilent of a new line when evalutaing.

Although you don't put it at the end of iterators, like..

if a > b;
c = 2;
end;

is wrong, it would be

if a > b
c = 2;
end

and could also be written

if a > b { c = 2; }
or
if a > b { c = 2 }

Some languages will use one syntax over another, or both, or require ; or not, its these little quirks that make the difference in learning programming languages.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top