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.

Error with Custom script

ok. I'm working on my scripting skills right now, and I made a little script that lets the player define certian options in the game, such as the windowskin. I've been getting a syntax error at the end of the script, which last I checked, meant that I either have one too many, or not enough ends in the script. I've been playing around with both and have not been able to fix this error. I was wondering if anyone could help by helping me fix this problem. I was also wondering if someone would take the time and help me clean up the scirpt and getting working right, (because I'm pretty sure that it is not working right because it is proably the worst script you will ever see...).


This first code goes in the Window section of the scripts. It should define the parameters of the window, correct?

Code:
#==============================================================================
# ** Window_GameOption
#------------------------------------------------------------------------------
#  This window shows options for the games settings.
#==============================================================================

class Window_Help < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 640, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
  end
  #--------------------------------------------------------------------------
  # * Set Text
  #  text  : text string displayed in window
  #  align : alignment (0..flush left, 1..center, 2..flush right)
  #--------------------------------------------------------------------------
  def set_text(text, align = 0)
    # If at least one part of text and alignment differ from last time
    if text != @text or align != @align
      # Redraw text
      self.contents.clear
      self.contents.font.color = normal_color
      self.contents.draw_text(4, 0, self.width - 40, 32, text, align)
      @text = text
      @align = align
      @actor = nil
    end
    self.visible = true
  end
end

This second script is just an update to Scene_Menu which modifies the menu accept the options choice.

Code:
#==============================================================================
# ** Scene_Menu
#------------------------------------------------------------------------------
#  This class performs menu screen processing.
#==============================================================================

class Scene_Menu
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     menu_index : command cursor's initial position
  #--------------------------------------------------------------------------
  def initialize(menu_index = 0)
    @menu_index = menu_index
  end
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Make command window
    s1 = $data_system.words.item
    s2 = $data_system.words.skill
    s3 = $data_system.words.equip
    s4 = "Status"
    s5 = "Options"
    s6 = "Save"
    s7 = "End Game"
    @command_window = Window_Command.new(160, [s1, s2, s3, s4, s5, s6, s7])
    @command_window.index = @menu_index
    @command_window.x = 0
    @command_window.y = 0
    # If number of party members is 0
    if $game_party.actors.size == 0
      # Disable items, skills, equipment, and status
      @command_window.disable_item(0)
      @command_window.disable_item(1)
      @command_window.disable_item(2)
      @command_window.disable_item(3)
    end
    # If save is forbidden
    if $game_system.save_disabled
      # Disable save
      @command_window.disable_item(4)
    end
    # Make gold window
    @gold_window = Window_Gold.new
    @gold_window.x = 0
    @gold_window.y = 400
    # Make location window
    @location_window = Window_Location.new
    @location_window.x = 480
    @location_window.y = 440
    # Make status window
    @status_window = Window_MenuStatus.new
    @status_window.x = 160
    @status_window.y = 0
    # Execute transition
    Graphics.transition
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of windows
    @command_window.dispose
    @gold_window.dispose
    @status_window.dispose
    @location_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update windows
    @command_window.update
    @gold_window.update
    @status_window.update
    @location_window.update
    # If command window is active: call update_command
    if @command_window.active
      update_command
      return
    end
    # If status window is active: call update_status
    if @status_window.active
      update_status
      return
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update (when command window is active)
  #--------------------------------------------------------------------------
  def update_command
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Switch to map screen
      $scene = Scene_Map.new
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # If command other than save or end game, and party members = 0
      if $game_party.actors.size == 0 and @command_window.index < 4
        # Play buzzer SE
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      # Branch by command window cursor position
      case @command_window.index
      when 0  # item
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to item screen
        $scene = Scene_Item.new
      when 1  # skill
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Make status window active
        @command_window.active = false
        @status_window.active = true
        @status_window.index = 0
      when 2  # equipment
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Make status window active
        @command_window.active = false
        @status_window.active = true
        @status_window.index = 0
      when 3  # status
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Make status window active
        @command_window.active = false
        @status_window.active = true
        @status_window.index = 0
      when 4  # options
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to options screen
        $scene = Scene_GameOption.new
      when 5  # save
        # If saving is forbidden
        if $game_system.save_disabled
          # Play buzzer SE
          $game_system.se_play($data_system.buzzer_se)
          return
        end
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to save screen
        $scene = Scene_Save.new
      when 6  # end game
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to end game screen
        $scene = Scene_End.new
      end
      return
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update (when status window is active)
  #--------------------------------------------------------------------------
  def update_status
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Make command window active
      @command_window.active = true
      @status_window.active = false
      @status_window.index = -1
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # Branch by command window cursor position
      case @command_window.index
      when 1  # skill
        # If this actor's action limit is 2 or more
        if $game_party.actors[@status_window.index].restriction >= 2
          # Play buzzer SE
          $game_system.se_play($data_system.buzzer_se)
          return
        end
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to skill screen
        $scene = Scene_Skill.new(@status_window.index)
      when 2  # equipment
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to equipment screen
        $scene = Scene_Equip.new(@status_window.index)
      when 3  # status
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Switch to status screen
        $scene = Scene_Status.new(@status_window.index)
      end
      return
    end
  end
end

This third script, which is also the one with problems, is the main part of the script and goes in the Scene section.

Code:
#==============================================================================
# ** Scene_GameOption
#------------------------------------------------------------------------------
#  This class performs game options screen processing.
#==============================================================================

