Manual:Event Engine

From Mudlet
Jump to navigation Jump to search

Event System

Events in Mudlet allow a paradigm of system-building that is easy to maintain (because if you’d like to restructure something, you’d have to do less work), enables interoperability (making a collection of scripts that work with each other is easier) and enables an event-based way of programming.

The essentials of it are as such: you use Scripts to define which events should a function to listen to, and when the event is raised, the said function(s) will be called. Events can also have function parameters with them, which’ll be passed onto the receiving functions.

Registering an event handler via UI

Registering an event handler means that you’ll be telling Mudlet what function should it call for you when an event is raised, so it’s a two step process - you need to tell it both what function you’d like to be called, and on what event should it be called.

To tell it what function should be called, create a new script, and give the script the name of the function you’d like to be called. This is the only time where an items name matters in Mudlet. You can define the function right inside the script as well, if you’d like.

Next, we tell it what event or events should this function be called on - you can add multiple ones. To do that, enter the events name in the Add User Defined Event Handler: field, press enter, and it’ll go into the list - and that is all.

Registering an event from a script

You can also register your event with the registerAnonymousEventHandler(event name, function name) function inside your scripts:

<lua> -- example taken from the God Wars 2 (http://godwars2.org) Mudlet UI - forces the window to keep to a certain size function keepStaticSize()

 setMainWindowSize(1280,720)

end -- keepStaticSize

registerAnonymousEventHandler("sysWindowResizeEvent", "keepStaticSize") </lua>

Note Note: Mudlet also uses the event system in-game protocols (like ATCP, GMCP and others).

Note Note: Event handler functions can now also be in namespaces (tables).

Raising an event

To raise an event, you’d use the raiseEvent() function:

<lua>raiseEvent(name, [arguments...])</lua>

It takes an event name as the first argument, and then any amount of arguments after it which will be passed onto the receiving functions.

Mini-tutorial

As an example, our prompt trigger could raise an onPrompt event if you want to attach 2 functions to it. In your prompt trigger, all you’d need to do is raiseEvent("onPrompt"). Now we go about creating functions that attach to the event - lets say the first one is check_health_stuff() and the other is check_salve_stuff(). We would like these to be executed when the event is raised. So create a script and give it a name of check_health_stuff. In the Add user defined event handler, type onPrompt, and press enter to add it to the list. In the script box, create:

<lua> function check_health_stuff()

 echo("I work!\n")

end </lua>

When the onPrompt event comes along, that script catches it, and runs check_health_stuff() for you.

Link title

Mudlet-raised events

Mudlet itself also creates events for your scripts to hook on. The following events are generated currently:

mapOpenEvent

Raised when the mapper is opened - either the floating dock or the in-Mudlet widget.

sysWindowResizeEvent

Raised when the main window is resized, with the new height and width coordinates passed to the event. A common usecase for this event is to move/resize your UI elements according to the new dimensions. Example

This sample code will echo whenever a resize happened with the new dimensions:

<lua> function resizeEvent( event, x, y )

 echo("RESIZE EVENT: event="..event.." x="..x.." y="..y.."\n")

end </lua>

sysWindowMousePressEvent

Raised when a mouse button is pressed down anywhere on the main window (note that a click is composed of a mouse press and mouse release). The button number and the x,y coordinates of the click are reported. Example

<lua> function onClickHandler( event, button, x, y )

 echo("CLICK event:"..event.." button="..button.." x="..x.." y="..y.."\n")

end </lua>

sysWindowMouseReleaseEvent

Raised when a mouse button is released after being pressed down anywhere on the main window (note that a click is composed of a mouse press and mouse release). See sysWindowMousePressEvent for example use.

sysLoadEvent

Raised when Mudlet is loading the profile. Note that when it does so, it also compiles and runs all scripts - which could be a good idea to initialize everything at, but beware - scripts are also run when saved. Hence, hooking only on the sysLoadEvent would prevent multiple re-loads as you’re editing the script.

sysExitEvent

Raised when Mudlet is shutting down the profile - a good event to hook onto for saving all of your data.

sysDownloadDone

Raised when Mudlet is finished downloading a file successfully - the location of the downloaded file is passed as a second argument.

Example - put it into a new script and save it to run:

<lua> -- create a function to parse the downloaded webpage and display a result function downloaded_file(_, filename)

 -- is the file that downloaded ours?
 if not filename:find("achaea-who-count.html", 1, true) then return end
 -- read the contents of the webpage in
 local f, s, webpage = io.open(filename)
 if f then webpage = f:read("*a"); io.close(f) end
 -- delete the file on disk, don't clutter
 os.remove(filename)
 -- parse our downloaded file for the player count
 local pc = webpage:match(Total: (%d+) players online)
 display("Achaea has "..tostring(pc).." players on right now.")

end

-- register our function to run on the event that something was downloaded registerAnonymousEventHandler("sysDownloadDone", "downloaded_file")

-- download a list of fake users for a demo downloadFile(getMudletHomeDir().."/achaea-who-count.html", "https://www.achaea.com/game/who") </lua>

You should see a result like this:

DownloadFile demo.png

sysDownloadError

Raised when downloading a file failed - the second argument contains the error message. Starting with Mudlet 2.0-test5+, it specifies the original URL that was going to be downloaded.

sysIrcMessage

Raised when you see or receive an IRC message. The speakers name, channel and their message will follow as arguments.

<lua> function onIrcMessage(_, person, channel, message)

 echo(string.format('(%s) %s says, "%s"\n', channel, person, message))

end </lua>

Added to Mudlet in an unreleased 2.0rc8

sysDataSendRequest

Raised right before a command from the send() function or the command line is sent to the game - useful for keeping track of what your last command was, or even denying the command to be sent if necessary with denyCurrentSend().

Note: if you'll be making use of denyCurrentSend(), you really should notify the user that you denied their command - unexperienced ones might conclude that your script or Mudlet is buggy if they don't see visual feedback. Do not mis-use this and use it as keylogger either.

<lua> function onNetworkOutput(_, command)

 if math.random(2) == 1 then
   echo("Hello! Sending "..command.." to the game.\n")
 else
   echo("Not your day! Denying "..command..".\n")
 end

end </lua>

sysConnectionEvent

Raised when the profile becomes connected to a MUD - available in 2.0-test5+.

sysDisconnectionEvent

Raised when the profile becomes disconnected, either manually or by the game - available in 2.0-test5+.

sysTelnetEvent

Raised whenever an unsupported telnet option is encountered, allowing you to handle it yourself. The arguments that get passed with the event are type, telnet option, and the message. Available in 2.1+

sysMapDownloadEvent

Raised whenever an MMP map (currently only supported by IRE games) is downloaded and loaded in.

sysProtocolDisabled

Raised whenever a communications protocol is disabled, with the protocol name passed as an argument. Current values Mudlet will use for this are: GMCP, MDSP, ATCP, GMCP, MXP, and channel102.

sysProtocolEnabled

Raised whenever a communications protocol is enabled, with the protocol name passed as an argument. Current values Mudlet will use for this are: GMCP, MDSP, ATCP, MXP, and channel102.

<lua> function onProtocolEnabled(_, protocol)

 if protocol == "GMCP" then
   print("GMCP enabled! Now we can use GMCP data.")
 end

end </lua>