[RGSS3] Creating your own window handlers

This tutorial describes how you can create your own window handlers for any windows that you use for your scene. Some other environments call them "event listeners". For more information you can read the wiki article on the general concept.

Handlers allow the window to communicate with the scene. For example, when you're in the item menu and selecting an item to use, when you press the OK button, the item window will tell the scene that the OK button was triggered, and the scene calls the "on_item_ok" method accordingly.

But how does the window know that it has an OK handler?

And how does the window know what method to call?

This tutorial will answer the two questions, and you should be able to create your own custom handlers.

trans.gif
Assigning Handlers

Handlers are usually assigned to windows in the scene, using the `set_handler` method. This method is defined as follows in Window_Selectable:

#--------------------------------------------------------------------------# * Set Handler Corresponding to Operation# method : Method set as a handler (Method object)#--------------------------------------------------------------------------def set_handler(symbol, method) @handler[symbol] = methodendThe symbol is just any name you want to give for your handler.The method is the method that will be called when this handler is called.

Look at how Scene_Item creates the item window:

def create_item_window wy = @category_window.y + @category_window.height wh = Graphics.height - wy @item_window = Window_ItemList.new(0, wy, Graphics.width, wh) @item_window.viewport = @viewport @item_window.help_window = @help_window @item_window.set_handler🙂ok, method🙂on_item_ok)) @item_window.set_handler🙂cancel, method🙂on_item_cancel)) @category_window.item_window = @item_windowendThe important part is these two lines:
Code:
@item_window.set_handler(:ok, method(:on_item_ok))@item_window.set_handler(:cancel, method(:on_item_cancel))
The scene assigns two handlers to the item window, an "ok" handler, which will call the scene's "on_item_ok" method, as well as a "cancel" handler, which will call the "on_item_cancel" method. Both of these methods are defined in the scene.The way these handlers are called is done in the `process_handling` method in Window_Selectable:

def process_handling return unless open? && active return process_ok if ok_enabled? && Input.trigger?🙂C) return process_cancel if cancel_enabled? && Input.trigger?🙂 return process_pagedown if handle?🙂pagedown) && Input.trigger?🙂R) return process_pageup if handle?🙂pageup) && Input.trigger?🙂L)endYou can see that the selectable window checks for four types of handles, depending on which button is pressed. If you press the "C" button, it will call the method associated with the "ok" handler. If you press the "B" button, it will call the method that is associated with the "cancel" handler.Creating your own handler

What you have seen above is basically all there is to handlers: the scene assigns some handlers to the window, giving the handler a name and a callback method, and the window then checks whether any of the handlers are called and calls the appropriate method attached to the handler.

The steps for creating your handlers is summarized below.

  • Choose a name for your handler. Something intuitive would be nice.
  • Decide how you want this handler to be triggered. It can be a keypress, or any other boolean expression. For example, maybe you want a handler to be triggered every 5 seconds by checking some sort of timer.
  • Decide what methods will be triggered for each handler.
  • Call `set_handler` and assign a handler to your window, using the name and method that you picked above.
  • add the handling logic inside `process_handling` to your window according to what you have decided in step 2.
Following this procedure, you should be able to easily define custom handlers for your windows.Example

Here is a working example that adds custom handlers to the item window in the item scene that will be triggered whenever you press the left or right keys. I use the procedure described above.

1. Decide on handler names

Since the handlers will be triggered when I press the left or right keys, I will just call them `:left` and `:right`

2. Decide how the handlers are triggered

They will be triggered when I press the left or right keys. This can be accomplished using the following calls:

if Input.trigger?🙂LEFT)if Input.trigger?🙂RIGHT)3. Decide what methods will be triggeredFor demonstration purposes, I will simply print out "left" or "right" to the console, depending on which key I pressed.

def on_item_left p 'LEFT' @item_window.activateenddef on_item_right p 'RIGHT' @item_window.activateend4. Assign the handlers to the windowThis is straightforward enough at this point. Just calling the set_handler method and assigning the two handlers as required:

@item_window.set_handler🙂left, method🙂on_item_left))@item_window.set_handler🙂right, method🙂on_item_right))5. Add the handling logic to the windowThis is also pretty much just copying what you see from the default method

