Function with different arguments

Post Reply
tarrant
Posts: 49
Joined: Thu Apr 15, 2010 10:36 pm

Function with different arguments

Post by tarrant »

Code: [show] | [select all] lua
function typeSwitch( fire, earth, lightning, water, air )
Is it possible to make a function and put it into an alias such that i can do "switch <type>" and i'll check to see what type of elemental i'm using. If i'm using the same type that i want to switch to, it'll do nothing, but if its a different type, i'll switch to that type?

I think i can do it via aliases. But i read somewhere in the forums that it's better to use functions to do things, and i want to learn too. I've been looking around the net and i haven't found anything that explains it sufficiently and simply enough for me to understand.

Iocun
Posts: 174
Joined: Wed Dec 02, 2009 1:45 am

Re: Function with different arguments

Post by Iocun »

Since I don't know the details of what you actually want to do (i.e. what commands you actually want your function to send to the MUD, etc.), I can only give a rough guess:

I'll assume that the "elemental types" are the five you listed in your function header: Fire, earth, lightening, water, air. I assume that you want Mudlet to remember which of these you are currently using, be able to switch this around with this alias/function, and then either do nothing, or send a certain command that tells the MUD to switch to another type. Since I don't know this command, I'll simply make this SWITCH WATER or SWITCH EARTH for the sake of this demonstration.

First of all, we need a variable that stores the current elemental you're using. Go to the "Scripts" section of Mudlet, and use "Add Item" to create a new script. In this you first define your variable:
Code: [show] | [select all] lua
elemental = ""
(Actually you don't -need- to initialize the variable like that. If you left that line out, it would simply have the value nil, until you set it to something different, which would work as well. But I like having an explicit variable definition like that right at the beginning of the script, so I can clearly see right from the beginning what variables appear in the script.)

Below this, we're creating our function:
Code: [show] | [select all] lua
function switchElemental(type)
   if elemental~=type then
       send("switch "..type)
       elemental=type
   end
end
What are we doing here?
First we give the function its name switchElemental and define that this function will expect a single argument: The type of elemental that you want to use.
Next we check whether your current elemental type (stored in the variable elemental, which is still empty right now) is different from the type you want to switch to. ~= means: not equal to. So: If the content of the variable "elemental" is not equal to what we received as an argument for this function, execute the following commands.
The following commands are now first a send() which will send "switch " followed by your desired type to the MUD. (for instance "switch fire".)
Then we update our variable "elemental" to now carry our new type of elemental.

If we now call the function with switchElemental("lightning") it will first check whether we're already using lightning, and since we aren't, it will send "switch lightning" to the MUD, then set the variable elemental to "lightning".

The only thing you still need to do is to include a call of this function in your alias.
Create a new alias, let's say with the pattern: ^switch (\w+)$
Have it execute: switchElemental(matches[2])

This will call the function switchElemental with the argument specified in our alias call.


------
So, now that we're finished with this, you may be wondering why it was necessary to use a function and all that for this. Fact is, it wasn't necessary. It would have been just as feasible to put the whole script in the alias directly. But you said you wanted to learn how to use functions, which I find a good goal, so there we go!

Where then -should- you importantly use functions? For one, if the script executes a lot of things, or complicated things, I recommend it. I tend only to put simple commands and instructions in my aliases and handle all the complex scripting stuff in dedicated functions. That way I keep the core of my scripts in one central place, and the "user interface" (i.e. the aliases, macros, etc.) in a different one. That helps to keep things clear.

You also should definitely use functions when you're going to use the same script several times for different things. It's generally much better to have 10 different aliases call the same function, than to copy and paste the same code into 10 different aliases. In the former case, if you want to change the code, you only have to change a single function. In the latter case, you'll have to go around and find all occurrences of the code, change it, and still be likely to miss one or two which will then easily produce errors that can be annoying to find.

Functions also can serve a number of other purposes, such as being able to call themselves in a recursive loop, and so on.

Lastly, in the thread I suppose you saw where I mentioned that one should always call functions from within scripts instead of using expandAliases, I meant just this comparison. expandAlias is a functionality that should in -most- cases be avoided, since it can have certain nasty side-effects and is generally considered bad practice. Aliases should almost only be called manually by -you- typing stuff into the command line. Aliases are -not- meant to be called from within scripts. That's what functions are for. If you remember this distinction (aliases are what you call yourself from the command line, functions are what are called internally, from within scripts/aliases/macros/whatever), you'll produce a much clearer, logical code.

Post Reply