Let's say I have Window_Selectable with a bunch of entries in the list (the list isn't fixed, but changes based on certain actions during the game, I use an array to populate it), and I want to show different descriptions for each entries in a separate window (maybe a Window_Base) whenever the cursor is on one of the entries of the list, how would I do that? Specifically, I am having troubles figuring out how to know and store which entry is highlighted at any specific point in time, and how to update the window in a timely fashion.
[VX Ace/RGSS3] A question about menus and windows
RGSSx Script SupportCall the help_window method on your new window to set its help window to the help window of the scene, then in the update_help method of your window, call @help_window.set_item(@data[index]).
Thanks a lot! Can I ask you how I would add descriptions for my entries, though? Maybe I am just blind and missing something obvious, but I couldn't figure it out.
Also, this isn't really necessary, but I was wondering: how would you do the same thing if the help window didn't exist in RPG Maker?
Also, this isn't really necessary, but I was wondering: how would you do the same thing if the help window didn't exist in RPG Maker?
You would make your own window class (or any other type of class, really) to handle your own descriptions.
The default Window_Help class is so limited that it is not really something that I could recommend using to anyone.
It can only display text (with message codes), and that's it. Sure, you can add in your own methods there to allow other things, but why would you do that instead of making your own class to handle your own methods, right?
You can still use the update_help method of your selectable window to refresh your description window when needed, but instead of calling the default methods from the default help window, you call your own methods from your own help window class.
For example, you make your own window class. Add a refresh method for it (you can name the method however you want, I always use refresh or something similar for it), or if needed, add more refresh methods for different things.
Now you have your own window class which can display your own data.
Next, make your selectable window (I prefer Window_Command for these, since they are way more easy to handle than Window_Selectable and got tons of useful methods for managing the list already), add your commands for it from your own source. You wrote that you populate this using an array, so I guess, you already have a way to get the correct commands to show.
Now, the only thing left to do is to link the 2 windows before you can correctly update the help window.
You have 2 options:
So, now your windows are linked, you have access to the help window from your selectable window, everything is set to make your update method.
You can do this in the update_help method in your selectable window. Depending of your refresh method you made for the help window, you may need some extra steps added in in this method, but for the most time (if you made the refresh method right), the only thing you need to do is to call your refresh method on the help window from the update_help method using the current index of the selectable window (which is called @index), and that should be it. So, you will use the @index variable to determine which text to show in your help window.
For example:
This refresh method would always clear the help window when it is called, but will only draw something (or do the other things you added in it) if the argument it gets is not nil. This is to prevent your game to crash if it gets a non-existent index or it can also be used to manually clear the help window whenever you need it.
BUT!
You CAN use other things for your arguments if needed too, and for more complex data displays, it is usually needed. Will get back to this later...
For making your own database for these things you are trying to display (for the commands and for the help texts alike), I recommend you to use a module, and let a constant store your data (or more constants if needed). The most flexible data type for storing any kind of data is without a doubt a hash.
You can use any kind of keys in a hash for your values, you can use any kind of data for your values, and it is easily extensible anytime as well.
Obviously, you should not use a hash for simple data, like a true/false value only, unless you want to group them together into one setting to hint that they are connected.
Here is a very basic example module for an example database, along with the full code of a working scene with a custom command window and a help window:
You see, it's not complicated, you just have to make your database settings organized and clean, and of course, easily understandable. If you manage to do that, displaying any data will become way too easy.
Note that modules got no such things as $game_switches or $game_variables, for example, because they are evaluated before classes, therefore you will have to use constants in your settings. But this is not a problem, because you can turn these constants into conditions with simple coding, which will enable you to link your settings to valid variables used in the game (see the :switch setting I made, for example).
Coffee break over, good luck with your coding!
The default Window_Help class is so limited that it is not really something that I could recommend using to anyone.
It can only display text (with message codes), and that's it. Sure, you can add in your own methods there to allow other things, but why would you do that instead of making your own class to handle your own methods, right?
You can still use the update_help method of your selectable window to refresh your description window when needed, but instead of calling the default methods from the default help window, you call your own methods from your own help window class.
For example, you make your own window class. Add a refresh method for it (you can name the method however you want, I always use refresh or something similar for it), or if needed, add more refresh methods for different things.
Now you have your own window class which can display your own data.
Next, make your selectable window (I prefer Window_Command for these, since they are way more easy to handle than Window_Selectable and got tons of useful methods for managing the list already), add your commands for it from your own source. You wrote that you populate this using an array, so I guess, you already have a way to get the correct commands to show.
Now, the only thing left to do is to link the 2 windows before you can correctly update the help window.
You have 2 options:
- Make the selectable window in the scene class and assign your help window for it using (the names are examples, obviously): @sel_win.help_window = @my_help
- Make the selectable window in the scene class, and make your help window in the selectable window itself. Note that if you do this, you will also need to dispose the help window manually from the selectable window when you exit the scene.
So, now your windows are linked, you have access to the help window from your selectable window, everything is set to make your update method.
You can do this in the update_help method in your selectable window. Depending of your refresh method you made for the help window, you may need some extra steps added in in this method, but for the most time (if you made the refresh method right), the only thing you need to do is to call your refresh method on the help window from the update_help method using the current index of the selectable window (which is called @index), and that should be it. So, you will use the @index variable to determine which text to show in your help window.
For example:
Code:
def refresh(index=nil)
contents.clear
return unless index
# Do whatever you want here to display your data
end
BUT!
You CAN use other things for your arguments if needed too, and for more complex data displays, it is usually needed. Will get back to this later...
For making your own database for these things you are trying to display (for the commands and for the help texts alike), I recommend you to use a module, and let a constant store your data (or more constants if needed). The most flexible data type for storing any kind of data is without a doubt a hash.
You can use any kind of keys in a hash for your values, you can use any kind of data for your values, and it is easily extensible anytime as well.
Obviously, you should not use a hash for simple data, like a true/false value only, unless you want to group them together into one setting to hint that they are connected.
Here is a very basic example module for an example database, along with the full code of a working scene with a custom command window and a help window:
Code:
module ExampleName
# You can comment out the :switch settings to enable the commands without
# any switch requirements.
ExampleData = {
:cmd1 => { # Data for command 1
:name => "Command 1",
:desc => "Description 1",
:img => "img1",
:icon => 45,
:switch => 12, # Will only show when Switch 12 in ON
# ... You can add as many data here as you need.
},
:cmd2 => { # Data for command 2
:name => "Command 2",
:desc => "Description 2",
:img => "img2",
:icon => 46,
:switch => 13, # Will only show when Switch 13 in ON
# ... You can add as many data here as you need.
},
:cmd3 => { # Data for command 3
:name => "Command 3",
:desc => "Description 3",
:img => "img3",
:icon => 47,
:switch => 14, # Will only show when Switch 14 in ON
# ... You can add as many data here as you need.
},
# ... You can add as many as you need.
}
end
class ExampleHelp < Window_Base
def initialize(x,y,w,h)
super(x,y,w,h)
end
# The refresh method.
# Draws an image, and right next to it, draws some description text as well.
# Use nil as an argument to clear the help window.
def refresh(data=nil)
contents.clear
return unless data
bmp = Cache.picture(data[:img])
draw_img(bmp)
draw_text_ex(bmp.width+4,0,data[:desc])
end
# Draws an image
def draw_img(bmp)
rct = Rect.new(0,0,bmp.width,bmp.height)
contents.blt(0,0,bmp,rct,255)
end
end
class ExampleCmd < Window_Command
def initialize(x,y)
super(x,y)
end
def window_width
return 200
end
def window_height
return Graphics.width - 96
end
# Makes the command list using the example data from the example module.
def make_command_list
ExampleName::ExampleData.each do |key,data|
next if data[:switch] && !$game_switches[data[:switch]]
add_command(data[:name],:ok,true,data)
end
end
def draw_item(index)
rct = item_rect(index)
data = @list[index][:ext] # This gets the correct ext data of the command
draw_icon(data[:icon],rct.x,rct.y)
# We use draw_text_ex instead of draw_text, cuz why not? :D
# This will let you use message codes in your :name settings.
draw_text_ex(rct.x+26,rct.y,data[:name])
end
# This will update the help window.
# Note that we use the current_ext data, which is always the 4th argument
# in the 'add_command' method, and it can be set to anything you want.
# The ext data is set to the command's data from the example data we made
# in the example module.
# This lets you connect any kind of data with any command, and this is the
# reason I prefer to use Window_Command intead of Window_Selectable, it is
# ready to make any kind of list.
# Note that if the current_ext data is nil, it will just clear the help
# window, and will not do anything else.
def update_help
@help_window.refresh(current_ext)
end
end
class ExampleScene < Scene_Base
def start
super
init_help_win
init_cmd_win
end
# The help window must be made before it could be linked to the command
# window, so we make that one first.
def init_help_win
@helpy = ExampleHelp.new(0,0,Graphics.width,96)
end
# You link the help window with the command window here.
def init_cmd_win
@cmds = ExampleCmd.new(0,96)
@cmds.help_window = @helpy # <-- With this line.
@cmds.set_handler(:ok, method(:on_cmd_ok))
@cmds.set_handler(:cancel, method(:on_cmd_cancel))
end
def on_cmd_ok
# Example line, will not do anything, just re-activates the command window.
@cmds.activate
# Do something here if the command is selected if you need to
end
def on_cmd_cancel
return_scene
# Do whatever you need to do when the cancel button is pressed on a command
# here. We just exit the scene with the example line.
end
end
Note that modules got no such things as $game_switches or $game_variables, for example, because they are evaluated before classes, therefore you will have to use constants in your settings. But this is not a problem, because you can turn these constants into conditions with simple coding, which will enable you to link your settings to valid variables used in the game (see the :switch setting I made, for example).
Coffee break over, good luck with your coding!
Thanks a lot, this was very interesting. When I do things my way, I still end up using stupid systems which are long detours, far from the best approach, but I am glad I understood the basics. 🙂
Original thread: https://forums.rpgmakerweb.com/threads/vx-ace-rgss3-a-question-about-menus-and-windows.77215/