return call_handler🙂left) if handle?🙂left) && Input.trigger?🙂LEFT)return call_handler🙂right) if handle?🙂right) && Input.trigger?🙂RIGHT)It is not necessary to check whether the handlers are defined if you are using a custom window because chances are they will be defined. Window_Selectable is only written that way because you may have windows that do not assign all of those handlers.At this point, if you load up your game, add some items to your inventory, and then scroll through your item list, you should see the appropriate string printed out to the console if you press left or right.

Here is the full code:

Code:
class Scene_Item < Scene_ItemBase  alias :th_input_handler_create_item_window :create_item_window  def create_item_window    th_input_handler_create_item_window    @item_window.set_handler(:LEFT, method(:on_item_left))    @item_window.set_handler(:RIGHT, method(:on_item_right))  end  def on_item_left    p 'LEFT'    @item_window.activate  end  def on_item_right    p 'RIGHT'    @item_window.activate  endendclass Window_ItemList < Window_Selectable  alias :th_input_handler_process_handling :process_handling  def process_handling    return unless open? && active    th_input_handler_process_handling    return call_handler(:LEFT) if handle?(:LEFT) && Input.trigger?(:LEFT)    return call_handler(:RIGHT) if handle?(:RIGHT) && Input.trigger?(:RIGHT)  endend 
It's not a TRUE event listener (I think) because I can not attach/detach multiple methods to the same event but I get what you mean. Nice post.

Events are not supported in Ruby and creating them manually can be messy. I tried it a few times because I really really miss that and aliasing is not always that nice.
It's not a TRUE event listener (I think) because I can not attach/detach multiple methods to the same event but I get what you mean.
I am not sure what you mean can you explain in more detail?

You mean something like

set_handler🙂ok, method🙂something), method🙂something_else))?Granted, in terms of event listeners, Window_Selectable basically only supports one: onkeypress.
No I mean events like in C#. I'm so bad at explaining these things. Like so you can simply register methods to some class without changing any code in the original class that raises the events.

http://www.dotnetperls.com/event

I have seen multiple Ruby libraries for it but most of 'em are still a bit 'messy' or weird to use.

Code:
Scene_Item  Event On_Sell(price)  ...  if player sells item then call On_Sell(price)endmy_class  def initialize    Scene_Item.On_Sell.add(item_was_sold)  end  def item_was_sold(price)    p 'an item was sold'    p price  endend
Code:
my_class2def initialize  Scene_Item.On_Sell.add(item_was_sold)end  def item_was_sold(price)    p 'an item was sold #2'    p price  endend
So a single event listener simply has multiple methods attached to it, and when the event is fired, it calls all of those attached methods.


Unless I am not understanding it because the example it only demonstrates how to use C# events.


Anyways that behavior can be implemented easily by simply storing an array of methods for each handler like this


http://himeworks.wordpress.com/2013/07/14/multiple-handler-methods/


In our case we don't have an explicit event object, since there's only one event anyways and that's the one that's always going to be called.
Good post Tsukihime. I started doing this to pass messages between windows instead of passing window references. It makes encapsulation way easier for windows and looks much cleaner besides.
What I always kinda missed is for example:

def initialize  on_battle_start.add(foo1)  on_game_exit.add(foo2)enddef dispose  on_battle_start.remove(foo1)  on_game_exit.remove(foo2) # <release unmanaged resources here>enddef foo1  # do something fancy before the battle startsenddef foo2 disposeendInstead now I have to create aliases and having to look up what methods exactly start/end the behavior that I need to alias. But it get's worse when it's in the middle of some method. Because then I can't alias but then I have to overwritte it (possibly making my script uncompatible with other scripts). Plus if you create the above using aliases it will look so 'messy'.

Edit:

I created my own topic because I don't want to hijack this topic: http://forums.rpgmakerweb.com/index.php?/topic/15641-events-in-ruby/
Nice topic Tsukihime :3!  
 

Original thread: https://forums.rpgmakerweb.com/threads/rgss3-creating-your-own-window-handlers.15302/