Doing more with one alias.

Share your scripts and packages with other Mudlet users.
Post Reply
Guilend
Posts: 15
Joined: Fri Mar 09, 2018 1:21 am

Doing more with one alias.

Post by Guilend » Sun Aug 05, 2018 11:22 pm

I was asked on the Mudlet discord channel to post some of my scripts, so this is just a couple, all of my scripts follows this same pattern since it's the only way I have learned to script at the moment. This is probably the most simplest script to most of you, but I didn't know how to script and still really don't. I had asked someone to send me a script for my charming spells on the MUD I play, Avalon The Legend lives. He sent me this one first.


function charmingSpell(spell, spellTarget)
send("chant " .. spell)
if spellTarget then
send("cast " .. spellTarget)
elseif target then
send("cast " .. target)
else
send("cast")
end
end


With this you can cast spells at someone else, like a friend, or yourself, or at your target. With this one script I learned a lot about scripting with "if,elseif and else" commands. This one I wrote for poisons.

function PoisonSS(PoiS, SpiTT)
if SpiTT then
send("outp " .. PoiS .. " spit " .. PoiS " at " .. target)
else
send("rub " .. PoiS .. " on knife;stab " .. target)
end
end

With this I can either spit or stab poisons at my target.

Jor'Mox
Posts: 1022
Joined: Wed Apr 03, 2013 2:19 am

Re: Doing more with one alias.

Post by Jor'Mox » Mon Aug 06, 2018 1:53 pm

Just as a useful note, when posting code to the forums, it is useful to use a tag to indicate that the text to follow is code, or more specially lua code. To do this, you start by using this tag with the spaces removed: [ lua ] , and you close out that block of text with this tag, again with the spaces removed: [ /lua ]

It makes it look like this, which is much easier to read:
Code: [show] | [select all] lua
function charmingSpell(spell, spellTarget)
    send("chant " .. spell)
    if spellTarget then
        send("cast " .. spellTarget)
    elseif target then
        send("cast " .. target)
    else
        send("cast")
    end
end
Of course, this doesn't help if the code itself isn't properly indented, like in your second example, but it is a step in the right direction regardless.

Guilend
Posts: 15
Joined: Fri Mar 09, 2018 1:21 am

Re: Doing more with one alias.

Post by Guilend » Tue Aug 07, 2018 1:09 pm

Thanks, I was wondering how others did that lol

Jor'Mox
Posts: 1022
Joined: Wed Apr 03, 2013 2:19 am

Re: Doing more with one alias.

Post by Jor'Mox » Tue Aug 07, 2018 1:45 pm

Since you are new to all this, here is some feedback on the functions you posted.

Firstly, here is just a nicely formatted version of your second function:
Code: [show] | [select all] lua
function PoisonSS(PoiS, SpiTT)
    if SpiTT then
        send("outp " .. PoiS .. " spit " .. PoiS " at " .. target)
    else
        send("rub " .. PoiS .. " on knife;stab " .. target)
    end
end
One obvious issue here is that you are missing a concatenation symbol (the '..') in your first send line, right after the second PoiS. But, you can make all of that a bit simpler to manage by using the string.format function like this:
Code: [show] | [select all] lua
function PoisonSS(PoiS, SpiTT)
    if SpiTT then
        send(string.format("outp %s spit %s at %s", PoiS, PoiS, target))
    else
        send(string.format("rub %s on knife;stab %s", PoiS, target))
    end
end
Using string.format, each instance of %s is replaced with the corresponding variable or value, which makes it much easier to avoid forgetting spaces or concatenations, etc, and is generally just a bit easier to read.

You can also use "or" and "and" operators to combine things together to make logic a bit simpler, such as in your first function, which could be changed like this:
Code: [show] | [select all] lua
function charmingSpell(spell, spellTarget)
    send("chant " .. spell)
    spellTarget = spellTarget or target  -- this will replace spellTarget with target if there is nothing in spellTarget
    spellTarget = (spellTarget and (" " .. spellTarget)) or ""  -- this will either add a space before spellTarget if there is one, or replace it with an empty string if there isn't
    send("cast" .. spellTarget) -- and this will send either "cast target" or "cast", depending on if you have a target or not
end
I added some comments to the code here to help explain what each of the different steps is doing. While this isn't significantly simpler than your original version, I think it provides a useful example to help illustrate how you can use 'or' and 'and'. Realistically, I would probably use a version like this:
Code: [show] | [select all] lua
function charmingSpell(spell, spellTarget)
    send("chant " .. spell)
    spellTarget = spellTarget or target
    if spellTarget then
        send("cast " .. spellTarget)
    else
        send("cast")
    end
end

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests