All of this information, and more, can be found across Mudlet's wiki, manual, and lua tutorials. This is just my submitting what I know to the public. It may be difficult to understand, or missing information, but it's something. Probably more accurate to call it "scripting," but, meh. If anyone's really grateful I accept most forms of 'thank you'.
What you should know before reading further:
- What a trigger is
- What an alias is
- What a script is
- How to make or delete each of the above
- Basic algebra and PEMDAS
Coding Basics - Datatypes
There are a number of different data types, but for the sake of this post, we're going to focus on just a few within Mudlet:
- Numbers: For those who are unfamiliar with programming, any real, rational number. For those who are, it's the unholy lovechild of a float and integer.
- Strings: One or more characters.
- ""
- " "
- "apple"
- "A sentence with multiple words."
- "4ctu4lly, 4nyth1ng 1n btwn qwotz!"
- Booleans: True or false.
- Nil values: Lack of a useful value, great as a placeholder.
- Tables: A collection of values
- {1,12,123,1234}
- {"a","ab","abc","abcd"}
- {1234, "abc", 12, "a"}
- {["letters"] = {"a", "b", "c"}, ["numbers"] = {1,2,3}}
- {nil, nil, nil, nil}
- Functions: Code to be performed, either on its own, on a variable, or on a collection of variables.
Variables are user defined and should avoid being named the same as predefined variables (like if or else or or or and). For those with previous programming experience, there is no need to declare the datatype when creating a variable. There is also no use of semi-colons; Mudlet's coding is semi-whitespace. Examples of variables in use:
intelligence = 2
farmerjoe = "little ol' Farmer Joe"
amDying = false
cheeses = {"cream", "feta", "muenster", "swiss", "american"}
Important note on tables: the first indexing value of a table is 1 (for those with programming experience, this may be hard to get used to initially; 1, not 0, 1!)
Coding Basics - Conditionals and Loops
Well, that's great, you can make variables and sort of understand the data types behind them! Congratulations! But... they don't... do much, do they. They're just there. Blah. But Lusternia isn't stationary.
In fact, you may opt that you want to do a certain thing if a variable is a certain value, or do another thing if your variable is another value, or do something else completely with the variable if it's neither of those. For you, we have the conditionals!
Conditionals
== is equivalent to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
Standard form of a conditional is:
if someConditionIsTrue then
someCode
end
Let's say you have a variable called age with a value of 5.
if age < 21 then
echo("No alcohol for you!")
end
The above code, when run, will echo "No alcohol for you!" onto your screen. (Damn straight too, no drinking babies in Lusternia!)
But let's say you want the code to echo something else, if you're old enough to drink.
if age < 21 then
echo("No alcohol for you!")
else
echo("Drink it up!")
end
So, if age has a value of 21 or higher, the code will display, "Drink it up!" Great, right?
But what if you set age's value on accident to "bljeriy3qyhjgnbjnbf"? Indeed, you might have been drunk when trying to enter the value. The aforementioned piece of code would tell you to keep drinking... which is a TERRIBLE TERRIBLE IDEA. This is where elseif comes in, working like if does.
if age < 21 then
echo("No alcohol for you!")
elseif age >= 21 then
echo("Drink it up!")
else
echo("Time to hand over your keys.")
end
Now, you're ready to face the world! Or the bar. Either way, you're ready.
Lastly with conditionals, there are compound conditional statements. These are done with or and and. With an or compound conditional, only one of conditional statements must be true. With and, they must all be.
- if 1+1 == 2 and 2 - 2 == 0 then
- if 1+2 > 2 and 3*3 < 7 then
- if 1+2 > 2 or 3*3 < 7 then
- if "a" == "b" and "q" == 2 then
The first example would run, as both statements are true.
The second example would not run, as one statement is false.
The third example would run, as one statement is true.
The fourth example would not run, as both statements are false.
Okay, now you're ready to face the world, really.
"Wait, Zouviqil, there's still loops!" Ah, yes, loops. There are three kinds of loops in lua, and perhaps in Mudlet: for, while, and repeat. But because I hate using while and repeat loops in lua, we're only talking about for loops, of which there are two types.
For Loops - Numbers
These are a great way to iterate over some tables (for those of you who have programming experience, the tables that are more like lists or arrays rather than dictionaries), or to perform an action when a certain value arises. This is their standard form:
for starting position, final position, increment do
someCode
end
Let's say you need to count by two and have it displayed on your screen. I don't know why you would, but let's say you need to. Up to the number 10.
for i = 0, 10, 2 do
echo(i)
end
So, you start at 0, will stop looping if you go higher than ten, and at the end of every loop, your variable's value will increase by two. You can also use this with tables, as I've said. Consider the following:
t = {"a", "b", "c", "d", "e"}
for i = 1, 5, 1 do
echo(t[i])
end
This would result in the following being displayed: 12345
For Loop - Tables
The other sort of loop is used solely for tables. Consider this layout:
table = {["some string"] = someValue, ["some other string"] = someOtherValue}
for keyValue, itemValue in pairs(table) do
someCode
end
You have a table that will be have its keys and their values iterated over each progression of the loop, until the end of the table is met.
Let's look at this again with a more practical example...
t = {["apple"] = "red", ["banana"] = "yellow", ["grape"] = "purple"}
for k,v in pairs(t) do
echo(k .. ": " .. v)
end
In this example, the colors of each fruit will be displayed. For each key and its associated value in the table t when it's paired, the key and its value is displayed. When it's finished going through the table, it stops looping.
As a note: You can combine conditionals and loops, by putting a for loop in a conditional, or by putting a conditional in a for loop! You can do it with the other loops I didn't teach you, too, I guess.
Coding Basics - Regular Expressions
Yes, it's basic, because without it, your code will do some crazy things you don't want. Regular expressions, AKA regex, are the weird symbol and letter combinations you will sometimes see in code you receive from others. It's a magical thing, though.
For example, let's say, for whatever reason, you want to keep count of the things you've killed. You'd need a database of some sort for that, but, for the sake of an example...
You have slain a rat.
You have slain Amberle.
You have slain Count Chocula. (HOW COULD YOU?!)
Surely you've noticed a pattern. "You have slain _____." But if you put that in for a pattern, Mudlet will laugh at you. So what do you do?
First, you know the line starts with "You". There is nothing that will appear before it. That's what the ^ (carat) is used for at the start of a statement. You only want to capture things you have slain, so you can leave the next two words there as well. At this point, we have:
^You have slain
Well, it will go, if you have code and this is your trigger. But it won't pick up any matches, which is terrible, if you're trying to use it in a database. What do we know? We know that the next thing in our pattern is going to be one or more words. So perhaps this:
^You have slain (\w)
Well. That certainly matches, but only the first letter in whatever you've slain. No good. The \w is any alphanumeric or underscore character. The parentheses say, "Hey, look here, look at this!" If you add a plus after the w, it will capture one or more of these characters.
^You have slain (\w+)
In the case with "a rat" or "Count Chocula", this only captures "a" or "Count" because of that pesky space. So what can be done?
When you put things inside of [braces] when using regex, you are telling the program to look for whatever's in the braces. But only one.
^You have slain ([a-zA-Z0123456789_]+)
This is exactly the same as the previous suggestion, but we know this isn't enough. Some names have apostrophes ' and some names have commas , and some names have dashes -. Apostrophes and dashes have special meanings in regex, so you must put a backslash before them to tell the regex "hey, I'm just looking for a dash, not trying to scope things." Let's face it, also, that things we kill typically don't have numbers or underscores. Names also can have spaces.
^You have slain ([a-zA-Z\-\', ]+)
This is nearly sufficient. We know that there's a period at the end and that it's the final character in the pattern. However, periods also have a special meaning within regex, so it'll need a backslash also. Lastly, a dollar sign after the period signifies it's the end of the line.
^You have slain ([a-zA-Z\-\', ]+)\.$
Yes... it's a bit difficult, but still rather basic in terms of what you must be able to do to make triggers and aliases. If you want to work with regex more and learn more about it, I'd suggest finding a free pattern tester online.
Coding Intermediate - Strings
Whoa, what, intermediate? Yes. Yes I went there. Actually, we went there.
Concatenation
This is among the simplest things to teach about strings. In other languages, to put two strings together, you typically use a "+" but here, you use ".."
str1 = "Do you believe" .. " in life after love? / "
str2 = "I can feel something inside me say / "
str3 = "I" .. " really " .. " don't " .. " think " .. " you're " .. " strong " .. " enough "
love = str1 .. str2 .. str3
Gsub
Strings have multiple things that may be done to or with them. Among the most useful is likely gsub. Think of it like regex meets replacing things in strings!
Let's say we have the sentence, "He-Who-Must-Not-Be-Named is an evil man. He-Who-Must-Not-Be-Named needs his reign ended, now!" (A Harry Potter nod, to those who somehow escaped over half a decade of books and movies). Let's say we won't take it anymore, we're calling him Voldemort!
someStr = "He-Who-Must-Not-Be-Named is an evil man. He-Who-Must-Not-Be-Named needs his reign ended, now!"
someStr = someStr:gsub("He%-Who%-Must%-Not%-Be%-Named", "Voldemort")
echo("\n" .. someStr)
The result?
Voldemort is an evil man. Voldemort needs his reign ended, now!
If you want to learn more about patterns for gsub, take a swing over to this page.
Changing Cases, Reversing, and Length
Make a string lower case
str1 = "THIS IS TOO LOUD MAKE IT QUIET"
str1 = str1:lower()
Make a string upper case:
str2 = "this is too quiet make it loud"
str2 = str2:upper()
Make a string title-cased:
str3 = "this could be a book title"
str3 = str3:title()
Reverse the order of characters in a string:
str2 = str2:reverse()
Get the length of the string:
str3:len()
Coding Intermediate - Functions
Functions make life a lot easier, and I've opted to avoid them until this point because I was hoping you'd get a hang of everything up until this point, first. Functions are best if you plan to repeat certain steps over and over.
function functionName()
someCode
end
However, you can pass variables into the function and do things with them, and then have the function return a value as well.
function someOtherFunctionName(var1, var2, var3)
someCode
return SomethingAwesome
end
I cannibalized the following example from another IRE game's forums, though I can't recall which or who wrote it... so thank you, person, for the example!
function inTable(table, item)
for k, v in pairs(table) do
if v == item then return k end
end
return false
end
The above looks through a table and if the item you're looking for is in the table, will return the index (or key) for it. If not, it'll return false. Could be very useful if you're trying to determine if you should add something to a table or not!... but there are two similar functions to this for tables that exist.
Coding Intermediate - Tables
I don't often use table functions aside from getn, insert and remove, but these will provide you with extra mobility when dealing with your coding.
Contains
Like our function example, contains works nearly the same way, except it returns no value, only true or false.
table.contains(tableName, value)
Size
Returns the number of items within a table.
table.size(tableName)
Index Of
Again, like our function example, index_of works nearly the same way, except that it returns a value or nil.
table.index_of(tableName, value)
Insert
Adds a value to a table. It has an optional parameter that tells it where in the table the value should be added.
table.insert(tableName, value)
table.insert(tableName, location, value)
Remove
Removes a value from the table.
table.remove(tableName, locationInTable)
Coding Advanced
If you're interested in doing GUI stuff, you'll want to learn CSS before Mudlet's Geyser.
If you're interested in databases, you'll want to take a look at LuaSQL lite.
I know how to do both of these, and could write up a mini primer for each, but that takes a long time and a lot of patience.
Comments
this section of code will error out:
Because you can't compare a string and a number.
Signature!