Difference between revisions of "Manual:Mudlet Object Functions"
Line 5: | Line 5: | ||
;appendCmdLine() | ;appendCmdLine() | ||
: Appends text to the main input line. | : Appends text to the main input line. | ||
+ | |||
+ | : See also: [[Manual:Lua_Functions#printCmdLine|printCmdLine()]] | ||
;Example | ;Example |
Revision as of 16:27, 17 January 2019
Mudlet Object Functions
appendCmdLine
- appendCmdLine()
- Appends text to the main input line.
- See also: printCmdLine()
- Example
-- adds the text "55 backpacks" to whatever is currently in the input line
appendCmdLine("55 backpacks")
-- makes a link, that when clicked, will add "55 backpacks" to the input line
echoLink("press me", "appendCmdLine'55 backpack'", "Press me!")
clearCmdLine
- clearCmdLine()
- Clears the input line of any text that's been entered.
- Example
-- don't be evil with this!
clearCmdLine()
createStopWatch
- createStopWatch()
- This function creates a stopwatch, a high resolution time measurement tool. Stopwatches can be started, stopped, reset and asked how much time has passed since the stop watch has been started.
- Returns: The ID of a stopwatch.
- Example
- In a global script you can create all stop watches that you need in your system and store the respective stopWatch-IDs in global variables:
fightStopWatch = fightStopWatch or createStopWatch() -- create, or re-use a stopwatch, and store the watchID in a global variable to access it from anywhere
-- then you can start the stop watch in some trigger/alias/script with:
startStopWatch(fightStopWatch)
-- to stop the watch and measure its time in e.g. a trigger script you can write:
fightTime = stopStopWatch(fightStopWatch)
echo("The fight lasted for " .. fightTime .. " seconds.")
resetStopWatch(fightStopWatch)
- You can also measure the elapsed time without having to stop the stop watch with getStopWatchTime().
Note: it's best to re-use stopwatch IDs if you can - Mudlet at the moment does not delete them, so creating more and more would use more memory.
disableAlias
- disableAlias(name)
- Disables/deactivates the alias by its name. If several aliases have this name, they'll all be disabled.
- See also: enableAlias()
- Parameters
- name:
- The name of the alias. Passed as a string.
- Examples
--Disables the alias called 'my alias'
disableAlias("my alias")
disableKey
- disableKey(name)
- Disables key a key (macro) or a key group. When you disable a key group, all keys within the group will be implicitly disabled as well.
- See also: enableKey()
- Parameters
- name:
- The name of the key or group to identify what you'd like to disable.
- Examples
-- you could set multiple keys on the F1 key and swap their use as you wish by disabling and enabling them
disableKey("attack macro")
disableKey("jump macro")
enableKey("greet macro")
Note:' From Version 3.10 returns true if one or more keys or groups of keys were found that matched the name given or false if not; prior to then it returns true if there were any keys - whether they matched the name or not!
disableTimer
- disableTimer(name)
- Disables a timer from running it’s script when it fires - so the timer cycles will still be happening, just no action on them. If you’d like to permanently delete it, use killTrigger instead.
- See also: enableTimer()
- Parameters
- name:
- Expects the timer ID that was returned by tempTimer on creation of the timer or the name of the timer in case of a GUI timer.
- Example
--Disables the timer called 'my timer'
disableTimer("my timer")
disableTrigger
- disableTrigger(name)
- Disables a permanent (one in the trigger editor) or a temporary trigger.
- See also: enableTrigger()
- Parameters
- name:
- Expects the trigger ID that was returned by tempTrigger or other temp*Trigger variants, or the name of the trigger in case of a GUI trigger.
- Example
-- Disables the trigger called 'my trigger'
disableTrigger("my trigger")
enableAlias
- enableAlias(name)
- Enables/activates the alias by it’s name. If several aliases have this name, they’ll all be enabled.
- See also: disableAlias()
- Parameters
- name:
- Expects the alias ID that was returned by tempAlias on creation of the alias or the name of the alias in case of a GUI alias.
- Example
--Enables the alias called 'my alias'
enableAlias("my alias")
enableKey
- enableKey(name)
- Enables a key (macro) or a group of keys (and thus all keys within it that aren't explicitly deactivated).
- Parameters
- name:
- The name of the group that identifies the key.
-- you could use this to disable one key set for the numpad and activate another
disableKey("fighting keys")
enableKey("walking keys")
Note:' From Version 3.10 returns true if one or more keys or groups of keys were found that matched the name given or false if not; prior to then it returns true if there were any keys - whether they matched the name or not!
enableTimer
- enableTimer(name)
- Enables or activates a timer that was previously disabled.
- Parameters
- name:
- Expects the timer ID that was returned by tempTimer on creation of the timer or the name of the timer in case of a GUI timer.
-- enable the timer called 'my timer' that you created in Mudlets timers section
enableTimer("my timer")
-- or disable & enable a tempTimer you've made
timerID = tempTimer(10, [[echo("hi!")]])
-- it won't go off now
disableTimer(timerID)
-- it will continue going off again
enableTimer(timerID)
enableTrigger
- enableTrigger(name)
- Enables or activates a trigger that was previously disabled.
- Parameters
- name:
- Expects the trigger ID that was returned by tempTrigger or by any other temp*Trigger variants, or the name of the trigger in case of a GUI trigger.
-- enable the trigger called 'my trigger' that you created in Mudlets triggers section
enableTrigger("my trigger")
-- or disable & enable a tempTrigger you've made
triggerID = tempTrigger("some text that will match in a line", [[echo("hi!")]])
-- it won't go off now when a line with matching text comes by
disableTrigger(triggerID)
-- and now it will continue going off again
enableTrigger(triggerID)
exists
- exists(name, type)
- Tells you how many things of the given type exist.
- Parameters
- name:
- The name or the id returned by the temp* function to identify the item.
- type:
- The type can be 'alias', 'trigger', 'timer' or as of Mudlet 3.2, 'keybind'.
- Example
echo("I have " .. exists("my trigger", "trigger") .. " triggers called 'my trigger'!")
- You can also use this alias to avoid creating duplicate things, for example:
-- this code doesn't check if an alias already exists and will keep creating new aliases
permAlias("Attack", "General", "^aa$", [[send ("kick rat")]])
-- while this code will make sure that such an alias doesn't exist first
-- we do == 0 instead of 'not exists' because 0 is considered true in Lua
if exists("Attack", "alias") == 0 then
permAlias("Attack", "General", "^aa$", [[send ("kick rat")]])
end
- Especially helpful when working with permTimer:
if not exists("My animation timer", "timer") then
vdragtimer = permTimer("My animation timer", "", .016, onVDragTimer) -- 60fps timer!
end
enableTimer("My animation timer")
getButtonState
- getButtonState()
- This function can be used within checkbox button scripts (2-state buttons) to determine the current state of the checkbox.
- Returns 2 if button is checked, and 1 if it's not.
- Example
local checked = getButtonState()
if checked == 1 then
hideExits()
else
showExits()
end
getCmdLine
- getCmdLine()
- Returns the current content of the main input line.
- See also: printCmdLine()
- Example
-- replaces whatever is currently in the input line by "55 backpacks"
printCmdLine("55 backpacks")
--prints the text "55 backpacks" to the main console
echo(getCmdLine())
Note: Available in Mudlet 3.1+
invokeFileDialog
- invokeFileDialog(fileOrFolder, dialogTitle)
- Opens a file chooser dialog, allowing the user to select a file or a folder visually. The function returns the selected path or "" if there was none chosen.
- Parameters
- fileOrFolder: true for file selection, false for folder selection.
- dialogTitle: what to say in the window title.
- Examples
function whereisit()
local path = invokeFileDialog(false, "Where should we save the file? Select a folder and click Open")
if path == "" then return nil else return path end
end
isActive
- isActive(name, type)
- You can use this function to check if something, or somethings, are active.
- Returns the number of active things - and 0 if none are active. Beware that all numbers are true in Lua, including zero.
- Parameters
- name:
- The name or the id returned by temp* function to identify the item.
- type:
- The type can be 'alias', 'trigger', 'timer', or as Mudlet 3.2, 'keybind'.
- Example
echo("I have " .. isActive("my trigger", "trigger") .. " currently active trigger(s) called 'my trigger'!")
isPrompt
- isPrompt()
- Returns true or false depending on if the line at the cursor position is a prompt. This infallible feature is available for MUDs that supply GA events (to check if yours is one, look to bottom-right of the main window - if it doesn’t say <No GA>, then it supplies them).
- Example use could be as a Lua function, making closing gates on a prompt real easy.
- Example
-- make a trigger pattern with 'Lua function', and this will trigger on every prompt!
return isPrompt()
killAlias
- killAlias(aliasID)
- Deletes a temporary alias with the given ID.
- Parameters
- aliasID:
- The id returned by tempAlias to identify the alias.
-- deletes the alias with ID 5
killAlias(5)
killKey
- killKey(name)
- Deletes a keybinding with the given name. If several keybindings have this name, they'll all be deleted.
- Parameters
- name:
- The name or the id returned by tempKey to identify the key.
Note: Available in Mudlet 3.2+
-- make a temp key
local keyid = tempKey(mudlet.key.F8, [[echo("hi!")]])
-- delete the said temp key
killKey(keyid)
killTimer
- killTimer(id)
- Deletes a tempTimer.
Note: Non-temporary timers that you have set up in the GUI OR by using permTimer cannot be deleted with this function. Use disableTimer() and enableTimer() to turn them on or off.
- Parameters
- id: the ID returned by tempTimer.
- Returns true on success and false if the timer id doesn’t exist anymore (timer has already fired) or the timer is not a temp timer.
- Example
-- create the timer and remember the timer ID
timerID = tempTimer(10, [[echo("hello!")]])
-- delete the timer
if killTimer(timerID) then echo("deleted the timer") else echo("timer is already deleted") end
killTrigger
- killTrigger(id)
- Deletes a tempTrigger, or a trigger made with one of the temp<type>Trigger() functions.
Note: When used in out of trigger contexts, the triggers are disabled and deleted upon the next line received from the game - so if you are testing trigger deletion within an alias, the 'statistics' window will be reporting trigger counts that are disabled and pending removal, and thus are no cause for concern.
- Parameters
- id:
- The ID returned by tempTimer to identify the item. ID is a string and not a number.
- Returns true on success and false if the trigger id doesn’t exist anymore (trigger has already fired) or the trigger is not a temp trigger.
permAlias
- permAlias(name, parent, regex, lua code)
- Creates a persistent alias that stays after Mudlet is restarted and shows up in the Script Editor.
- Parameters
- name:
- The name you’d like the alias to have.
- parent:
- The name of the group, or another alias you want the trigger to go in - however if such a group/alias doesn’t exist, it won’t do anything. Use "" to make it not go into any groups.
- regex:
- The pattern that you’d like the alias to use.
- lua code:
- The script the alias will do when it matches.
- Example
-- creates an alias called "new alias" in a group called "my group"
permAlias("new alias", "my group", "^test$", [[echo ("say it works! This alias will show up in the script editor too.")]])
Note: Mudlet by design allows duplicate names - so calling permAlias with the same name will keep creating new aliases. You can check if an alias already exists with the exists function.
permGroup
- permGroup(name, itemtype, [parent])
- Creates a new group of a given type at the root level (not nested in any other groups). This group will persist through Mudlet restarts.
- Parameters
- name:
- The name of the new group item you want to create.
- itemtype:
- The type of the item, can be trigger, alias, or timer.
- parent:
- (optional) Name of existing item which the new item will be created as a child of.
- Example
--create a new trigger group
permGroup("Combat triggers", "trigger")
--create a new alias group only if one doesn't exist already
if exists("Defensive aliases", "alias") == 0 then
permGroup("Defensive aliases", "alias")
end
Note: Added to Mudlet in the 2.0 final release. Parameter parent available in Mudlet 3.1+
permRegexTrigger
- permRegexTrigger(name, parent, pattern table, lua code)
- Creates a persistent trigger with one or more regex patterns that stays after Mudlet is restarted and shows up in the Script Editor.
- Parameters
- name is the name you’d like the trigger to have.
- parent is the name of the group, or another trigger you want the trigger to go in - however if such a group/trigger doesn’t exist, it won’t do anything. Use "" to make it not go into any groups.
- pattern table is a table of patterns that you’d like the trigger to use - it can be one or many.
- lua code is the script the trigger will do when it matches.
- Example
-- Create a regex trigger that will match on the prompt to record your status
permRegexTrigger("Prompt", "", {"^(\d+)h, (\d+)m"}, [[health = tonumber(matches[2]); mana = tonumber(matches[3])]])
Note: Mudlet by design allows duplicate names - so calling permRegexTrigger with the same name will keep creating new triggers. You can check if a trigger already exists with the exists() function.
permBeginOfLineStringTrigger
- permBeginOfLineStringTrigger(name, parent, pattern table, lua code)
- Creates a persistent trigger that stays after Mudlet is restarted and shows up in the Script Editor. The trigger will go off whenever one of the regex patterns it's provided with matches. The function returns the trigger ID for subsequent enableTrigger(), disableTrigger() and killTrigger() calls.
- Parameters
- name is the name you’d like the trigger to have.
- parent is the name of the group, or another trigger you want the trigger to go in - however if such a group/trigger doesn’t exist, it won’t do anything. Use "" to make it not go into any groups.
- pattern table is a table of patterns that you’d like the trigger to use - it can be one or many.
- lua code is the script the trigger will do when it matches.
- Examples
-- Create a trigger that will match on anything that starts with "You sit" and do "stand".
-- It will not go into any groups, so it'll be on the top.
permBeginOfLineStringTrigger("Stand up", "", {"You sit"}, [[send ("stand")]])
-- Another example - lets put our trigger into a "General" folder and give it several patterns.
permBeginOfLineStringTrigger("Stand up", "General", {"You sit", "You fall", "You are knocked over by"}, [[send ("stand")]])
Note: Mudlet by design allows duplicate names - so calling permBeginOfLineStringTrigger with the same name will keep creating new triggers. You can check if a trigger already exists with the exists() function.
permSubstringTrigger
- permSubstringTrigger( name, parent, pattern table, lua code )
- Creates a persistent trigger with one or more substring patterns that stays after Mudlet is restarted and shows up in the Script Editor.
- Parameters
- name is the name you’d like the trigger to have.
- parent is the name of the group, or another trigger you want the trigger to go in - however if such a group/trigger doesn’t exist, it won’t do anything. Use "" to make it not go into any groups.
- pattern table is a table of patterns that you’d like the trigger to use - it can be one or many.
- lua code is the script the trigger will do when it matches.
- Example
-- Create a trigger to highlight the word "pixie" for us
permSubstringTrigger("Highlight stuff", "General", {"pixie"},
[[selectString(line, 1) bg("yellow") resetFormat()]])
-- Or another trigger to highlight several different things
permSubstringTrigger("Highlight stuff", "General", {"pixie", "cat", "dog", "rabbit"},
[[selectString(line, 1) fg ("blue") bg("yellow") resetFormat()]])
Note: Mudlet by design allows duplicate names - so calling permSubstringTrigger with the same name will keep creating new triggers. You can check if a trigger already exists with the exists() function.
permTimer
- permTimer(name, parent, seconds, lua code)
- Creates a persistent timer that stays after Mudlet is restarted and shows up in the Script Editor.
- Parameters
- name
- name of the timer.
- parent
- name of the timer group you want the timer to go in.
- seconds
- a floating point number specifying a delay in seconds after which the timer will do the lua code (stored as the timer's "script") you give it as a string.
- lua code is the code with string you are doing this to.
- Returns
- a unique id number for that timer.
- Example
-- create a timer in the "first timer group" group
permTimer("my timer", "first timer group", 4.5, [[send ("my timer that's in my first timer group fired!")]])
-- create a timer that's not in any group; just at the top
permTimer("my timer", "", 4.5, [[send ("my timer that's in my first timer group fired!")]])
-- enable timer - they start off disabled until you're ready
enableTimer("my timer")
Note: The timer is NOT active after creation, it will need to be enabled by a call to enableTimer() before it starts.
Note: Mudlet by design allows duplicate names - so calling permTimer with the same name will keep creating new timers. You can check if a timer already exists with the exists() function.
permKey
- permKey(name, parent, [modifier], key code, lua code)
- Creates a persistent key that stays after Mudlet is restarted and shows up in the Script Editor.
- Parameters
- name
- name of the key.
- parent
- name of the timer group you want the timer to go in or "" for the top level.
- modifier
- (optional) modifier to use - can be one of mudlet.keymodifier.Control, mudlet.keymodifier.Alt, mudlet.keymodifier.Shift, mudlet.keymodifier.Meta, mudlet.keymodifier.Keypad, or mudlet.keymodifier.GroupSwitch or the default value of mudlet.keymodifier.None which is used if the argument is omitted. To use multiple modifiers, add them together: (mudlet.keymodifier.Shift + mudlet.keymodifier.Control)
- key code
- actual key to use - one of the values available in mudlet.key, e.g. mudlet.key.Escape, mudlet.key.Tab, mudlet.key.F1, mudlet.key.A, and so on. The list is a bit long to list out in full so it is available here.
- lua code'
- code you would like the key to run.
- Returns
- a unique id number for that key.
Note: Available in Mudlet 3.2+
- Example
-- create a key at the top level for F8
permKey("my key", "", mudlet.key.F8, [[echo("hey this thing actually works!\n")]])
-- or Ctrl+F8
permKey("my key", "", mudlet.keymodifier.Control, mudlet.key.F8, [[echo("hey this thing actually works!\n")]])
-- Ctrl+Shift+W
permKey("jump keybinding", "", mudlet.keymodifier.Control + mudlet.keymodifier.Shift, mudlet.key.W, [[send("jump")]])
Note: Mudlet by design allows duplicate names - so calling permKey with the same name will keep creating new keys. You can check if a key already exists with the exists() function. The key will be created even if the lua code does not compile correctly - but this will be apparent as it will be seen in the Editor.
printCmdLine
- printCmdLine(text)
- Replaces the current text in the input line, and sets it to the given text.
printCmdLine("say I'd like to buy ")
raiseEvent
- raiseEvent(event_name, arg-1, … arg-n)
- Raises the event event_name. The event system will call the main function (the one that is called exactly like the script name) of all such scripts in this profile that have registered event handlers. If an event is raised, but no event handler scripts have been registered with the event system, the event is ignored and nothing happens. This is convenient as you can raise events in your triggers, timers, scripts etc. without having to care if the actual event handling has been implemented yet - or more specifically how it is implemented. Your triggers raise an event to tell the system that they have detected a certain condition to be true or that a certain event has happened. How - and if - the system is going to respond to this event is up to the system and your trigger scripts don’t have to care about such details. For small systems it will be more convenient to use regular function calls instead of events, however, the more complicated your system will get, the more important events will become because they help reduce complexity very much.
- The corresponding event handlers that listen to the events raised with raiseEvent() need to use the script name as function name and take the correct number of arguments.
Note: handles nil and boolean arguments since Mudlet 3.0.
- Example
- raiseEvent("fight") a correct event handler function would be: myScript( event_name ). In this example raiseEvent uses minimal arguments, name the event name. There can only be one event handler function per script, but a script can still handle multiple events as the first argument is always the event name - so you can call your own special handlers for individual events. The reason behind this is that you should rather use many individual scripts instead of one huge script that has all your function code etc. Scripts can be organized very well in trees and thus help reduce complexity on large systems.
Where the number of arguments that your event may receive is not fixed you can use ... as the last argument in the function declaration to handle any number of arguments. For example:
-- try this function out with "lua myscripthandler(1,2,3,4)"
function myscripthandler(a, b, ...)
print("Arguments a and b are: ", a,b)
-- save the rest of the arguments into a table
local otherarguments = {...}
print("Rest of the arguments are:")
display(otherarguments)
-- access specific otherarguments:
print("First and second otherarguments are: ", otherarguments[1], otherarguments[2])
end
raiseGlobalEvent
- raiseGlobalEvent(event_name, arg-1, … arg-n)
- Like raiseEvent() this raises the event "event_name", but this event is sent to all other opened profiles instead. The profiles receive the event in circular alphabetical order (if profile "C" raised this event and we have profiles "A", "C", and "E", the order is "E" -> "A", but if "E" raised the event the order would be "A" -> "C"); execution control is handed to the receiving profiles so that means that long running events may lock up the profile that raised the event.
- The sending profile's name is automatically appended as the last argument to the event.
- Example
-- from profile called "My MUD" this raises an event "my event" with additional arguments 1, 2, 3, "My MUD" to all profiles except the original one
raiseGlobalEvent("my event", 1, 2, 3)
Note: Available since Mudlet 3.1.0.
Tip: you might want to add the profile name to your plain raiseEvent() arguments. This'll help you tell which profile your event came from similar to raiseGlobalEvent().
resetStopWatch
- resetStopWatch(watchID)
- This function resets the time to 0:0:0.0, but does not start the stop watch. You can start it with startStopWatch → createStopWatch
setConsoleBufferSize
- setConsoleBufferSize(consoleName, linesLimit, sizeOfBatchDeletion)
- Sets the maximum number of lines a buffer (main window or a miniconsole) can hold. Default is 10,000.
- Parameters
- consoleName:
- The name of the window
- linesLimit:
- Sets the amount of lines the buffer should have.
Note: Mudlet performs extremely efficiently with even huge numbers, so your only limitation is your computers memory (RAM).
- sizeOfBatchDeletion:
- Specifies how many lines should Mudlet delete at once when you go over the limit - it does it in bulk because it's efficient to do so.
- Example
-- sets the main windows size to 5 million lines maximum - which is more than enough!
setConsoleBufferSize("main", 5000000, 1000)
setTriggerStayOpen
- setTriggerStayOpen(name, number)
- Sets for how many more lines a trigger script should fire or a chain should stay open after the trigger has matched - so this allows you to extend or shorten the fire length of a trigger. The main use of this function is to close a chain when a certain condition has been met.
- Parameters
- name: The name of the trigger which has a fire length set (and which opens the chain).
- number: 0 to close the chain, or a positive number to keep the chain open that much longer.
- Examples
-- if you have a trigger that opens a chain (has some fire length) and you'd like it to be closed
-- on the next prompt, you could make a trigger inside the chain with a Lua function pattern of:
return isPrompt()
-- and a script of:
setTriggerStayOpen("Parent trigger name", 0)
-- to close it on the prompt!
startStopWatch
- startStopWatch( watchID )
- Starts the stop watch. Stopwatches can be stopped and re-started any number of times.
- See also: createStopWatch(), stopStopWatch()
- Parameters
- watchID: The stopwatch ID you get with createStopWatch().
- Examples
-- this is a common pattern for re-using stopwatches and starting them:
watch = watch or createStopWatch()
startStopWatch(watch)
stopStopWatch
- stopStopWatch( watchID )
- Stops the stop watch and returns the elapsed time in milliseconds in form of 0.001. You can also retrieve the current time without stopping the stopwatch with getStopWatchTime().
- Returns time as a number.
- See also: createStopWatch(), getStopWatchTime()
- Parameters
- watchID: The stopwatch ID you get with createStopWatch().
- Examples
-- this is a common pattern for re-using stopwatches and starting them:
watch = watch or createStopWatch()
startStopWatch(watch)
-- do some long-running code here ...
print("The code took: "..stopStopWatch(watch).."s to run.")
Note: The current implementation is broken in that it is NOT possible to stop a stopwatch - it behaves the same as the getStopWatchTime() command.
tempAlias
- aliasID = tempAlias(regex, code to do)
- Creates a temporary alias - temporary in the sense that it won't be saved when Mudlet restarts (unless you re-create it). The alias will go off as many times as it matches, it is not a one-shot alias. The function returns an ID for subsequent enableAlias(), disableAlias() and killAlias() calls.
- Parameters
- regex: Alias pattern in regex.
- code to do: The code to do when the alias fires - wrap it in [[ ]].
- Examples
myaliasID = tempAlias("^hi$", [[send ("hi") echo ("we said hi!")]])
-- you can also delete the alias later with:
killAlias(myaliasID)
tempBeginOfLineTrigger
- tempBeginOfLineTrigger(part of line, code, expireAfter)
- Creates a trigger that will go off whenever the part of line it's provided with matches the line right from the start (doesn't matter what the line ends with). The trigger is temporary in the sense that it won't stay when you close Mudlet, and it will go off multiple times until you disable or destroy it. You can also make it be temporary and self-delete after a number of matches with the expireAfter parameter. The function returns the trigger ID for subsequent enableTrigger(), disableTrigger() and killTrigger() calls.
- Parameters
- part of line: start of the line that you'd like to match. This can also be regex.
- code to do: code to do when the trigger runs - wrap it in [[ ]], or give it a Lua function (since Mudlet 3.5.0).
- expireAfter: Delete trigger after a specified number of matches (since Mudlet 3.11). You can make a trigger match not count towards expiration by returning true after it fires.
- Examples
mytriggerID = tempBeginOfLineTrigger("Hello", [[echo("We matched!")]])
--[[ now this trigger will match on any of these lines:
Hello
Hello!
Hello, Bob!
but not on:
Oh, Hello
Oh, Hello!
]]
-- or as a function:
mytriggerID = tempBeginOfLineTrigger("Hello", function() echo("We matched!") end)
-- you can make the trigger match only a certain amount of times as well, 5 in this example:
tempBeginOfLineTrigger("This is the start of the line", function() echo("We matched!") end, 5)
-- if you want a trigger match not to count towards expiration, return true. In this example it'll match 5 times unless the line is "Start of line and this is the end."
tempBeginOfLineTrigger("Start of line",
function()
if line == "Start of line and this is the end." then
return true
else
return false
end
end, 5)
tempButton
- tempButton(toolbar name, button text, orientation)
- Creates a temporary button. Temporary means, it will disappear when Mudlet is closed.
- Parameters
- toolbar name: The name of the toolbar to place the button into.
- button text: The text to display on the button.
- orientation: a number 0 or 1 where 0 means horizontal orientation for the button and 1 means vertical orientation for the button. This becomes important when you want to have more than one button in the same toolbar.
- Example
-- makes a temporary toolbar with two buttons at the top of the main Mudlet window
lua tempButtonToolbar("topToolbar", 0, 0)
lua tempButton("topToolbar", "leftButton", 0)
lua tempButton("topToolbar", "rightButton", 0)
tempButtonToolbar
- tempButtonToolbar(name, location, orientation)
- Creates a temporary button toolbar. Temporary means, it will disappear when Mudlet is closed.
- Parameters
- name: The name of the toolbar.
- location: a number from 0 to 4, where 0 means "at the top of the screen", 1 means "left side", 2 means "right side" and 3 means "in a window of its own" which can be dragged and attached to the main Mudlet window if needed.
- orientation: a number 0 or 1, where 0 means horizontal orientation for the toolbar and 1 means vertical orientation for the toolbar. This becomes important when you want to have more than one toolbar in the same location of the window.
- Example
-- makes a temporary toolbar with two buttons at the top of the main Mudlet window
lua tempButtonToolbar("topToolbar", 0, 0)
lua tempButton("topToolbar", "leftButton", 0)
lua tempButton("topToolbar", "rightButton", 0)
tempColorTrigger
- tempColorTrigger(foregroundColor, backgroundColor, code, expireAfter)
- Makes a color trigger that triggers on the specified foreground and background color. Both colors need to be supplied in form of these simplified ANSI 16 color mode codes. The function returns the trigger ID for subsequent enableTrigger(), disableTrigger() and killTrigger() calls. The trigger is temporary in the sense that it won't stay when you close Mudlet, and it will go off multiple times until you disable or destroy it. You can also make it be temporary and self-delete after a number of matches with the expireAfter parameter.
- Parameters
- foregroundColor: The foreground color you'd like to trigger on.
- backgroundColor: The background color you'd like to trigger on.
- code to do: The code to do when the trigger runs - wrap it in [[ ]], or give it a Lua function (since Mudlet 3.5.0).
- expireAfter: Delete trigger after a specified number of matches (since Mudlet 3.11). You can make a trigger match not count towards expiration by returning true after it fires.
- Color codes
0 = default text color
1 = light black
2 = dark black
3 = light red
4 = dark red
5 = light green
6 = dark green
7 = light yellow
8 = dark yellow
9 = light blue
10 = dark blue
11 = light magenta
12 = dark magenta
13 = light cyan
14 = dark cyan
15 = light white
16 = dark white
- Examples
-- This script will re-highlight all text in blue foreground colors on a black background with a red foreground color
-- on a blue background color until another color in the current line is being met. temporary color triggers do not
-- offer match_all or filter options like the GUI color triggers because this is rarely necessary for scripting.
-- A common usage for temporary color triggers is to schedule actions on the basis of forthcoming text colors in a particular context.
tempColorTrigger(9, 2, [[selectString(matches[1],1) fg("red") bg("blue")]])
-- or:
tempColorTrigger(9, 2, function()
selectString(matches[1], 1)
fg("red")
bg("blue")
end)
-- match the trigger only 4 times
tempColorTrigger(9, 2, [[selectString(matches[1],1) fg("red") bg("blue")]], 4)
tempComplexRegexTrigger
- tempComplexRegexTrigger(name, regex, code, multiline, foreground color, bg color, filter, match all, highlight foreground color, highlight background color, play sound file, fire length, line delta, expireAfter)
- Allows you to create a temporary trigger in Mudlet, using any of the UI-available options. The trigger is temporary in the sense that it won't stay when you close Mudlet, and it will go off multiple times until you disable or destroy it. You can also make it be temporary and self-delete after a number of matches with the expireAfter parameter.
- Returns the trigger name, that you can use killTrigger() later on with.
- code to do: code to do when the trigger runs - wrap it in [[ ]], or give it a Lua function (since Mudlet 3.5.0).
- expireAfter: Delete trigger after a specified number of matches (since Mudlet 3.11). You can make a trigger match not count towards expiration by returning true after it fires.
- For the multiline and so on options, use a 0 if you don't want those options. Otherwise enter the value you want to use.
- For color, you need to provide both fg and bg together.
- For making multiline triggers, use the same trigger name an options, providing the new pattern to add:
tempComplexRegexTrigger("testTrigger", "first pattern", [[echo("it went")]], 1, 0, 0....)
tempComplexRegexTrigger("testTrigger", "second pattern", [[echo("it went")]], 1, 0, 0....)
- That will make 1 trigger with both regex patterns. Note only one script will ever be used (the last one set).
tempExactMatchTrigger
- tempExactMatchTrigger(exact line, code, expireAfter)
- Creates a trigger that will go off whenever the line from the game matches the provided line exactly (ends the same, starts the same, and looks the same). You don't need to use any of the regex symbols here (^ and $). The function returns the trigger ID for subsequent enableTrigger(), disableTrigger() and killTrigger() calls. The trigger is temporary in the sense that it won't stay when you close Mudlet, and it will go off multiple times until you disable or destroy it. You can also make it be temporary and self-delete after a number of matches with the expireAfter parameter.
- Parameters
- exact line: exact line you'd like to match.
- code: code to do when the trigger runs - wrap it in [[ ]], or give it a Lua function (since Mudlet 3.5.0).
- expireAfter: Delete trigger after a specified number of matches (since Mudlet 3.11). You can make a trigger match not count towards expiration by returning true after it fires.
- Examples
mytriggerID = tempExactMatchTrigger("You have recovered balance on all limbs.", [[echo("We matched!")]])
-- as a function:
mytriggerID = tempExactMatchTrigger("You have recovered balance on all limbs.", function() echo("We matched!") end)
-- expire after 4 matches:
tempExactMatchTrigger("You have recovered balance on all limbs.", function() echo("Got balance back!\n") end, 4)
-- you can also avoid expiration by returning true:
tempExactMatchTrigger("You have recovered balance on all limbs.", function() echo("Got balance back!\n") return true end, 4)
tempKey
- tempKey([modifier], key code, lua code)
- Creates a keybinding. This keybinding isn't temporary in the sense that it'll go off only once (it'll go off as often as you use it), but rather it won't be saved when Mudlet is closed.
- modifier
- (optional) modifier to use - can be one of mudlet.keymodifier.Control, mudlet.keymodifier.Alt, mudlet.keymodifier.Shift, mudlet.keymodifier.Meta, mudlet.keymodifier.Keypad, or mudlet.keymodifier.GroupSwitch or the default value of mudlet.keymodifier.None which is used if the argument is omitted. To use multiple modifiers, add them together: (mudlet.keymodifier.Shift + mudlet.keymodifier.Control)
- key code
- actual key to use - one of the values available in mudlet.key, e.g. mudlet.key.Escape, mudlet.key.Tab, mudlet.key.F1, mudlet.key.A, and so on. The list is a bit long to list out in full so it is available here.
- lua code'
- code you'd like the key to run - wrap it in [[ ]].
- Returns
- a unique id number for that key.
- Examples
local keyID = tempKey(mudlet.key.F8, [[echo("hi")]])
local anotherKeyID = tempKey(mudlet.keymodifier.Control, mudlet.key.F8, [[echo("hello")]])
-- bind Ctrl+Shift+W:
tempKey(mudlet.keymodifier.Control + mudlet.keymodifier.Shift, mudlet.key.W, [[send("jump")]])
-- delete it if you don't like it anymore
killKey(keyID)
tempLineTrigger
- tempLineTrigger(from, howMany, code, expireAfter)
- Temporary trigger that will fire on n consecutive lines following the current line. This is useful to parse output that is known to arrive in a certain line margin or to delete unwanted output from the MUD - the trigger does not require any patterns to match on. The function returns the trigger ID for subsequent enableTrigger(), disableTrigger() and killTrigger() calls. The trigger is temporary in the sense that it won't stay when you close Mudlet, and it will go off multiple times until you disable or destroy it. You can also make it be temporary and self-delete after a number of matches with the expireAfter parameter.
- Parameters
- from: from which line after this one should the trigger activate - 1 will activate right on the next line.
- howMany: how many lines to run for after the trigger has activated.
- code: code to do when the trigger runs - wrap it in [[ ]], or give it a Lua function (since Mudlet 3.5.0).
- expireAfter: Delete trigger after a specified number of matches (since Mudlet 3.11). You can make a trigger match not count towards expiration by returning true after it fires.
- Example
-- Will fire 3 times with the next line from the MUD.
tempLineTrigger(1, 3, function() print(" trigger matched!") end)
-- Will fire 20 lines after the current line and fire twice on 2 consecutive lines, 7 times.
tempLineTrigger(20, 2, function() print(" trigger matched!") end, 7)
tempPromptTrigger
- tempPromptTrigger(code, expireAfter)
- Temporary trigger that will match on the games prompt. The function returns the trigger ID for subsequent enableTrigger(), disableTrigger() and killTrigger() calls. The trigger is temporary in the sense that it won't stay when you close Mudlet, and it will go off multiple times until you disable or destroy it. You can also make it be temporary and self-delete after a number of matches with the expireAfter parameter.
Note: If the trigger is not working, check that the N: bottom-right has a number. This feature requires telnet Go-Ahead to be enabled in the game. Available in Mudlet 3.6+
- Parameters
- code: code to do when the trigger runs - wrap it in [[ ]], or give it a Lua function.
- expireAfter: Delete trigger after a specified number of matches. You can make a trigger match not count towards expiration by returning true after it fires.
Note: expireAfter is available since Mudlet 3.11
- Example
tempPromptTrigger(function()
echo("hello! this is a prompt!")
end)
-- match only 2 times:
tempPromptTrigger(function()
echo("hello! this is a prompt!")
end, 2)
-- match only 2 times, unless the prompt is "55 health."
tempPromptTrigger(function()
if line == "55 health." then return true end
end, 2)
tempRegexTrigger
- tempRegexTrigger(regex, code, expireAfter)
- Creates a temporary regex trigger that executes the code whenever it matches. The function returns the trigger ID for subsequent enableTrigger(), disableTrigger() and killTrigger() calls. The trigger is temporary in the sense that it won't stay when you close Mudlet, and it will go off multiple times until you disable or destroy it. You can also make it be temporary and self-delete after a number of matches with the expireAfter parameter.
- Parameters
- regex: regular expression that lines will be matched on.
- code: code to do when the trigger runs - wrap it in [[ ]], or give it a Lua function (since Mudlet 3.5.0).
- expireAfter: Delete trigger after a specified number of matches (since Mudlet 3.11). You can make a trigger match not count towards expiration by returning true after it fires.
- Examples
-- create a non-duplicate trigger that matches on any line and calls a function
html5log = html5log or {}
if html5log.trig then killTrigger(html5log.trig) end
html5log.trig = tempRegexTrigger("^", [[html5log.recordline()]])
-- or a simpler variant:
html5log.trig = tempRegexTrigger("^", html5log.recordline)
-- only match 3 times:
tempRegexTrigger("^You prick (.+) twice in rapid succession with", function() echo("Hit "..matches[2].."!\n") end, 3)
tempTimer
- tempTimer(time, code to do)
- Creates a temporary one-shot timer and returns the timer ID, which you can use with enableTimer(), disableTimer() and killTimer() functions. You can use 2.3 seconds or 0.45 etc. After it has fired, the timer will be deactivated and destroyed, so it will only go off once. Here is a more detailed introduction to tempTimer.
- Parameters
- time: The time in seconds for which to set the timer for - you can use decimals here for precision. The timer will go off x given seconds after you make it.
- code to do: The code to do when the timer is up - wrap it in [[ ]], or provide a Lua function.
- Examples
-- wait half a second and then run the command
tempTimer( 0.5, [[send("kill monster")]] )
-- or an another example - two ways to 'embed' variable in a code for later:
local name = matches[2]
tempTimer(2, [[send("hello, ]]..name..[[ !")]])
-- or:
tempTimer(2, function()
send("hello, "..name)
end)
Note: Double brackets, e.g: [[ ]] can be used to quote strings in Lua. The difference to the usual `" " quote syntax is that `[[ ]] also accepts the character ". Consequently, you don’t have to escape the " character in the above script. The other advantage is that it can be used as a multiline quote, so your script can span several lines.
Note: Lua code that you provide as an argument is compiled from a string value when the timer fires. This means that if you want to pass any parameters by value e.g. you want to make a function call that uses the value of your variable myGold as a parameter you have to do things like this:
tempTimer( 3.8, [[echo("at the time of the tempTimer call I had ]] .. myGold .. [[ gold.")]] )
-- tempTimer also accepts functions (and thus closures) - which can be an easier way to embed variables and make the code for timers look less messy:
local variable = matches[2]
tempTimer(3, function () send("hello, " .. variable) end)
tempTrigger
- tempTrigger(substring, code, expireAfter)
- Creates a substring trigger that executes the code whenever it matches. The function returns the trigger ID for subsequent enableTrigger(), disableTrigger() and killTrigger() calls. The trigger is temporary in the sense that it won't stay when you close Mudlet, and it will go off multiple times until you disable or destroy it. You can also make it be temporary and self-delete after a number of matches with the expireAfter parameter.
- Parameters
- substring: The substring to look for - this means a part of the line. If your provided text matches anywhere within the line from the game, the trigger will go off.
- code: The code to do when the trigger runs - wrap it in [[ ]], or give it a Lua function (since Mudlet 3.5)
- expireAfter: Delete trigger after a specified number of matches (since Mudlet 3.11). You can make a trigger match not count towards expiration by returning true after it fires.
Example:
-- this example will highlight the contents of the "target" variable.
-- it will also delete the previous trigger it made when you call it again, so you're only ever highlighting one name
if id then killTrigger(id) end
id = tempTrigger(target, [[selectString("]] .. target .. [[", 1) fg("gold") resetFormat()]])
-- you can also write the same line as:
id = tempTrigger(target, function() selectString(target, 1) fg("gold") resetFormat() end)
-- or like so if you have a highlightTarget() function somewhere
id = tempTrigger(target, highlightTarget)
-- a simpler trigger to replace "hi" with "bye" whenever you see it
tempTrigger("hi", [[selectString("hi", 1) replace("bye")]])
-- this trigger will go off only 2 times
tempTrigger("hi", function() selectString("hi", 1) replace("bye") end, 2)
-- table to store our trigger IDs in
nameIDs = nameIDs or {}
-- delete any existing triggers we've already got
for _, id in ipairs(nameIDs) do killTrigger(id) end
-- create new ones, avoiding lots of ]] [[ to embed the name
for _, name in ipairs{"Alice", "Ashley", "Aaron"} do
nameIDs[#nameIDs+1] = tempTrigger(name, function() print(" Found "..name.."!") end)
end