Difference between revisions of "Manual:Event Engine"

From Mudlet
Jump to navigation Jump to search
(Add sysInstall, sysUninstall, sysInstallPackage, sysInstallModule, sysSyncInstallModule, sysLuaInstallModule, sysUninstallPackage, sysUninstallModule, sysSyncUninstallModule, and sysLuaUninstallModule events)
(40 intermediate revisions by 7 users not shown)
Line 16: Line 16:
 
You can also register your event with the ''[[Manual:Miscellaneous_Functions#registerAnonymousEventHandler|registerAnonymousEventHandler(event name, function name)]]'' function inside your scripts:
 
You can also register your event with the ''[[Manual:Miscellaneous_Functions#registerAnonymousEventHandler|registerAnonymousEventHandler(event name, function name)]]'' function inside your scripts:
  
<lua>
+
<syntaxhighlight lang="lua">
 
-- example taken from the God Wars 2 (http://godwars2.org) Mudlet UI - forces the window to keep to a certain size
 
-- example taken from the God Wars 2 (http://godwars2.org) Mudlet UI - forces the window to keep to a certain size
 
function keepStaticSize()
 
function keepStaticSize()
Line 23: Line 23:
  
 
registerAnonymousEventHandler("sysWindowResizeEvent", "keepStaticSize")
 
registerAnonymousEventHandler("sysWindowResizeEvent", "keepStaticSize")
</lua>
+
</syntaxhighlight>
  
 
{{note}} Mudlet also uses the event system in-game protocols (like ATCP, GMCP and others).
 
{{note}} Mudlet also uses the event system in-game protocols (like ATCP, GMCP and others).
  
 
== Raising an event==  
 
== Raising an event==  
To raise an event, you’d use the [[Manual:Lua_Functions#raiseEvent|raiseEvent()]] function:
+
To raise an event, you'd use the [[Manual:Lua_Functions#raiseEvent|raiseEvent()]] function:
  
<lua>raiseEvent(name, [arguments...])</lua>
+
<syntaxhighlight lang="lua">raiseEvent(name, [arguments...])</syntaxhighlight>
  
 
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.
 
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
+
== Example of registering and raising an event ==
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:
+
Add a script to each profile you need the event.
  
<lua>
+
<syntaxhighlight lang="lua">
function check_health_stuff()
+
 
   echo("I work!\n")
+
-- This is the function that will be called when the event is raised.
 +
-- "event" is set to the event name.
 +
-- "arg" is the argument(s) provided with raiseEvent/rasieGlobalEvent.
 +
-- "profile" - Is the profile name from where the raiseGlobalEvent where triggered from
 +
--            It is 'nil' if raiseEvent() was used.
 +
function onMyEvent(event, arg, profile)
 +
  echo("Event: " .. event .. "\n")
 +
   echo("Arg  : " .. arg .. "\n")
 +
  -- If the event is not raised with raiseGlobalEvent() profile will be 'nil'
 +
  echo("Profile: " .. (profile or "Local" .. "\n")
 
end
 
end
</lua>
 
  
When the onPrompt event comes along, that script catches it, and runs ''check_health_stuff()'' for you.
+
-- Register the event handler.
 +
registerAnonymousEventHandler("my_event", "onMyEvent")
 +
</syntaxhighlight>
  
[[Link title]]
+
To raise the event you can call:
 +
<syntaxhighlight lang="lua">
 +
-- Trigger only in the current profile:
 +
raiseEvent("my_event", "Hello world!")
 +
 
 +
-- Trigger the event in all OTHER profiles:
 +
raiseGlobalEvent("my_event", "Hello World!")
 +
</syntaxhighlight>
  
 
==Mudlet-raised events==
 
==Mudlet-raised events==
Line 51: Line 68:
  
 
===channel102Message===
 
===channel102Message===
 +
 
Raised when a telnet sub-option 102 message is received (which comprises of two numbers passed in the event). This is of particular use with the [http://www.aardwolf.com Aardwolf MUD] who originated this protocol. See [http://forums.mudlet.org/viewtopic.php?f=6&t=1471 this] forum topic for more about the Mudlet Aardwolf GUI that makes use of this.
 
Raised when a telnet sub-option 102 message is received (which comprises of two numbers passed in the event). This is of particular use with the [http://www.aardwolf.com Aardwolf MUD] who originated this protocol. See [http://forums.mudlet.org/viewtopic.php?f=6&t=1471 this] forum topic for more about the Mudlet Aardwolf GUI that makes use of this.
  
Line 56: Line 74:
  
 
Raised when the mapper is opened - either the floating dock or the in-Mudlet widget.
 
Raised when the mapper is opened - either the floating dock or the in-Mudlet widget.
 +
 +
===sysAppStyleSheetChange===
 +
 +
Raised when [[Manual:UI_Functions#setAppStyleSheet|setAppStyleSheet]] is called and a new stylesheet applied to Mudlet.
 +
 +
<syntaxhighlight lang="lua">
 +
-- This will respond to a future (as yet unpublished) addition to the Mudlet code that will allow some
 +
-- of a default application style sheet to be supplied with Mudlet to compensate for some text colors
 +
-- that do not show up equally well in both light and dark desktop themes. That, perhaps, might finally
 +
-- allow different colored texts to be uses again for the trigger item types!
 +
function appStyleSheetChangeEvent( event, tag, source )
 +
  if source == "system" then
 +
    colorTable = colorTable or {}
 +
    if tag == "mudlet-dark-theme" then
 +
      colorTable.blue = {64, 64, 255}
 +
      colorTable.green = {0, 255, 0}
 +
    elseif tag == "mudlet-light-theme" then
 +
      colorTable.blue = {0, 0, 255}
 +
      colorTable.green = {64, 255, 64}
 +
    end
 +
  end
 +
end
 +
</syntaxhighlight>
 +
 +
{{note}}Available since Mudlet 3.19
  
 
===sysWindowResizeEvent===
 
===sysWindowResizeEvent===
Line 64: Line 107:
 
This sample code will echo whenever a resize happened with the new dimensions:
 
This sample code will echo whenever a resize happened with the new dimensions:
  
<lua>
+
<syntaxhighlight lang="lua">
 
function resizeEvent( event, x, y )
 
function resizeEvent( event, x, y )
 
   echo("RESIZE EVENT: event="..event.." x="..x.." y="..y.."\n")
 
   echo("RESIZE EVENT: event="..event.." x="..x.." y="..y.."\n")
 
end
 
end
</lua>
+
</syntaxhighlight>
  
 
===sysWindowMousePressEvent===
 
===sysWindowMousePressEvent===
Line 75: Line 118:
 
Example
 
Example
  
<lua>
+
<syntaxhighlight lang="lua">
 
function onClickHandler( event, button, x, y )
 
function onClickHandler( event, button, x, y )
 
   echo("CLICK event:"..event.." button="..button.." x="..x.." y="..y.."\n")
 
   echo("CLICK event:"..event.." button="..button.." x="..x.." y="..y.."\n")
 
end
 
end
</lua>
+
</syntaxhighlight>
  
 
===sysWindowMouseReleaseEvent===
 
===sysWindowMouseReleaseEvent===
Line 87: Line 130:
 
===sysLoadEvent===
 
===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.
+
Raised after Mudlet is done loading the profile, after all of the scripts, packages, and modules are installed. 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===
 
===sysExitEvent===
Line 99: Line 142:
 
Example - put it into a new script and save it to run:
 
Example - put it into a new script and save it to run:
  
<lua>
+
<syntaxhighlight lang="lua">
 
-- create a function to parse the downloaded webpage and display a result
 
-- create a function to parse the downloaded webpage and display a result
 
function downloaded_file(_, filename)
 
function downloaded_file(_, filename)
Line 121: Line 164:
 
-- download a list of fake users for a demo
 
-- download a list of fake users for a demo
 
downloadFile(getMudletHomeDir().."/achaea-who-count.html", "https://www.achaea.com/game/who")
 
downloadFile(getMudletHomeDir().."/achaea-who-count.html", "https://www.achaea.com/game/who")
</lua>
+
</syntaxhighlight>
  
 
You should see a result like this:
 
You should see a result like this:
Line 130: Line 173:
  
 
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.
 
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.
 +
 +
;Example
 +
<syntaxhighlight lang="lua">
 +
--if a download fails notify the player.
 +
function downloadErrorEventHandler(event, errorFound)
 +
    cecho("fuction downloadErrorEventHandler, "..errorFound)
 +
    debugc("fuction downloadErrorEventHandler, "..errorFound) --display to error screen in editor
 +
end --function downloadErrorEventHandler
 +
registerAnonymousEventHandler("sysDownloadError", "downloadErrorEventHandler")
 +
</syntaxhighlight>
  
 
===sysIrcMessage===
 
===sysIrcMessage===
  
Raised when you see or receive an IRC message. The speakers name, channel and their message will follow as arguments.
+
Raised when the IRC client receives an IRC message. The sender's name, channel name, and their message are passed as arguments to this event. 
 +
 
 +
Starting with Mudlet 3.3, this event changes slightly to provide more information from IRC network messages.  Data such as status codes, command responses, or error messages sent by the IRC Server may be formatted as plain text by the client and posted to lua via this event.
 +
 
 +
* '''''sender''''' argument may be the nick name of an IRC user or the name of the IRC server host, as retrievable by  [[Manual:Networking_Functions#getIrcConnectedHost|getIrcConnectedHost()]] 
 +
* '''''channel''''' argument may not always contain a channel name, but will be the name of the target/recipient of a message or action. In some networks the name may be that of a service (like ''"Auth"'' for example.
  
<lua>
+
;Example
function onIrcMessage(_, person, channel, message)
+
<syntaxhighlight lang="lua">
   echo(string.format('(%s) %s says, "%s"\n', channel, person, message))
+
function onIrcMessage(_, sender, target, message)
 +
   echo(string.format('(%s) %s says, "%s"\n', target, sender, message))
 
end
 
end
</lua>
+
</syntaxhighlight>
  
<sub>Added to Mudlet in an unreleased 2.0rc8</sub>
+
{{note}}Available since Mudlet 2.1
  
 
===sysDataSendRequest===
 
===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()''.
+
Raised right before a command from the send(), sendAll() functions or the command line is sent to the game - useful for keeping track of what your last command was, manipulating input or even denying the command to be sent if necessary with [[Manual:Miscellaneous_Functions#denyCurrentSend|denyCurrentSend()]].
 +
 
 +
sysDataSendRequest will send the event name and the command sent (in string form) to the functions registered to it. IE: commandSent in the example below will be "eat hambuger" if the user entered only that into command line and pressed enter, send("eat hamburger"), sendAll("eat humburger", "eat fish") or sendAll("eat fish", "eat humburger")
  
 
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 [http://en.wikipedia.org/wiki/Keylogger keylogger] either.
 
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 [http://en.wikipedia.org/wiki/Keylogger keylogger] either.
  
<lua>
+
<syntaxhighlight lang="lua">
function onNetworkOutput(_, command)
+
-- cancels all "eat hambuger" commands
   if math.random(2) == 1 then
+
function cancelEatHamburger(eventName, commandSent)
     echo("Hello! Sending "..command.." to the game.\n")
+
   if commandSent == "eat hamburger" then
   else
+
     denyCurrentSend() --cancels the command sent.
    echo("Not your day! Denying "..command..".\n")
+
    cecho("<red>Denied! Didn't let the command through.\n")
  end
+
   end --if commandSent == eat hamburger
end
+
end --function cancelEatHamburger
</lua>
+
 
 +
registerAnonymousEventHandler("sysDataSendRequest", "cancelEatHamburger")
 +
</syntaxhighlight>
 +
 
 +
If you wanted to control input you could set a bool after a trigger.
 +
This is useful if you want alias like control, do not know what text will be entered, but do know a trigger that WILL occur just before the user enters the command.
 +
 
 +
<syntaxhighlight lang="lua">
 +
function controlInput(_, command)
 +
  if changeCMDInput then --if changeCMDInput is true
 +
  changeCMDInput = false --set this if check to false to it doesn't occur every input.
 +
        --Also set the bool check BEFORE any send() functions within a sysDataSendRequest function.
 +
    sendAll(command .. "some target", command .. "some other target", true) --Duplicate and unknown command
 +
    denyCurrentSend() --Deny the origional command, not the commands sent with sendAll.
 +
  end --if changeCMDInput
 +
end --function controlInput
 +
registerAnonymousEventHandler("sysDataSendRequest", "controlInput")
 +
</syntaxhighlight>
 +
 
 +
Take note that functions registered under sysDataSendRequest WILL trigger with ALL commands that are sent.
 +
 
 +
===sysDeleteHttpDone===
 +
Raised whenever a [[Manual:Networking_Functions#deleteHTTP|deleteHTTP()]] request completes successfully. Arguments with the event are the URL that the request was sent to and the response body (if it's less than 10k characters).
 +
 
 +
See also [[#sysDeleteHttpError|sysDeleteHttpError]].
 +
 
 +
===sysDeleteHttpError===
 +
Raised whenever a [[Manual:Networking_Functions#deleteHTTP|deleteHTTP()]] request fails. Arguments are the error message and the URL that the request was sent to.
 +
 
 +
See also [[#sysDeleteHttpDone|sysDeleteHttpDone]].
  
 
===sysConnectionEvent===
 
===sysConnectionEvent===
Line 170: Line 260:
 
Raised whenever the "set location" command (on the 2D mapper context menu) is used to reposition the current "player room".  It provides the room ID of the new room ID that the player has been moved to.
 
Raised whenever the "set location" command (on the 2D mapper context menu) is used to reposition the current "player room".  It provides the room ID of the new room ID that the player has been moved to.
  
{{note}} Present since Mudlet 3.0.0 ''Epsilon'' preview.
+
{{note}}Available since Mudlet 3.0
  
 
===sysMapDownloadEvent===
 
===sysMapDownloadEvent===
Raised whenever an MMP map (currently only supported by IRE games) is downloaded and loaded in.
+
Raised whenever an [[Standards:MMP|MMP map]] is downloaded and loaded in.
 +
 
 +
===sysPostHttpDone===
 +
Raised whenever a [[Manual:Networking_Functions#postHTTP|postHTTP()]] request completes successfully. Arguments with the event are the URL that the request was sent to and the response body (if it's less than 10k characters).
 +
 
 +
See also [[#sysPostHttpError|sysPostHttpError]].
 +
 
 +
===sysPostHttpError===
 +
Raised whenever a [[Manual:Networking_Functions#postHTTP|postHTTP()]] request fails. Arguments are the error message and the URL that the request was sent to.
 +
 
 +
See also [[#sysPostHttpDone|sysPostHttpDone]].
  
 
===sysProtocolDisabled===
 
===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.
+
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, MXP, and channel102.
  
 
===sysProtocolEnabled===
 
===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.
 
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>
+
<syntaxhighlight lang="lua">
 
function onProtocolEnabled(_, protocol)
 
function onProtocolEnabled(_, protocol)
 
   if protocol == "GMCP" then
 
   if protocol == "GMCP" then
Line 187: Line 287:
 
   end
 
   end
 
end
 
end
</lua>
+
</syntaxhighlight>
 +
 
 +
===sysPutHttpDone===
 +
Raised whenever a [[Manual:Networking_Functions#putHTTP|putHTTP()]] request completes successfully. Arguments with the event are the URL that the request was sent to and the response body (if it's less than 10k characters).
 +
 
 +
See also [[#sysPutHttpError|sysPutHttpError]].
 +
 
 +
===sysPutHttpError===
 +
Raised whenever a [[Manual:Networking_Functions#putHTTP|putHTTP()]] request fails. Arguments are the error message and the URL that the request was sent to.
 +
 
 +
See also [[#sysPutHttpDone|sysPutHttpDone]].
  
 
===sysInstall===
 
===sysInstall===
Line 194: Line 304:
 
Event handlers receive the name of the installed package or module as additional argument.
 
Event handlers receive the name of the installed package or module as additional argument.
  
{{note}} Available in Mudlet 3.1+
+
{{note}}Available since Mudlet 3.1
 +
 
 +
<syntaxhighlight lang="lua">
 +
function myScriptInstalled(_, name)
 +
  -- stop if what got installed isn't my thing
 +
  if name ~= "my script name here" then return end
 +
 
 +
  print("Hey, thanks for installing my thing!")
 +
end
 +
registerAnonymousEventHandler("sysInstallPackage", "myScriptInstalled")
 +
</syntaxhighlight>
  
 
===sysUninstall===
 
===sysUninstall===
Line 201: Line 321:
 
Event handlers receive the name of the removed package or module as additional argument.
 
Event handlers receive the name of the removed package or module as additional argument.
  
{{note}} Available in Mudlet 3.1+
+
{{note}}Available since Mudlet 3.1
  
 
===sysInstallPackage===
 
===sysInstallPackage===
Line 208: Line 328:
 
Event handlers receive the name of the installed package as well as the file name as additional arguments.
 
Event handlers receive the name of the installed package as well as the file name as additional arguments.
  
{{note}} Available in Mudlet 3.1+
+
{{note}}Available since Mudlet 3.1
  
 
===sysInstallModule===
 
===sysInstallModule===
Line 215: Line 335:
 
Event handlers receive the name of the installed module as well as the file name as additional arguments.
 
Event handlers receive the name of the installed module as well as the file name as additional arguments.
  
{{note}} Available in Mudlet 3.1+
+
{{note}}Available since Mudlet 3.1
  
 
===sysSyncInstallModule===
 
===sysSyncInstallModule===
Line 222: Line 342:
 
Event handlers receive the name of the installed module as well as the file name as additional arguments.
 
Event handlers receive the name of the installed module as well as the file name as additional arguments.
  
{{note}} Available in Mudlet 3.1+
+
{{note}}Available since Mudlet 3.1
  
 
===sysLuaInstallModule===
 
===sysLuaInstallModule===
Line 229: Line 349:
 
Event handlers receive the name of the installed module as well as the file name as additional arguments.
 
Event handlers receive the name of the installed module as well as the file name as additional arguments.
  
{{note}} Available in Mudlet 3.1+
+
{{note}}Available since Mudlet 3.1
  
 
===sysUninstallPackage===
 
===sysUninstallPackage===
Line 236: Line 356:
 
Event handlers receive the name of the removed package as additional argument.
 
Event handlers receive the name of the removed package as additional argument.
  
{{note}} Available in Mudlet 3.1+
+
{{note}}Available since Mudlet 3.1
  
===sysInstallModule===
+
===sysUninstallModule===
 
Raised right before a module is being removed through the module dialog. This can be used to display post-removal information or for cleanup.
 
Raised right before a module is being removed through the module dialog. This can be used to display post-removal information or for cleanup.
  
 
Event handlers receive the name of the removed module as additional argument.
 
Event handlers receive the name of the removed module as additional argument.
  
{{note}} Available in Mudlet 3.1+
+
{{note}}Available since Mudlet 3.1
  
===sysSyncInstallModule===
+
===sysSyncUninstallModule===
 
Raised right before a module is being removed through the "sync" mechanism. This can be used to display post-removal information or for cleanup.
 
Raised right before a module is being removed through the "sync" mechanism. This can be used to display post-removal information or for cleanup.
  
 
Event handlers receive the name of the removed module as additional argument.
 
Event handlers receive the name of the removed module as additional argument.
  
{{note}} Available in Mudlet 3.1+
+
{{note}}Available since Mudlet 3.1
  
===sysLuaInstallModule===
+
===sysLuaUninstallModule===
 
Raised right before a module is being removed through the lua uninstallModule command. This can be used to display post-removal information or for cleanup.
 
Raised right before a module is being removed through the lua uninstallModule command. This can be used to display post-removal information or for cleanup.
  
 
Event handlers receive the name of the removed module as additional argument.
 
Event handlers receive the name of the removed module as additional argument.
  
{{note}} Available in Mudlet 3.1+
+
{{note}}Available since Mudlet 3.1
 +
 
 +
===sysSoundFinished===
 +
Raised when a sound finished playing. This can be used in a music player for example to start the next song.
 +
 
 +
Event handlers receive the sound file name and the file path as additional arguments.
 +
 
 +
{{note}}Available since Mudlet 3.1
 +
 
 +
<!-- does not actually seem to work
 +
 
 +
===sysGamepadConnected===
 +
Raised a [https://en.wikipedia.org/wiki/Gamepad gamepad] is connected. On Windows, this uses the XInput backend.
 +
 
 +
Event handlers receive device ID as an argument.
 +
 
 +
{{note}}Available since Mudlet 3.3
 +
 
 +
Example:
 +
 
 +
<syntaxhighlight lang="lua">
 +
function noticeConnection(deviceID)
 +
  local output = string.format("New gamepad connected: %s\n", deviceID)
 +
  echo(output)
 +
end
 +
 
 +
function noticeDisconnection(deviceID)
 +
  local output = string.format("Gamepad disconnected: %s\n", deviceID)
 +
  echo(output)
 +
end
 +
 
 +
function noticeButtonPress(deviceID, buttonNumber, valueNumber)
 +
  local output = string.format("Button pressed: gamepad %s, button %s, value %s\n", deviceID, buttonNumber, valueNumber)
 +
  echo(output)
 +
end
 +
 
 +
function noticeButtonRelease(deviceID, buttonNumber, valueNumber)
 +
  local output = string.format("Button released: gamepad %s, button %s, value %s\n", deviceID, buttonNumber, valueNumber)
 +
  echo(output)
 +
end
 +
 
 +
function noticeAxis(deviceID, axisNumber, valueNumber)
 +
  local output = string.format("Axis moved: gamepad %s, axis %s, value %s\n", deviceID, axisNumber, valueNumber)
 +
  echo(output)
 +
end
 +
 
 +
registerAnonymousEventHandler("sysGamepadConnected", "noticeConnection")
 +
registerAnonymousEventHandler("sysGamepadDisconnected", "noticeDisconnection")
 +
registerAnonymousEventHandler("sysGamepadButtonPress", "noticeButtonPress")
 +
registerAnonymousEventHandler("sysGamepadButtonRelease", "noticeButtonRelease")
 +
registerAnonymousEventHandler("sysGamepadAxisMove", "noticeAxis")
 +
</syntaxhighlight>
 +
 
 +
===sysGamepadDisconnected===
 +
Raised a [https://en.wikipedia.org/wiki/Gamepad gamepad] is disconnected
 +
 
 +
Event handlers receive device ID as an argument.
 +
 
 +
{{note}}Available since Mudlet 3.3
 +
 
 +
===sysGamepadButtonPress===
 +
Raised a button on a gamepad is pressed down.
 +
 
 +
Event handlers receive the device ID, button number, and the new value as a number.
 +
 
 +
===sysGamepadButtonRelease===
 +
Raised a button on a gamepad is released.
 +
 
 +
Event handlers receive the device ID, button number, and the new value as a number.
 +
 
 +
{{note}}Available since Mudlet 3.3
 +
 
 +
===sysGamepadAxisMove===
 +
Raised an axis on a gamepad is moved.
 +
 
 +
Event handlers receive the device ID, axis number, and the new value as a number.
 +
 
 +
{{note}}Available since Mudlet 3.3
  
 +
-->
 
[[Category:Mudlet Manual]]
 
[[Category:Mudlet Manual]]

Revision as of 18:12, 3 December 2019

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:

-- 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")

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

Raising an event

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

raiseEvent(name, [arguments...])

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.

Example of registering and raising an event

Add a script to each profile you need the event.

-- This is the function that will be called when the event is raised.
-- "event" is set to the event name.
-- "arg" is the argument(s) provided with raiseEvent/rasieGlobalEvent.
-- "profile" - Is the profile name from where the raiseGlobalEvent where triggered from
--             It is 'nil' if raiseEvent() was used.
function onMyEvent(event, arg, profile)
  echo("Event: " .. event .. "\n")
  echo("Arg  : " .. arg .. "\n")
  -- If the event is not raised with raiseGlobalEvent() profile will be 'nil'
  echo("Profile: " .. (profile or "Local" .. "\n")
end

-- Register the event handler.
registerAnonymousEventHandler("my_event", "onMyEvent")

To raise the event you can call:

-- Trigger only in the current profile:
raiseEvent("my_event", "Hello world!")

-- Trigger the event in all OTHER profiles:
raiseGlobalEvent("my_event", "Hello World!")

Mudlet-raised events

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

channel102Message

Raised when a telnet sub-option 102 message is received (which comprises of two numbers passed in the event). This is of particular use with the Aardwolf MUD who originated this protocol. See this forum topic for more about the Mudlet Aardwolf GUI that makes use of this.

mapOpenEvent

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

sysAppStyleSheetChange

Raised when setAppStyleSheet is called and a new stylesheet applied to Mudlet.

-- This will respond to a future (as yet unpublished) addition to the Mudlet code that will allow some
-- of a default application style sheet to be supplied with Mudlet to compensate for some text colors
-- that do not show up equally well in both light and dark desktop themes. That, perhaps, might finally
-- allow different colored texts to be uses again for the trigger item types!
function appStyleSheetChangeEvent( event, tag, source )
  if source == "system" then
    colorTable = colorTable or {}
    if tag == "mudlet-dark-theme" then
      colorTable.blue = {64, 64, 255}
      colorTable.green = {0, 255, 0}
    elseif tag == "mudlet-light-theme" then
      colorTable.blue = {0, 0, 255}
      colorTable.green = {64, 255, 64}
    end
  end
end

Note Note: Available since Mudlet 3.19

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:

function resizeEvent( event, x, y )
  echo("RESIZE EVENT: event="..event.." x="..x.." y="..y.."\n")
end

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

function onClickHandler( event, button, x, y )
  echo("CLICK event:"..event.." button="..button.." x="..x.." y="..y.."\n")
end

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 after Mudlet is done loading the profile, after all of the scripts, packages, and modules are installed. 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:

-- 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")

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.

Example
--if a download fails notify the player.
function downloadErrorEventHandler(event, errorFound)
    cecho("fuction downloadErrorEventHandler, "..errorFound)
    debugc("fuction downloadErrorEventHandler, "..errorFound) --display to error screen in editor
end --function downloadErrorEventHandler
registerAnonymousEventHandler("sysDownloadError", "downloadErrorEventHandler")

sysIrcMessage

Raised when the IRC client receives an IRC message. The sender's name, channel name, and their message are passed as arguments to this event.

Starting with Mudlet 3.3, this event changes slightly to provide more information from IRC network messages. Data such as status codes, command responses, or error messages sent by the IRC Server may be formatted as plain text by the client and posted to lua via this event.

  • sender argument may be the nick name of an IRC user or the name of the IRC server host, as retrievable by getIrcConnectedHost()
  • channel argument may not always contain a channel name, but will be the name of the target/recipient of a message or action. In some networks the name may be that of a service (like "Auth" for example.)
Example
function onIrcMessage(_, sender, target, message)
  echo(string.format('(%s) %s says, "%s"\n', target, sender, message))
end

Note Note: Available since Mudlet 2.1

sysDataSendRequest

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

sysDataSendRequest will send the event name and the command sent (in string form) to the functions registered to it. IE: commandSent in the example below will be "eat hambuger" if the user entered only that into command line and pressed enter, send("eat hamburger"), sendAll("eat humburger", "eat fish") or sendAll("eat fish", "eat humburger")

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.

-- cancels all "eat hambuger" commands
function cancelEatHamburger(eventName, commandSent)
  if commandSent == "eat hamburger" then
    denyCurrentSend() --cancels the command sent.
    cecho("<red>Denied! Didn't let the command through.\n")
  end --if commandSent == eat hamburger
end --function cancelEatHamburger

registerAnonymousEventHandler("sysDataSendRequest", "cancelEatHamburger")

If you wanted to control input you could set a bool after a trigger. This is useful if you want alias like control, do not know what text will be entered, but do know a trigger that WILL occur just before the user enters the command.

function controlInput(_, command) 
  if changeCMDInput then --if changeCMDInput is true
  	changeCMDInput = false --set this if check to false to it doesn't occur every input.
        --Also set the bool check BEFORE any send() functions within a sysDataSendRequest function.
    sendAll(command .. "some target", command .. "some other target", true) --Duplicate and unknown command
    denyCurrentSend() --Deny the origional command, not the commands sent with sendAll.
   end --if changeCMDInput
end --function controlInput
registerAnonymousEventHandler("sysDataSendRequest", "controlInput")

Take note that functions registered under sysDataSendRequest WILL trigger with ALL commands that are sent.

sysDeleteHttpDone

Raised whenever a deleteHTTP() request completes successfully. Arguments with the event are the URL that the request was sent to and the response body (if it's less than 10k characters).

See also sysDeleteHttpError.

sysDeleteHttpError

Raised whenever a deleteHTTP() request fails. Arguments are the error message and the URL that the request was sent to.

See also sysDeleteHttpDone.

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+

sysManualLocationSetEvent

Raised whenever the "set location" command (on the 2D mapper context menu) is used to reposition the current "player room". It provides the room ID of the new room ID that the player has been moved to.

Note Note: Available since Mudlet 3.0

sysMapDownloadEvent

Raised whenever an MMP map is downloaded and loaded in.

sysPostHttpDone

Raised whenever a postHTTP() request completes successfully. Arguments with the event are the URL that the request was sent to and the response body (if it's less than 10k characters).

See also sysPostHttpError.

sysPostHttpError

Raised whenever a postHTTP() request fails. Arguments are the error message and the URL that the request was sent to.

See also sysPostHttpDone.

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, 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.

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

sysPutHttpDone

Raised whenever a putHTTP() request completes successfully. Arguments with the event are the URL that the request was sent to and the response body (if it's less than 10k characters).

See also sysPutHttpError.

sysPutHttpError

Raised whenever a putHTTP() request fails. Arguments are the error message and the URL that the request was sent to.

See also sysPutHttpDone.

sysInstall

Raised right after a module or package is being installed by any means. This can be used to display post-installation information or setup things.

Event handlers receive the name of the installed package or module as additional argument.

Note Note: Available since Mudlet 3.1

function myScriptInstalled(_, name)
  -- stop if what got installed isn't my thing
  if name ~= "my script name here" then return end

  print("Hey, thanks for installing my thing!")
end
registerAnonymousEventHandler("sysInstallPackage", "myScriptInstalled")

sysUninstall

Raised right before a module or package is being uninstalled by any means. This can be used to display post-removal information or for cleanup.

Event handlers receive the name of the removed package or module as additional argument.

Note Note: Available since Mudlet 3.1

sysInstallPackage

Raised right after a package is being installed by any means. This can be used to display post-installation information or setup things.

Event handlers receive the name of the installed package as well as the file name as additional arguments.

Note Note: Available since Mudlet 3.1

sysInstallModule

Raised right after a module is being installed through the module dialog. This can be used to display post-installation information or setup things.

Event handlers receive the name of the installed module as well as the file name as additional arguments.

Note Note: Available since Mudlet 3.1

sysSyncInstallModule

Raised right after a module is being installed through the "sync" mechanism. This can be used to display post-installation information or setup things.

Event handlers receive the name of the installed module as well as the file name as additional arguments.

Note Note: Available since Mudlet 3.1

sysLuaInstallModule

Raised right after a module is being installed through the lua installModule command. This can be used to display post-installation information or setup things.

Event handlers receive the name of the installed module as well as the file name as additional arguments.

Note Note: Available since Mudlet 3.1

sysUninstallPackage

Raised right before a package is being removed by any means. This can be used to display post-removal information or for cleanup.

Event handlers receive the name of the removed package as additional argument.

Note Note: Available since Mudlet 3.1

sysUninstallModule

Raised right before a module is being removed through the module dialog. This can be used to display post-removal information or for cleanup.

Event handlers receive the name of the removed module as additional argument.

Note Note: Available since Mudlet 3.1

sysSyncUninstallModule

Raised right before a module is being removed through the "sync" mechanism. This can be used to display post-removal information or for cleanup.

Event handlers receive the name of the removed module as additional argument.

Note Note: Available since Mudlet 3.1

sysLuaUninstallModule

Raised right before a module is being removed through the lua uninstallModule command. This can be used to display post-removal information or for cleanup.

Event handlers receive the name of the removed module as additional argument.

Note Note: Available since Mudlet 3.1

sysSoundFinished

Raised when a sound finished playing. This can be used in a music player for example to start the next song.

Event handlers receive the sound file name and the file path as additional arguments.

Note Note: Available since Mudlet 3.1