Handling GUI Events from Simkin

Event handlers in Simkin are just regular Simkin functions that take one parameter. A GUI::EventArgs struct. The functions you register as event handlers must already be registered with the system (i.e. defined in a message, inventory or conversation script file), otherwise you will need to load some script files before doing any Simkin handled events. To do this you can use the GUI::System::executeScriptFile function in a Simkin script like so:

GUI.System.executeScriptFile("eventHandlers.s");

Calling this function will execute the specified Simkin script file. This means that functions declared within this file will be accessible to the GUI system.

Now let's take a look at that Simkin event handler:

simkinbtn_clicked[(e)
{
we = GUI.toWindowEventArgs(e);
we.window.setText("handled from Simkin");
}]

Here we make sure the button text is handled from Simkin when the button is clicked. We use a utility function

GUI.toWindowEventArgs(e);

which converts the GUI::EventArgs parameter to a GUI::WindowEventArgs object. Converters for the other EventArgs classes are also available.

Registering Simkin Event Handlers from Scripts

Let's assume we have created a GUI::PushButton named simkin_powered_button. We can access this button in a script with the help of the GUI::WindowManager.

pb = GUI.WindowManager.getWindow("simkin_powered_button");

Then we just need to use the GUI::Window::subscribeEvent function to connect the event handler to the desired event, in this example the Clicked event:

pb.subscribeEvent("Clicked", "simkinbtn_clicked");

or condensed in one line

GUI.WindowManager.getWindow("simkin_powered_button").subscribeEvent("Clicked", "simkinbtn_clicked");

Registering Simkin Event Handlers from XML Layouts

An even easier way to bind scripted event handlers to the windows in your XML layouts is to define the event handlers in the XML file itself.

Take a look a this example:

<?xml version="1.0"?>
<GUILayout>
<Window Type="RFLook/Button" Name="simkin_powered_button">
<Property Name="Width" Value="0.1" />
<Property Name="Height" Value="0.1" />
<Property Name="XPosition" Value="0.1" />
<Property Name="YPosition" Value="0.1" />
<Event Name="Clicked" Function="simkinbtn_clicked" />
</Window>
</GUILayout>

This simple layout creates a push button named simkin_powered_button and even binds the scripted event handler to it.