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.

How to call methods in order

Khoa

Member

I have:
Code:
def A
  ....
end

def B
  ...
end

def C
  ...
end
I want to call A, after A finish, I call B, after that I call C.
Can anyone tell me the best way to do that, thank you (^^).
 

khmp

Sponsor

Well you can't begin the name of methods with capital letters. You'll get an error. So "def a" instead of "def A". There's actually a couple ways you can do this. The easiest being:
Code:
def a
  p 'a'
  b
end
def b
  p 'b'
  c
end
def c
  p 'c'
  d
end
def d
  p 'd'
end

You can do something unique and pointless like store the methods in an array or a hash.  :lol:
Code:
def a
  p 'a'
end
def b
  p 'b'
end
def c
  p 'c'
end
def d
  p 'd'
end

methods_to_execute = ['a','b','c','d']
methods_to_execute.each { |method| eval(method) }

Good luck with it Khoa! :thumb:
 

Khoa

Member

Thank you, khmp. But I'm still not understand, I'm new with RGSS (^^)
Example, I want to show a picture, then move it down, then right, then up.
I understand your first way, but the picture need update when it move, if I update each methods, the code will be very long (><).
 

Khoa

Member

I want to show my logo before Scene_Title (Scene_Logo) (^^).
And I also want to learn RGSS, so Scene_Logo is my first step (^^)
EDIT: This is my way, is it ok (><)?
Code:
class Scene_Logo
  def main
    @logo = Sprite.new
    @logo.bitmap = RPG::Cache.title("Logo")
    @move_down = true
    Graphics.transition
    loop do
      Graphics.update
      update
      if $scene != self
        break
      end
    end
    Graphics.freeze
    @logo.dispose
  end

  def update
    move_down if @move_down == true
    move_right if @move_right == true
    move_up if @move_up == true
  end

  def move_down
     if @logo.y < 240
        @logo.y += 10
     return
     end
     @move_down = false
     @move_right = true
   end

   def move_right
     if @logo.x < 500
        @logo.x += 10
     return
     end
     @move_right = false
     @move_up = true
   end

   def move_up
     if @logo.y > 0
        @logo.y -= 10
     return
     end
     @move_up = false
     $scene = Scene_Title.new
   end
end
 

khmp

Sponsor

Lol you edited to include your own work which nullifies mine.  :tongue: :lol:

Oh well. Its still important give it a read over.

Ok well have you started the scene at all. If not I'm going to think you are trying to weasel a request out of me :wink: But you did include the keyword "learn".

Alright where to start. First let's create our scene. So open up the script editor(F11) and insert an empty section above "Main". The scene itself is the most important class from a managerial perspective. He gets the input, the graphics and tells the objects that are contained within the scene to do work. The basic set up for a Scene is the following.

Code:
#==============================================================================
# ** Scene_Logo
#------------------------------------------------------------------------------
#  This class performs logo screen processing
#==============================================================================

class Scene_Logo
  #--------------------------------------------------------------------------
  # * Constant Variables WE WILL NEED THESE LATER
  #--------------------------------------------------------------------------
  Time_Per_Move = 20
  Moves = ['move_right', 'move_down', 'move_left', 'move_up']
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Create resources.
    Graphics.transition   # Required to break the last Graphics.freeze
    loop do                  # Main loop that will run until the Scene changes.
      Graphics.update    # Update the Graphics so we see dynamic elements redrawn.
      Input.update        # Poll the devices for changes in input.
      update                # Call our update
      break if $scene != self # If the Scene has changed break the loop.
    end
    Graphics.freeze       # Required to properly dispose of resources and lead into next transition.
    # Dispose of allocated resources.
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update

  end
end

That is the bare bones for creating a scene. If you managed to get into this scene it would be an infinite loop of nothing but it would technically qualify as a scene. I guess the most important thing is providing the user some way of exiting this scene. So let's take a look at our update method. Right now nothing is inside of it. The most common way for exiting a Scene in game is through input. Let's make it so if the user presses the B or the C key the scene changes to Scene_Title. To find out what keys are pressed in this circumstance we will sue Input.trigger? which takes in the number representation of the key. So now our update method will look like this:
Code:
  #--------------------------------------------------------------------------
  # * Frame Update 
  #--------------------------------------------------------------------------
  def update
    if Input.trigger?(Input::B) || Input.trigger?(Input::C)
      $scene = Scene_Title.new
    end
  end

