Difference between revisions of "Manual:Migrating"
Line 306: | Line 306: | ||
To make a function, you go to the Scripts section, create a new script, and write in your function: | To make a function, you go to the Scripts section, create a new script, and write in your function: | ||
− | function <name> () | + | function <name> () |
<stuff it does> | <stuff it does> | ||
− | end | + | end |
For example, if we want to make a function that does two things for us at once, we’d do this: | For example, if we want to make a function that does two things for us at once, we’d do this: | ||
− | function eat_bloodroot() | + | function eat_bloodroot() |
send ("outr bloodroot") | send ("outr bloodroot") | ||
send ("eat bloodroot") | send ("eat bloodroot") | ||
− | end | + | end |
Then just do this in our alias or trigger to outr and eat bloodroot: | Then just do this in our alias or trigger to outr and eat bloodroot: | ||
− | eat_bloodroot() | + | eat_bloodroot() |
As mentioned, you can also give things to functions - in this case lets expand the function to eat any herb for us we tell it: | As mentioned, you can also give things to functions - in this case lets expand the function to eat any herb for us we tell it: | ||
− | function eat (what) | + | function eat (what) |
send ("outr " .. what) | send ("outr " .. what) | ||
send ("eat " .. what) | send ("eat " .. what) | ||
− | end | + | end |
+ | [...] | ||
+ | eat ("bloodroot") | ||
+ | eat ("kelp") | ||
+ | eat ("ginseng") | ||
− | + | Lastly, functions can also give you data back. One useful example for this is to break down tasks into functions, which will help your code readability: | |
− | |||
− | |||
− | |||
− | + | function can_i_stand() | |
− | |||
− | function can_i_stand() | ||
if not paralyzed and not prone and not stunned then | if not paralyzed and not prone and not stunned then | ||
− | + | return true | |
else | else | ||
− | + | return false | |
− | end | + | end |
− | + | [...] | |
− | [...] | + | if can_i_stand() and have_balance and have_equilibrium then |
− | |||
− | if can_i_stand() and have_balance and have_equilibrium then | ||
send ("stand") | send ("stand") | ||
− | end | + | end |
===Notes to be aware of=== | ===Notes to be aware of=== |
Revision as of 02:11, 11 May 2011
Migrating to Mudlet
From Nexus
Trigger patterns
Lets start with triggers. For translating Nexus patterns into Mudlet, use the following table below. Make sure to set the pattern type in Mudlet to be perl regex, because the default substring works differently.
Mudlet Nexus What it does (\w+) {w} Match one or more non-whitespace characters (a 'word') (\d+) {d} Match one or more numeric digits (a number) ([abc]) {[abc]} Match one or more of either 'a', 'b' or 'c'. (a character class) ([^abc]) {[^abc]} Match one or more of anything EXCEPT 'a', 'b', or 'c' (.*) {*} Match zero or more characters (anything) ^ {<} Match the beginning of a line (not the actual newline character) $ {>} Match the end of a line (not the actual newline character)
Note If you just have a pattern with a {<} and {>} and nothing else, copy the pattern over without the {<}{>} and select the exact match pattern type. For example {<}You sit down.{>} in Nexus would become You sit down. in Mudlet with the pattern type of exact match. This is easier to read and matches way faster!
Basic scripting
The Nexus way of doing a function is like this: #function stuff it does. In Mudlet, it’s function(stuff it does). Note that if you’d like to give text to a function, you put it inside double or single quotes.
Nexus
- send hi
- echo Hello to me!
Mudlet
send ("hi") echo ("Hello to me!")
If you’d like to use a variable inside text, you’d put it outside the quotes and glue it together with two dots.
Nexus
- set number 1234
- echo My number is: $number
- echo the $number is in the middle of my text
Mudlet
number = 1234 echo ("My number is: " .. number) echo ("the " .. number .. " is in the middle of my text")
wildcards in Mudlet
To use the wildcard variable in Nexus, you’d use $1 for the first match, $2 for the second and so on. In Mudlet, the matches[] table contains the matches - and it starts placing the matches from 2. So the Nexus $1 would be matches[2], $2 would be matches[3].
Nexus: {w} kicks you.
- set badguy $1
- echo $badguy kicked me!
Mudlet: (\w+) kicks you\. badguy = matches[2] echo(badguy .. " kicked me!")
Another example would be the targetting alias:
Nexus: alias name: t alias script: #set target $1
Mudlet: alias pattern: ^t (.*)$ alias script: target = matches[2]
The reason the first match goes into matches[2] and not matches[1] is because matches[1] contains the part of the line that matched your trigger / alias.
#wait in Mudlet
In Nexus, a #wait will freeze the script while it’s executing - such that commands after it are delayed. In Mudlet, we use tempTimer which that works a bit differently - it doesn’t freeze the script, but instead makes it so commands a tempTimer is asked to do will get done in the future.
So the difference is that a tempTimer doesn’t freeze the commands following it at all, everything gets done at once as usual. Just things a tempTimer was asked to do get done in the future.
Nexus script:
- send hello
- wait 1000
- send hi
- wait 500
- send bye
Mudlet: send("hello") tempTimer(1, send("hi")) tempTimer(1.5, send("bye"))
ifs in Mudlet
Next are the #if statements. Here’s a table comparing the syntaxes of Nexus and Mudlet: Mudlet Nexus
if <condition> then <text> end
- if <condition> <text>
- if <condition> { <script> }
if <condition> then <script> else <script> end
- if <condition> { <script> } else { <script> }
- if <condition> { <script> } { <script> }
if <condition> then <script> elseif <condition> then <script> end
- if <condition> { <script> } elsif <condition> { <script> }
if <condition> then <script> elseif <condition> then <script> else <script> end
- if <condition> { <script> } elsif <condition> { <script> } else { <script> }
Here is the sample side-by-side comparison of how you’d use it: Mudlet Nexus
-- If foo is true, echo that fact.
if foo then echo ("Foo is true!") end -- If foo is true, echo that fact.
-- A slight modification of the above that illustrates an 'else' clause. -- Note that the 'then' is always necessary to avoid confusion.
if foo then echo ("Foo is true!") else echo ("Foo is false!") end
-- Illustration of putting more than one statement in a block (also -- spans lines here). Note the if doesn't end until the last 'end' is -- encountered. -- We also add a \n at the end of multiple echos so each one is on it's own line.
if foo then echo ("Foo is true!\n") echo ("Isn't it grand?\n") echo ("These commands are all on separate lines too!\n") end
-- This shows how ifs continue (on 'logical' lines) even though the -- blocks in its constituent clauses may have multiple lines. The -- following lines are all _one_ single if statement.
if foo > 50 then echo ("Foo is big!\nYes it is.") elseif foo > 10 then echo ("Foo is pretty big...\n") echo ("I've seen bigger.") else echo ("Foo is actually kind of small.") end
-- Ifs can be nested too.
if foo then if bar then echo ("Both foo and bar are true.") else echo ("Foo's true, but bar isn't.") end end
// If $foo is true, echo that fact.
#if $foo #echo Foo is true! // If $foo is true, echo that fact.
// A slight modification of the above that illustrates an 'else' clause. // The second form shows that the word 'else' is actually optional too. // (The two lines are functionally the same.)
#if $foo { #echo Foo is true! } else { #echo Foo is false! } #if $foo { #echo Foo is true! } { #echo Foo is false! }
// Illustration of putting more than one statement in a block (also // spans lines here). Note the if doesn't end until the last '}' is // encountered.
#if $foo { #echo Foo is true! #echo Isn't it grand? #echo These commands are all on separate lines too! }
// This shows how ifs continue (on 'logical' lines) even though the // blocks in its constituent clauses may have multiple lines. The // following lines are all _one_ single if statement.
#if $foo > 50 { #echo Foo is big! #echo Yes it is. } elsif $foo > 10 { #echo Foo is pretty big... #echo I've seen bigger. } else { #echo Foo is actually kind of small. }
// Ifs can be nested too.
#if $foo { #if $bar { #echo Both foo and bar are true. } else { #echo Foo's true, but bar isn't. } }
Mudlet equivalents of Nexus functions
Now that we got the ifs covered, lets go over the Mudlet equivalents of other Nexus functions. Note that this is a direct Nexus→Mudlet comparison; certain functions in Mudlet have much more capability. Mudlet Nexus
<variable> = <variablename> <+ - / *> <number or variable>
- add <variablename> <expression>
echo "text\n" or echo("text\n")
- echo <text>
echo "text" or echo("text")
- echo_ <text>
enableAlias("<group name>"), enableTrigger(), enableKey() or enableTimer()
- groupon <group name>
disableAlias("<group name>"), disableTrigger(), disableKey() or disableTimer()
- groupoff <group name>
selectString (line, 1) bg("<color>") resetFormat()
- highlight <color>
deleteLine()
- gag
openUrl("<url>")*
- openurl <url>
send("<stuff>")
- send <text>
sendAll("hi", "hello", "bye")
- send_ hi #send_ hello #send bye
<variable name> = "<text value>"
- set <variable name> <text value>
<variable name> = <expression>
- set <variable name> = <expression>
<variable name> = nil
- unset <variable name>
tempTimer(
- wait <milliseconds>
- Planned for Mudlet 1.0.6
How to call aliases in Mudlet
The typical workflow in Nexus is that you’d define the aliases for yourself to use, and also use aliases for system-related things (things like checking if you’d need to eat a herb, etc).
While the first use is good, the second isn’t - because the user can also run this alias at an inopportune time, because if you want to call an alias it has to match all alises again, and so on.
Now, while in Mudlet you can call another alias with the expandAlias() function, this is strongly discouraged. What you do instead if create a function (for example, send() and echo() are functions) that you can then call from your alias or trigger. This has many advantages - it’s faster, you can easily give your function values, and your function can return you values, too.
To make a function, you go to the Scripts section, create a new script, and write in your function:
function <name> () <stuff it does> end
For example, if we want to make a function that does two things for us at once, we’d do this:
function eat_bloodroot() send ("outr bloodroot") send ("eat bloodroot") end
Then just do this in our alias or trigger to outr and eat bloodroot:
eat_bloodroot()
As mentioned, you can also give things to functions - in this case lets expand the function to eat any herb for us we tell it:
function eat (what) send ("outr " .. what) send ("eat " .. what) end [...] eat ("bloodroot") eat ("kelp") eat ("ginseng")
Lastly, functions can also give you data back. One useful example for this is to break down tasks into functions, which will help your code readability:
function can_i_stand() if not paralyzed and not prone and not stunned then return true else return false end [...] if can_i_stand() and have_balance and have_equilibrium then send ("stand") end
Notes to be aware of
To finish off, a couple of points that should be remembered:
Mudlet does profile snapshots (nexus archives) automatically - to load a different one, select it from the list when connecting
Mudlet can import and export xml
Mudlet and Nexus xml formats aren’t compatible
Mudlet can do nexus graphics, here is a package for - Achaea
Mudlet has a ton more features such are more speed, less bugs, copy from the normal window, replay logging, color html logging, more powerful scripting, and the list goes on.