Create Your Own Menu (part5)

Engine : RPG Maker VX Ace

Click here for part 1
Click here for part 2
Click here for part 3
Click here for part 4

6. Assigning the Commands

6a. Set commands handler
An easy way to set what will happens when a command is activated is with a command handler that RGSS3 provided. This can only be applied to Window_Selectable, which is the superclass of our commands window. Insert this code in def create_window_menu_list under @menulist_window = Window_MenuList.new


    #window handler is basically assigning a method to the input symbol or
    #command symbol. When we select or press a symbol, the method will be called
    #window.set_handler(symbol, method)
    @menulist_window.set_handler(:item,      method(:command_item))
    @menulist_window.set_handler(:skill,     method(:command_personal))
    @menulist_window.set_handler(:equip,     method(:command_personal))
    @menulist_window.set_handler(:status,    method(:command_personal))
    @menulist_window.set_handler(:formation, method(:command_formation))
    @menulist_window.set_handler(:crafting,  method(:command_crafting))
    @menulist_window.set_handler(:save,      method(:command_save))
    @menulist_window.set_handler(:game_end,  method(:command_game_end))
    #return_scene method is from Scene_Base superclass. What this method
    #do is basically return to the previous scene.
    @menulist_window.set_handler(:cancel,    method(:return_scene))



The syntax is:
  • set_handler(command symbol, executed method)

Q. Why are these :equip, :status and :formation use the same method which is :command_personal?
A. Becase those commands need actor selection.
And then, we have to create the methods for the commands. Insert these methods to "Scene_Menu" script that we have created.
  def command_item
  end

  def command_personal
  end
  
  def command_skill
  end
  
  def command_equip
  end
  
  def command_status
  end
  
  def command_formation
  end
  
  def command_crafting
  end
  
  def command_end
  end

6b. Setting up command_item
Because we want the item command to call Scene_Item scene, we can just add this code to command_item method
  def command_item
    #Go to Scene_Item
    SceneManager.call(Scene_Item)
  end

6c. Setting up command_personal
This method is basically for activating actor selection. When the actor is selected, the actual method will be called depending on command symbol. First we need a flag variable to indicate whether actor selection is active or not.
Add this code in Scene_Menu start method, under super
    #Initialize actor selection flag.
    @actor_selection = false

And then for the command_personal method
  def command_personal
    #Set actor selection flag to true.
    @actor_selection = true
    #Variable for storing actor selection index.
    @actor_index = 0
    #Update status windows, activate or deactivate the windows depending on 
    #@actor_index
    update_status_window_selection
  end

You might've noticed that there's a new method which is update_status_window_selection that doesn't exist yet. Add this for updating actor status windows based on @actor_index variable.

  def update_status_window_selection
    #loop through @actor_status_windows, getting the individual window
    #and its index.
    @actor_status_windows.each_with_index do |window, i|
      #window will be activated if its index is the same as 
      #@actor_index variable
      if i == @actor_index
        window.activate
        #Select the first window item, causing the window cursor to appear.
        window.select(0)
      else
        window.deactivate
        #Deselect all window items, causing the window cursor to disappear.
        window.unselect
      end
    end
  end

In the Scene_Menu update method, add these codes
    #Update status windows, activate or deactivate the windows depending on 
    #@actor_index. Only when @actor_selection is true.
    update_actor_selection if @actor_selection
    #Because these status windows are inside an array, we have to update it
    #manually.
    update_actor_status_windows

And this

  def update_actor_selection
    #Store the current actor index in temporary variable
    temp_index = @actor_index
    #If left key is pressed
    if Input.repeat?(:LEFT)
      #Decrease the actor index
      @actor_index -= 1
      #If the actor index is less than 0, wrap it to the highest index.
      @actor_index = @actor_status_windows.size - 1 if @actor_index < 0
    end
    #If right key is pressed
    if Input.repeat?(:RIGHT)
      #Increase the actor index
      @actor_index += 1
      #If the actor index is higher than the highest index, wrap it to 0.
      @actor_index = 0 if @actor_index > @actor_status_windows.size - 1
    end
    #If the previous stored index is not the same as the current actor index
    if temp_index != @actor_index
      #Play cursor sound
      Sound.play_cursor
      #Update actor status windows
      update_status_window_selection
    end
    #If confirmation key is pressed
    if Input.trigger?(:C)
      #Update Input so that the trigger flag is set back to false.
      Input.update
      #Play confirmation sound
      Sound.play_ok
      #Set menu actor to currently selected actor.
      $game_party.menu_actor = $game_party.members[@actor_index]
      #Depending on the menulist current symbol, execute these codes.
      case @menulist_window.current_symbol
      when :skill
        command_skill
      when :equip
        command_equip
      when :status
        command_status
      end
    end
    if Input.trigger?(:B)
      #Update Input so that the trigger flag is set back to false.
      Input.update
      #Play cancel sound
      Sound.play_cancel
      #Set actor index to -1, causing all status windows deactivated.
      @actor_index = -1
      update_status_window_selection
      #Set actor selection to false
      @actor_selection = false
      #Activate menu command window
      @menulist_window.activate
    end
  end


  def update_actor_status_windows
    #Because these status windows are inside an array, we have to update it
    #manually.
    @actor_status_windows.each do |window|
      window.update
    end
  end 

Now when you select command that use command_personal method, it'll look like this


Complete script for this tutorial part


1 komentar: