[RMXP][RESOLVED]Help with menu script

Started by jayje, July 07, 2018, 10:55:08 pm

Previous topic - Next topic

jayje

July 07, 2018, 10:55:08 pm Last Edit: July 08, 2018, 09:02:48 am by jayje
 :^_^\': Hey gang!  :^_^\':

I have this custom menu that shows the Monstructs that you 've created (kinda like a bestiary)
Spoiler: ShowHide


Problem is I this image in the background:
Spoiler: ShowHide


I've been trying to figure out how to do this and i've been failing miserably.

here is the script:
Spoiler: ShowHide
#---------------------------------------------------------
# Ignore this part. It simply tells RMXP to
# stop looping the menu index from bottom to top.
#---------------------------------------------------------
class Window_Command < Window_Selectable
  def set_commands(list)
    if (!list.nil?)
      @commands = list
    end
    refresh
  end
 
  def update
    old_index = (self.index)
    super
    # stop looping
    if (old_index > (self.index) +1) or (old_index < (self.index) -1)
      self.index = old_index
    end
  end
 
end
#---------------------------------------------------------------------#
#     Scene_Monstruct_Menu                                            #
# Changes: menu scrolls to show all the options.                      #
#          new class: Window_Monstruct                                #
#                                                                     #
#  Notes:                                                             #
#  - The tiny arrows are made by me.                                  #
#     copy them into your game's Pictre folder.                       #
#  - you need to edit the methods make_list & get_picture             #
#                                                                     #
#---------------------------------------------------------------------#
class Scene_Monstruct_Menu
 
  def main
    # this line shows the map behind the menu
    # if you erase it, the screen will be black around it.
    @spriteset = Spriteset_Map.new   
    # list of menu commands (monstruct names)
    # scroll down to see what 'make_list' does.
    @manual_background
    @commands = make_list
    # only show 6 names at a time:
    @commands_max = [10,@commands.size].min
    # use the first 6 names as a menu.
    @list = Array.new
    for i in 0...@commands_max
      @list.push(@commands[i])
    end
    # these two variables are used to get the 'real' index in the list.
    # normally, it's simply menu.index but since we're scrolling
    # we must calculate it.
    @list_start = 0
    @start_from = 0
    @end_in = @commands_max
    # command_window is our menu
    @command_window = Window_Command.new(160, @list)
    @command_window.x = 480
    @command_window.index = 0
    @command_window.opacity = 0
    @command_window.back_opacity = 0
    # in rmxp you must draw a picture inside a window
    # I made a new window class for it.
    @pic_window = Window_Monstruct.new
    @pic_window.opacity = 0
    @pic_window.back_opacity = 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
    @pic_window.dispose
    @spriteset.dispose if !@spriteset.nil?       
    if (!@down.nil?) : @down.dispose end
    if (!@up.nil?)  : @up.dispose end
  end
 
  def update
    # Update windows
    @command_window.update
    # If command window is active: call update_command
    if @command_window.active
      update_command
      return
    end
  end
 
  #-------------------------------------------------------
  # edit below to create the list the way you want it.
  #-------------------------------------------------------
  def make_list
    list = []
    switches = []
    # push all names into the array
    for i in  1401..1556
      switches.push(i)
    end

    for id in switches
      if $game_switches[id]
        k = id-1400
        name = $data_enemies[k].name
        list.push(name)
      end
    end
    #while list.size < 6
      #list.push 'Empty'
    #end
   
    return list
  end
 
  def update_command
    # If B button was pressed
    if Input.trigger?(Input::B)
      # delete the arrows
      if (!@down.nil?): @down.dispose end
      if (!@up.nil?): @up.dispose end
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      $game_switches[974] = true
      # Switch to map screen
      $scene = Scene_Map.new
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # delete the arrows
      if (@down): @down.dispose end
      if (@up): @up.dispose end
      # Play decision SE
      $game_system.se_play($data_system.decision_se)
      # get menu index
      @list_index = @command_window.index + @start_from
      # get the name of the monstruct we're pointing at
      monstruct = @commands[@list_index]
      # use the new class to show the picture
      @pic_window.draw(monstruct)
      return
    end
    # show an arrow up/ down if there are more options
    update_scroll_arrow
    # making the menu scroll up/down
    index = @command_window.index
    if (Input.trigger?(Input::DOWN))&&(index ==@commands_max-1)
      scroll_down
      return
    end
    if (Input.trigger?(Input::UP))&&(index ==0)
      scroll_up
    end
  end
 
  def scroll_down
    index_fix = 0
    @start_from = @list_start + @commands_max
    @end_in = @start_from + @commands_max
    if (@end_in > @commands.size)
      index_fix = @end_in - @commands.size
      @end_in = @commands.size
      @start_from = @end_in - @commands_max
    end
    @list = Array.new
    for i in (@start_from)...(@end_in)
      @list.push(@commands[i])
    end
    @list_start += @commands_max -1
    @list_start -= index_fix
    @command_window.dispose
    @command_window = Window_Command.new(160, @list)
    @command_window.x = 480
    @command_window.opacity = 0
    @command_window.back_opacity = 0               # new
    @command_window.active = true
    @command_window.index = @commands_max-1
  end
 
  def scroll_up
    index_fix = 0
    @start_from = @list_start - @commands_max-1
    @end_in = @start_from + @commands_max
    if (@start_from<0)
      index_fix = @start_from
      @start_from = 0
      @end_in = @commands_max
    end
    if (@end_in > @commands.size)
      index_fix = @end_in - @commands.size
      @end_in = @commands.size
      @start_from = @end_in - @commands_max
    end
    @list = Array.new
    for i in @start_from...@end_in
      @list.push(@commands[i])
    end
    @list_start = @list_start - @commands_max +1
    @list_start -= index_fix
    @command_window.dispose
    @command_window = Window_Command.new(160, @list)
    @command_window.x = 480
    @command_window.opacity = 0
    @command_window.back_opacity = 0               # new
    @command_window.active = true
    @command_window.index = 0
  end
 
  def show_down_arrow
    @down = Sprite.new
    @down.bitmap = RPG::Cache.picture("arrow_down.png")
    @down.z = 101
    @down.x = 60
    @down.y = 6*32+16
  end

  def show_up_arrow
    @up = Sprite.new
    @up.bitmap = RPG::Cache.picture("arrow_up.png")
    @up.z = 101
    @up.x = 60
    @up.y = 10
  end

  def update_scroll_arrow
    # show an arrow up/ down if there are more options
    index = @command_window.index +@start_from
    commands = @commands.size
    item_max = index + @commands_max
    if (commands >item_max)
     show_down_arrow
    else
      if (@down): @down.dispose end
    end
    item_min= index-@commands_max
    if  (index>@commands_max-1)     
      show_up_arrow
    else
      if (@up): @up.dispose end
    end
  end
 
end

#-------------------------------------------------------
#  This class shows a 400x400 window
#      with a monster picture in it.
#-------------------------------------------------------
class Window_Monstruct < Window_Base
 
  def initialize
    super(21, 16, 640, 480)
    self.contents = Bitmap.new(width - 32, height - 32)
  end
 
 
  #---------------------------------------------
  # edit below to set which picture is shown
  #---------------------------------------------
  def get_picture(name)
    hue = 0
    # name is the name of the monstruct
    # use it to decide which picture to use :
    # case (name)
    # when 'badger'
    #   filename = 'badger_battler.png'
    # when 'unicorn'
    #   filename = 'unicorn2.png'
    # end
    filename = name + " Page.png"
    return RPG::Cache.load_bitmap("Graphics/Manual/",filename, hue)
  end
 
  def draw(name)
    self.contents.clear
    bitmap = get_picture(name)
    cw = bitmap.width
    ch = bitmap.height
    x = 32
    y = 32
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x - 46 / 2, y - 70 / 2, bitmap, src_rect)#x - 46 / 2, y - 70
  end 
end


Any help will def be appreciated. :haha:

EDIT:
"code" tags are much easier to read code from than "quote" tags  :)  - Zer0


G_G

July 07, 2018, 11:08:18 pm #1 Last Edit: July 07, 2018, 11:13:21 pm by G_G
Did you know you don't actually need to create a window to show a picture?