class Scene_GameOption
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Make Options Window
    @game_window = Window_GameOption.new
    # Make Options for game
    s1 = Windowskin Style
    @command_window = Window_Command.new(160, [s1])
    @command_window.index = @menu_index
    @command_window.x = 20
    @command_window.y = 40
    # Execute transition
    Graphics.transition
        # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of windows
    @game_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update window
    @game_window.update
    update_gameoption
    return
  end
  #--------------------------------------------------------------------------
  # * Frame Update (gameoption)
  #--------------------------------------------------------------------------
  def update_gameoption
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Switch to menu screen
      $scene = Scene_Menu.new(0)
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # Branch by command window cursor position
      case @command_window.index
      when 0  # Windowskin
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Make new choices
        s1 = Style1
        s2 = Style2
        @command_window2 = Window_Command.new(160, [s1, s2])
    @command_window2.index = @menu_index
    @command_window2.x = 600
    @command_window2.y = 440
    # Execute transition
    Graphics.transition
        # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      choiceupdate
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update window
    @game_window.update
    update_choiceoption
    return
  end
  #--------------------------------------------------------------------------
  # * Frame Update (gameoption)
  #--------------------------------------------------------------------------
  def update_choiceoption
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Exit Choices
      update
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # Branch by command window cursor position
      case @command_window2.index
      when 0  # Windowskin Style1
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Set Windowskin
        # 001-Blue01
      when 1  # Windowskin Style2
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Set Windowskin
        # windowskin2
      end
      return
    end
  end
end

Thanks in advance for any help i get. Thanks!!
 
Glad to see someone learning to script. So I am always willing to lend a hand for that.

1)
Code:
    update_gameoption
    return
  end

I noticed this here. Though its not an error, there is no need for the return.

2) Your scene structure is just off. Don't worry, I did nearly the same thing as you when I started.  Create all your windows under main, just turn some of their visibility/activeness off like so:
Code:
#==============================================================================
# ** Scene_GameOption
#------------------------------------------------------------------------------
#  This class performs game options screen processing.
#==============================================================================

class Scene_GameOption
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Make Options Window
    @game_window = Window_GameOption.new
    # Make Options for game
    s1 = 'Windowskin Style'
    @main_command = Window_Command.new(160, [s1])
    @main_command.x = 20
    @main_command.y = 40
    # Make new choices
    s1 = 'Style 1'
    s2 = 'Style 2'
    @skin_command = Window_Command.new(160, [s1, s2])
    @skin_command.x = 600
    @skin_command.y = 440
    @skin_command.active = false
    @skin_command.visble = false
    # Execute transition
    Graphics.transition
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of windows
    @game_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update windows
    @game_window.update
    @main_command.update
    @skin_command.update
    # If Main Command Active
    if @main_command.active
      update_maincommand
      return
    end
    # If Skin Command Active
    if @skin_command.active
      update_skincommand
      return
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update (maincommand)
  #--------------------------------------------------------------------------
  def update_gameoption
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Switch to menu screen
      $scene = Scene_Menu.new
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # Play decision SE
      $game_system.se_play($data_system.decision_se)
      # Branch by command window cursor position
      case @main_command.index
      when 0  # Windowskin
        # Turn main command off
        @main_command.active = false
        @main_command.visible = false
        # Turn skin command on
        @skin_command.active = true
        @skin_command.visible = true
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update (skillcommand)
  #--------------------------------------------------------------------------
  def update_choiceoption
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Turn main command on
      @main_command.active = true
      @main_command.visible = true
      # Turn skin command off
      @skin_command.active = false
      @skin_command.visible = false
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # Play decision SE
      $game_system.se_play($data_system.decision_se)
      # Branch by command window cursor position
      case @command_window2.index
      when 0  # Windowskin Style 1
        # Set Windowskin
        
      when 1  # Windowskin Style 2
        # Set Windowskin
        
      end
      return
    end
  end
end

Some things to note:
~ You create all windows in main 99% of the time (the only other way is more complex, but it requires != nil checks in the update method and more method branches. This is a higher skill level. If you are interested in seeing what it looks like, look in Scene_Battle under update_phase3)
~ .visible is seeing the window, .active is an instance variable for selectable windows that is used mostly in #update in Window_Selectable to handling the updating of the index. If .active == false, the window doesn't update. There are other uses but this is the main.)
~ Never have more than one method with the same name in a class. The first one will be overwrote. When it comes to see, you follow this basic format:
main
update
  - update branch 1
  - update branch 2
  - ...

The idea is to process the least amount of data at once by only looking at which object is active. With menu scenes like this, you keep to usually just 1 command window active.
~ Remember, words and such must have a " or ' mark before and after them. Otherwise, ruby looks for variables and you will come up with an undefined error.
~ Always tab your script correctly! This avoids them silly syntax errors by having too many or not enough end marks. 99%, the script editor takes care of the tabbing as you push enter. Just be careful. Most blue words in the editor means the next line is tabbed and will not tab back until you use an end keyword. You will learn most of this after a few scripts.
~ Always call your update methods. In your script, @main_command could have updated under def update_maincommand and the same for update_skincommand, but because its a relatively small scene, its ok this time to just update them all every frame.
~ Label your methods the best you can. Again, you will learn a practice for this as you create more scripts.


If you need help more help, just ask.  :thumb:
 
Thanks!! This really helps. I'll let you know if I have any more problems...(Which is very likely  :grin:)  :thumb:

EDIT: ok, one small question...how do you change the windowskin with a script???
 
sweet..thanks...I knew it was going to be simple..]

EDIT: Should've tested before I posted...It doesn't work right... I get an undefined method when I try to use it. Why does it do this?? Thanks
 

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