So, I've decided to write a basic guide for coding in MUDs. While it'll be Mudlet-oriented, the beginning bits will at least generalize to other clients as well (or so I assume). This isn't the only post I'll make (hopefully?) but it is going to be the first one for awhile (because man this took like an hour!) So, without further ado, here we go.Basic Coding Information
I'd rather assume nothing, and leave nothing to chance. I'm sure this quick'n'dirty thing won't cover something - if you think of something easy that I missed, let me know and I'll add it!
Think of it as a box. It stores a value for you. Variables are initialized in Mudlet by doing simply variableName = value. Some examples:
number = 13
text = "This is how you store a string of text!"
truefalse = true --true, false and nil are booleans. Nil means 'there's no value at all in here'.
number = text --This is how you'd copy a variable's value
If we want to COMPARE variables, we use ==
if text == number then
This brings us to our next topic...
The statement exemplified above is an 'if' statement. It means exactly how it reads: IF the variable 'text' EQUALS the variable named 'number', THEN do whatever. And then it ends. You can do several other kinds of statements - loops. The most common one I use is the WHILE loop.
while text == number do
Please note that this will execute forever if text and number are the same, unless you have some way within the loop for that condition to no longer be true. And it will crash your Mudlet, and if you go have a cry I won't judge you because I do it all the damn time.
+ for addition, - for subtraction, * for multiplication and / for division. You can add/subtract/multiply/divide variables and numbers freely.
Sometimes we don't know what's going to happen. Maybe Toz punches you in the face. Maybe it's Moirean. Maybe it's you punching yourself - maybe it was always you punching yourself. Do you have 1 gold, or 100000 gold? Did you swing an elegant rapier inlaid with rubies, or a short cudgel covered in grease? These questions would matter if it wasn't for wildcards - your new best friend.
If we need a placeholder for a single word?
Toz punches you in the face.
(\w+) punches you in the face.
What if it's two words? Or...or three words? OR MORE THAN THREE?!
You suddenly feel like eating a giant pickle sundae.
can be done as
You suddenly feel like eating (.+)
...Don't look now, but there's a number behind you!
You have 2 sets.
You have (\d+) sets
OR for extra credit,
You have (\d+) (\w+)
since you can have 1 set, or 2 sets
And now time to terrify you:
A pack with 3000 gold is here.
can become, simply
A pack with (.*) is here.
Be careful with (.+) and (.*) - they're REALLY grabby. Additionally, if you have a wildcard that you don't want to save for later? Just drop off the ()s and leave it as \w+ or whatever - they won't mind.
And now that you've crammed some value into your new best friend (remind me to never be friends with you), you're going to want to pull it out some time. I...hope. To do that, we're going to touch upon another topic briefly and then run away screaming: an array. An array is a bunch of boxes that exist in numbered order. And now we scream. And scream. And ok we're back - so the values we save from a trigger are stored in an array called matches. To reference a specific part of an array, we use the format arrayName[value]. So for matches, we'd do matches to see the WHOLE LINE. Please note this, because it messes newbies up lots. If you want the FIRST VARIABLE YOU USED A WILDCARD FOR, use matches. The 2nd variable is matches. Etc. It's about as clear as mud to a newbie (heh, MUD/mud) - but if you remember it, you'll be ok.
I'm sure I've left something off from here, so lemme know and I'll add it. And now onto...The Big 3
There are four aspects to coding within Mudlet. They are Aliases
, and Scripts
. Before we go any further, it is important to know what these things are, so allow me to give an overview. I'll go a little more in-depth after each is explained, so if you're already solid on these feel free to skip ahead.Aliases
is nothing more than a shortcut. When you type in an alias into the input line, instead of sending the text to the MUD, it will execute the code. An example alias, and its output, is below:
echo("Toz is our name.\n")
echo(".Eman rou si zoT")
What that will do, is any time I type in 'toz' into the input line, it will display the echoes. Specifically why that works (scripts!) is something coming up soon, but for now just know that it works - any time you type in something in the pattern line of an alias into the input line, it'll execute whatever is in the big box instead of sending what you typed in to the MUD.Triggers
is almost the opposite of an alias. Whereas aliases are active things you type in, a trigger runs reactively. When the MUD sends you a line, Mudlet (or your client) will try to match the line against all the triggers you currently have, and if a match is found, it will run the code it is supposed to. An example
To explain the above, if the line 'Password correct. Welcome to Aetolia.' appears on your screen (as it does every time you login), you'll attempt to FARSEE TOZ. Mileage/hammerage may vary, depending on how often you log in, but I digress.
A discerning eye will notice a drop-down menu that I left strategically open, to the right of the trigger line. Just know that is there, for now - I'll get to what it does later!Script
. You could go your entire life without ever using one, but that would also make me cry. It is the nature of problems to reoccur, with slightly different forms - so if you find yourself running into the same problem (as in, requiring the same code or similar code) over and over, you could simplify your life and put everything in a script!
A simple example of a real script I use is an infoEcho() - on the system I'm building, I like a pretty format display to come up on my screen that reads [CATS]: . So if I ran
infoEcho("The cake is a lie.")
I would see
[CATS]: The cake is a lie.
appear on my screen. Note the format for calling functions - it is functionName(things to send). Multiple things to send (parameters) can be sent using functionName(parameter, parameter2, parameter3). You can send numbers, strings, even variables to a function!
So now that you've been properly introduced to aliases, triggers and scripts, we can move on to more advanced interplay between them. This post is already fairly long, so I'm going leave you in a cliffhanger of sorts - instead of doing advanced stuff, I'm going to underscore some things in case they aren't as clear as I want them to be. Then I'll review, wrap it up, and take a break from writing!Aliases 2: The Aliasing
Sometimes it's not enough to just type my name into the input line - an unfortunate twist of fate. Sometimes we may want to have an alias work for many different things, because we are fundamentally lazy (and that's okay!); instead of making a million different aliases for every possible solution, I'm going to reference something from the beginning: wildcards.
That's right, your best friend wildcard is back, fresh from having the last value you crammed into them (and then promptly forgot about as you read the rest of this guide) removed with the jaws of life, and they're back for more. So first let's attach some anchors!
...What is an anchor? Glad I asked! An anchor is what you use to specify the BEGINNING and END of your alias. If you don't anchor your toz alias, any time you type toz EVER, it'll trigger. If you do ^toz$ instead, it'll only ever work if you type in 'toz' with nothing around it/no spaces. So from now on, whenever we work with aliases (OR TRIGGERS, sometimes), remember to slap some anchors on it so it doesn't float away. So let's have an example! What if we want to smack someone with our weapon? Well, using the magic of wildcards, picking your target is as simple as one alias!
alias: ^smack (\w+)$ --Notice, the wildcard!
send("smack "..matches) --Notice we use the wildcard!
Now when we type in 'smack toz', you draw your smacker (...whatever that is), and smack me with it. But if you type in 'smack fallon', you'll hit them instead. Just use the appropriate wildcard for what you want to do, and you'll be okay!Triggers:
Tumblr EditionToz Edition
So, we've touched upon wildcards in triggers before, but I'd like to take a moment to reinforce some basics, because that's important. When you work with triggers, anchor them. Or else they'll fly away and take your entire script with them! If you know the start of the line, lead your trigger with ^. If you know the whole line, wrap it up in ^ and $. If you only know the last few bits at the end, feel free to just use $ at the end - but remember to anchor as much as you can!
Additionally, to make wildcards work with triggers, you'll need to set the type as PERL REGEX. You're using regular expression (REGEX) to capture via wildcards, so you have to let the client know. There are things in regex that exist as wildcard/functions that you may not want to be read as regex, as well - what if you wanted to (for whatever reason) trigger off seeing a '.*'? You're able to, using a backslash! \.\* tells regex to interpret those LITERALLY, that is, they're not a wildcard. They're just a dot then an asterisk.
^This is a valid trigger\.$
^(\w+) is this\.$
is valid too\!$
^I can do this too
I can even do this, but I shouldn't\.