Question about instance and its method calls from other class

Started by azdesign, August 24, 2012, 05:29:32 am

Previous topic - Next topic

azdesign

I have a script which define a custom class, simply :

class Poser
  def update
    #run specific animation
  end
end


This Poser class will draw a picture, then animate it. In order to be animated, its update must be placed on scene_menu's update :

class Scene_Map
  def main
    @spriteset = Spriteset_Map.new
    @message_window = Window_Message.new
    Graphics.transition
    loop do
      [b]if $test != nil then $test.animate end[/b]
      Graphics.update
      Input.update
      update
      if $scene != self
        break
      end
    end
    Graphics.freeze
    @spriteset.dispose
    @message_window.dispose
    if $scene.is_a?(Scene_Title)
      Graphics.transition
      Graphics.freeze
    end
  end
end


As you can see, the instance of Poser class is $test, which was created upon triggering an event on the map, so far no problem.
Now, I need to make 2,3 instance of Poser, maybe name it as $test2, $test3, so on. which means, in Scene Map's loop, I have to add

[b]if $test != nil then $test.animate end[/b]
[b]if $test2 != nil then $test2.animate end[/b]
[b]if $test3 != nil then $test3.animate end[/b]


Okay that was stupid, more instances means more lines. my question is, is it possible to :

[b]if [poser_instance] > 0 then [every_poser_instances].animate end[/b]


The point is, I don't have to explicitly mention instance's name to call its method. Because the instance might be more than 10 later.
Thanks in advance  :D
~ Check out my deviantart http://my-az-design.deviantart.com/ ~

Heretic86

You could probably hold them all in an array like $game_map.events, which would allow you to update each poser in the array by iterating each element of the array.  Of course, you'd have to create an empty array when you create game map, so minor modification to game_map.

alias poser_initialize :initialize

def initialize
  # Run the Original
  poser_initialize
  # Empty Array Holds Posers
  @posers = []
end

But to instantiate your actual posers, $game_map.posers.push (poser.new)

if $game_map.posers.size > 0
  for poser in $game_map.posers
    poser.update (or animate)
  end
end
Current Scripts:
Heretic's Moving Platforms

Current Demos:
Collection of Art and 100% Compatible Scripts

(Script Demos are all still available in the Collection link above.  I lost some individual demos due to a server crash.)

azdesign

@Heretic86
Yep that solve my problem. Thanks, never crossed my mind to store each instances in an array.  :haha:
I'll change the array into hash, because I need each poser instance to be easily identified. rather than @game_map.poser[0].set_pose (dunno which character that poser is). In hash, @game_map.poser[:Aluxes].set_pose

Thanks  :D
~ Check out my deviantart http://my-az-design.deviantart.com/ ~