Now if you run get into this scene you can at least exit it by pressing either the B or C button. Next you said you wanted an Image to be shown and this image will bounce around for a certain amount of frames. To show an image we need to use a sprite object. These little guys are responsible for all the 2D drawing you see when you run the game. We will create this sprite above our Graphics.transition within our main method of Scene_Logo. But its not just the sprite we need to initialize. We also need to actually load the image we want to show. We will initialize the sprite's bitmap to do this.
Code:
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Create resources.
    @sprite = Sprite.new
    @sprite.x = 100
    @sprite.y = 50
    @sprite.bitmap = RPG::Cache.picture('picture_name_here')

    Graphics.transition # Required to break the last Graphics.freeze
    
    loop do             # Main loop that will run until the Scene changes.
      Graphics.update   # Update the Graphics so we see dynamic elements 
                        # redrawn.
      Input.update      # Poll the devices for changes in input.
      update            # Call our update
      break if $scene != self # If the Scene has changed break the loop.
    end
    
    Graphics.freeze     # Required to properly dispose of resources and lead 
                        # into next transition.

    # Dispose of allocated resources.
    @sprite.bitmap.dispose
    @sprite.dispose
  end

After that you wanted to move the image around the screen. Let's implement those method you were talking about first. Using the array method I described. These methods can be placed right after the update method ends.
Code:
  #--------------------------------------------------------------------------
  # * Move Sprite Right
  #--------------------------------------------------------------------------
  def move_right
    @sprite.x += 1
  end
  #--------------------------------------------------------------------------
  # * Move Sprite Down
  #--------------------------------------------------------------------------
  def move_down
    @sprite.y += 1
  end
  #--------------------------------------------------------------------------
  # * Move Sprite Left
  #--------------------------------------------------------------------------
  def move_left
    @sprite.x -= 1
  end
  #--------------------------------------------------------------------------
  # * Move Sprite Up
  #--------------------------------------------------------------------------
  def move_up
    @sprite.y -= 1
  end

Now quickly back to main just below our sprite object creation. We will need to create two more additional variables. One variable will contain the index of the method we are currently calling. If for example the second item in our array of methods was "move_down" and the index variable was set to 1 we would be calling the "move_down" method. The second variable will represent the time till we need to increment or change the index. Let's say that when this variable reaches 20 we change the index.
Code:
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Create resources.
    @sprite = Sprite.new
    @sprite.bitmap = RPG::Cache.picture('picture_name_here')
    @index = 0
    @time = 0

    Graphics.transition # Required to break the last Graphics.freeze
    
    loop do             # Main loop that will run until the Scene changes.
      Graphics.update   # Update the Graphics so we see dynamic elements 
                        # redrawn.
      Input.update      # Poll the devices for changes in input.
      update            # Call our update
      break if $scene != self # If the Scene has changed break the loop.
    end
    
    Graphics.freeze     # Required to properly dispose of resources and lead 
                        # into next transition.

    # Dispose of allocated resources.
    @sprite.bitmap.dispose
    @sprite.dispose
  end

Lastly before we can leave Scene_Logo we need to update to update our timer, change the index when necessary and call the move method. First we will increment the time than if the time equals or exceeds the value we defined elsewhere. We reset the timer and increment the index. If the index equals the size of the array we will have an invalid index so we reset it to zero. Last but not least we call the proper move method in the manner described before.
Code:
  #--------------------------------------------------------------------------
  # * Frame Update 
  #--------------------------------------------------------------------------
  def update
    if Input.trigger?(Input::B) || Input.trigger?(Input::C)
      $scene = Scene_Title.new
    end
    @time += 1
    if @time >= Time_Per_Move
      @time = 0
      @index += 1
      @index = 0 if @index == Moves.size
    end
    eval(Moves[@index])
  end

Sadly even with all that work we still aren't done. The last change is making sure Scene_Logo is started in place of Scene_Menu. To do this open up the Main portion and change line 11 from:
Code:
  $scene = Scene_Title.new
to:
Code:
  $scene = Scene_Logo.new

I hope I explained everything properly if you have any questions just let me know.

Good luck with it Khoa! :thumb:
 

Khoa

Member

Thanks for your help, khmp (^^)

I have learned many things from you (^^).

This is my another question: I want to move my logo, after finishing moving, it automatically call Scene_Title, don't need any input (B or C). And the logo change its direction when it get X or Y coordinate defined by me, not by Time_Per_Move because it's difficult for me to calculate the time (^^). I tried to play se when it change direction, but it doesn't play se once, it repeat many times when moving (><)
 

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