Code: ruby
# Put this above the loop in your Menu.
@picture = Sprite.new
@picture.bitmap = RPG::Cache.picture("picture name here")
# Then just make sure to dispose it like everything else
@picture.dispose


EDIT: Just poked through your code too, one thing I would suggest since you only want to show 6 commands at a time, do this to your command window. It should take care of the rest.

@command_window.height = 32 * 6


Then you don't have to calculate your index.

jayje

you know i tried to find a way to do that lol :^_^\':. also did you mean just above "# Main loop"?


G_G


jayje

July 07, 2018, 11:58:37 pm #4 Last Edit: July 08, 2018, 12:20:15 am by jayje
ok. and where would i put this?

Quote@command_window.height = 32 * 6


also i got this error:

"Script 'Window_Command' line 18: RGSSError occurred. failed to create bitmap"

here is the updated script with your edits:

Spoiler: ShowHide
#---------------------------------------------------------
# Ignore this part. It simply tells RMXP to
# stop looping the menu index from bottom to top.
#---------------------------------------------------------
class Window_Command < Window_Selectable
  def set_commands(list)
    if (!list.nil?)
      @commands = list
    end
    refresh
  end
 
  def update
    old_index = (self.index)
    super
    # stop looping
    if (old_index > (self.index) +1) or (old_index < (self.index) -1)
      self.index = old_index
    end
  end
 
end
#---------------------------------------------------------------------#
#     Scene_Monstruct_Menu                                            #
# Changes: menu scrolls to show all the options.                      #
#          new class: Window_Monstruct                                #
#                                                                     #
#  Notes:                                                             #
#  - The tiny arrows are made by me.                                  #
#     copy them into your game's Pictre folder.                       #
#  - you need to edit the methods make_list & get_picture             #
#                                                                     #
#---------------------------------------------------------------------#
class Scene_Monstruct_Menu
 
  def main
    # this line shows the map behind the menu
    # if you erase it, the screen will be black around it.
    @spriteset = Spriteset_Map.new   
    # list of menu commands (monstruct names)
    # scroll down to see what 'make_list' does.
    @commands = make_list
    # only show 6 names at a time:
    @commands_max = [6,@commands.size].min
    # use the first 6 names as a menu.
    @list = Array.new
    for i in 0...@commands_max
      @list.push(@commands[i])
    end
    # these two variables are used to get the 'real' index in the list.
    # normally, it's simply menu.index but since we're scrolling
    # we must calculate it.
    @list_start = 0
    @start_from = 0
    @end_in = @commands_max
    # command_window is our menu
    @command_window = Window_Command.new(160, @list)
    @command_window.x = 480
    @command_window.index = 0
    @command_window.opacity = 0
    @command_window.back_opacity = 0
    # in rmxp you must draw a picture inside a window
    # I made a new window class for it.
    @pic_window = Window_Monstruct.new
    @pic_window.opacity = 0
    @pic_window.back_opacity = 0
    # Execute transition
    Graphics.transition
    @picture = Sprite.new #edits 7/7/#
    @picture.bitmap = RPG::Cache.picture("ManualBackground.png")#edits 7/7/#
    # 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
    @pic_window.dispose
    @spriteset.dispose if !@spriteset.nil?       
    if (!@down.nil?) : @down.dispose end
    if (!@up.nil?)  : @up.dispose end
  end
 
  def update
    # Update windows
    @command_window.update
    # If command window is active: call update_command
    if @command_window.active
      update_command
      return
    end
  end
 
  #-------------------------------------------------------
  # edit below to create the list the way you want it.
  #-------------------------------------------------------
  def make_list
    list = []
    switches = []
    # push all names into the array
    for i in  1401..1556
      switches.push(i)
    end

    for id in switches
      if $game_switches[id]
        k = id-1400
        name = $data_enemies[k].name
        list.push(name)
      end
    end
    #while list.size < 6
      #list.push 'Empty'
    #end
   
    return list
  end
 
  def update_command
    # If B button was pressed
    if Input.trigger?(Input::B)
      # delete the arrows
      if (!@down.nil?): @down.dispose end
      if (!@up.nil?): @up.dispose end
      @picture.dispose #edits 7/7/#
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      $game_switches[974] = true
      # Switch to map screen
      $scene = Scene_Map.new
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # delete the arrows
      if (@down): @down.dispose end
      if (@up): @up.dispose end
      # Play decision SE
      $game_system.se_play($data_system.decision_se)
      # get menu index
      @list_index = @command_window.index + @start_from
      # get the name of the monstruct we're pointing at
      monstruct = @commands[@list_index]
      # use the new class to show the picture
      @pic_window.draw(monstruct)
      return
    end
    # show an arrow up/ down if there are more options
    update_scroll_arrow
    # making the menu scroll up/down
    index = @command_window.index
    if (Input.trigger?(Input::DOWN))&&(index ==@commands_max-1)
      scroll_down
      return
    end
    if (Input.trigger?(Input::UP))&&(index ==0)
      scroll_up
    end
  end
 
  def scroll_down
    index_fix = 0
    @start_from = @list_start + @commands_max
    @end_in = @start_from + @commands_max
    if (@end_in > @commands.size)
      index_fix = @end_in - @commands.size
      @end_in = @commands.size
      @start_from = @end_in - @commands_max
    end
    @list = Array.new
    for i in (@start_from)...(@end_in)
      @list.push(@commands[i])
    end
    @list_start += @commands_max -1
    @list_start -= index_fix
    @command_window.dispose
    @command_window = Window_Command.new(160, @list)
    @command_window.x = 480
    @command_window.opacity = 0
    @command_window.back_opacity = 0               # new
    @command_window.active = true
    @command_window.index = @commands_max-1
  end
 
  def scroll_up
    index_fix = 0
    @start_from = @list_start - @commands_max-1
    @end_in = @start_from + @commands_max
    if (@start_from<0)
      index_fix = @start_from
      @start_from = 0
      @end_in = @commands_max
    end
    if (@end_in > @commands.size)
      index_fix = @end_in - @commands.size
      @end_in = @commands.size
      @start_from = @end_in - @commands_max
    end
    @list = Array.new
    for i in @start_from...@end_in
      @list.push(@commands[i])
    end
    @list_start = @list_start - @commands_max +1
    @list_start -= index_fix
    @command_window.dispose
    @command_window = Window_Command.new(160, @list)
    @command_window.x = 480
    @command_window.opacity = 0
    @command_window.back_opacity = 0               # new
    @command_window.active = true
    @command_window.index = 0
  end
 
  def show_down_arrow
    @down = Sprite.new
    @down.bitmap = RPG::Cache.picture("arrow_down.png")
    @down.z = 101
    @down.x = 60
    @down.y = 6*32+16
  end

  def show_up_arrow
    @up = Sprite.new
    @up.bitmap = RPG::Cache.picture("arrow_up.png")
    @up.z = 101
    @up.x = 60
    @up.y = 10
  end

  def update_scroll_arrow
    # show an arrow up/ down if there are more options
    index = @command_window.index +@start_from
    commands = @commands.size
    item_max = index + @commands_max
    if (commands >item_max)
     show_down_arrow
    else
      if (@down): @down.dispose end
    end
    item_min= index-@commands_max
    if  (index>@commands_max-1)     
      show_up_arrow
    else
      if (@up): @up.dispose end
    end
  end
 
end

#-------------------------------------------------------
#  This class shows a 400x400 window
#      with a monster picture in it.
#-------------------------------------------------------
class Window_Monstruct < Window_Base
 
  def initialize
    super(21, 16, 640, 480)
    self.contents = Bitmap.new(width - 32, height - 32)
  end
 
 
  #---------------------------------------------
  # edit below to set which picture is shown
  #---------------------------------------------
  def get_picture(name)
    hue = 0
    # name is the name of the monstruct
    # use it to decide which picture to use :
    # case (name)
    # when 'badger'
    #   filename = 'badger_battler.png'
    # when 'unicorn'
    #   filename = 'unicorn2.png'
    # end
    filename = name + " Page.png"
    return RPG::Cache.load_bitmap("Graphics/Manual/",filename, hue)
  end
 
  def draw(name)
    self.contents.clear
    bitmap = get_picture(name)
    cw = bitmap.width
    ch = bitmap.height
    x = 32
    y = 32
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x - 46 / 2, y - 70 / 2, bitmap, src_rect)#x - 46 / 2, y - 70
  end
 
end


G_G

July 08, 2018, 01:08:40 am #5 Last Edit: July 08, 2018, 01:14:49 am by G_G
That is an issue with your Window_Command, not the sprite. Remove all of your Window_Command code that you put in. Remove all the index calculating. Its a recipe for disaster. Also you put the @command_window.height line within the same group of code where you create your command window.

EDIT:

I cleaned up your script, see if this solves your issues.

Spoiler: ShowHide
#---------------------------------------------------------------------#
#     Scene_Monstruct_Menu                                            #
# Changes: menu scrolls to show all the options.                      #
#          new class: Window_Monstruct                                #
#                                                                     #
#  Notes:                                                             #
#  - The tiny arrows are made by me.                                  #
#     copy them into your game's Pictre folder.                       #
#  - you need to edit the methods make_list & get_picture             #
#                                                                     #
#---------------------------------------------------------------------#
class Scene_Monstruct_Menu
 
  def main
    # this line shows the map behind the menu
    # if you erase it, the screen will be black around it.
    @spriteset = Spriteset_Map.new   
    # list of menu commands (monstruct names)
    # scroll down to see what 'make_list' does.
    @commands = make_list
    # command_window is our menu
    @command_window = Window_Command.new(160, @commands)
    @command_window.x = 480
    @command_window.height = 6 * 32
    @command_window.index = 0
    @command_window.opacity = 0
    @command_window.back_opacity = 0
    # in rmxp you must draw a picture inside a window
    # I made a new window class for it.
    @pic_window = Window_Monstruct.new
    @pic_window.opacity = 0
    @pic_window.back_opacity = 0
    @picture = Sprite.new #edits 7/7/#
    @picture.bitmap = RPG::Cache.picture("ManualBackground.png")#edits 7/7/#
    # 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
    @picture.dispose
    @command_window.dispose
    @pic_window.dispose
    @spriteset.dispose if !@spriteset.nil?       
    if (!@down.nil?) : @down.dispose end
    if (!@up.nil?)  : @up.dispose end
  end
 
  def update
    # Update windows
    # If command window is active: call update_command
    if @command_window.active
      update_command
      return
    end
  end
 
  #-------------------------------------------------------
  # edit below to create the list the way you want it.
  #-------------------------------------------------------
  def make_list
    list = []
    for i in  1401..1556
      if $game_switches[i]
        enemy_id = i - 1400
        list.push($data_enemies[enemy_id].name)
      end
    end
    return list
  end
 
  def update_command
    @command_window.update
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      $game_switches[974] = true
      # Switch to map screen
      $scene = Scene_Map.new
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # delete the arrows
      if (@down): @down.dispose end
      if (@up): @up.dispose end
      # Play decision SE
      $game_system.se_play($data_system.decision_se)
      # get the name of the monstruct we're pointing at
      monstruct = @commands[@command_window.index]
      # use the new class to show the picture
      @pic_window.draw(monstruct)
      return
    end
  end

 
end

#-------------------------------------------------------
#  This class shows a 400x400 window
#      with a monster picture in it.
#-------------------------------------------------------
class Window_Monstruct < Window_Base
 
  def initialize
    super(21, 16, 640, 480)
    self.contents = Bitmap.new(width - 32, height - 32)
  end
 
 
  #---------------------------------------------
  # edit below to set which picture is shown
  #---------------------------------------------
  def get_picture(name)
    hue = 0
    # name is the name of the monstruct
    # use it to decide which picture to use :
    # case (name)
    # when 'badger'
    #   filename = 'badger_battler.png'
    # when 'unicorn'
    #   filename = 'unicorn2.png'
    # end
    filename = name + " Page.png"
    return RPG::Cache.load_bitmap("Graphics/Manual/",filename, hue)
  end
 
  def draw(name)
    self.contents.clear
    bitmap = get_picture(name)
    cw = bitmap.width
    ch = bitmap.height
    x = 32
    y = 32
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x - 46 / 2, y - 70 / 2, bitmap, src_rect)#x - 46 / 2, y - 70
  end
 
end

jayje

thanks. do you have a twitter or instagram handle? wanted to shout you out.