Page 1 of 2

Aaaaargh!

Posted: Thu Jan 07, 2010 12:05 am
by boardeaux
Right, just discovered Mudlet this week and its awesome. Thank you people/person who did make it.

But I really suck at programming and I can't get me head round something. I'm trying to create an alias thing to allow my character to spit or blowpipe a poison based on typing 1something. The something would be a pre-defined variable. The spit or blowpipe would happen based on whether the character was in or out of the forest - based on another variable called inforest.

So typing 1w would - spit wyg at target or blowpipe wyg target. There is a list of approx 40 poisons that would have to be pre-defined as variables.

So far I have the following but it doesn't work:

Alias name: Target1
Pattern: ^tt(\s(\w+))
Script:
target=(matches[3])
send("tt " .. target)

Alias name: Pipe/Spit
Pattern: ^1(\w+)
Script:
w=wyg
if inforest==1 then
send("blowpipe " .. matches[2] .. " " .. target)
else
send("spit " .. matches[2] .. " at " .. target)
end

A trigger sets whether inforest or not. This part and the target part seems to works ok.

Not sure how to get the pattern match to translate to the variable value. The output seems to just be spit w at target or blowpipe w target

Re: Aaaaargh!

Posted: Thu Jan 07, 2010 2:07 am
by colloquialism
There are two (relatively) simple ways of going about this.

The long, laborious way, is writing out something to test against what you match, e.g:

Code: Select all

if matches[2] == "w" then
   if inforest==1 then
      send("blowpipe " .. matches[2] .. " " .. target)
   else
      send("spit " .. matches[2] .. " at " .. target)
   end
elseif matches[2] == ...
and so on and so forth. Unless you're masochistic, I don't recommend this. The faster way to get what you want done is to use a table. If you have an alias that you run at login to initialize variables and tables, you'd want to add:

Code: Select all

venoms = {}
venoms.w = "wyg"
venoms.s = ...
and define your approx 40 venoms there. THEN your alias would look something like:

Code: Select all

Pattern: ^1(\w+)$
Script:
if inforest==1 then
   send("blowpipe " .. venoms[matches[2]] .. " " .. target)
else
   send("spit " .. venoms[matches[2]] .. " at " .. target)
end
With that script, the input "1w" would give: "blowpipe wyg target" or "spit wyg at target"

Re: Aaaaargh!

Posted: Thu Jan 07, 2010 2:14 am
by Thylacine
You will also want to check if matches[2] is actually in the table, for example:

Code: Select all

Pattern: ^1(\w+)$
Script:
if venoms[matches[2]] != nil then
    if inforest==1 then
       send("blowpipe " .. venoms[matches[2]] .. " " .. target)
    else
       send("spit " .. venoms[matches[2]] .. " at " .. target)
    end
end

Re: Aaaaargh!

Posted: Thu Jan 07, 2010 11:04 am
by boardeaux
That did the trick, thanks very much!

One dumb question though, what is the $ for at the end of the alias pattern?

Re: Aaaaargh!

Posted: Thu Jan 07, 2010 12:51 pm
by hempa
In a regular expression, $ means "end of line". If you didn't have it there, the following alias:
^1(\w+)
would match
1somelongtextuntilawhitespace
:)

Re: Aaaaargh!

Posted: Thu Jan 07, 2010 1:17 pm
by boardeaux
ahhh, I see. Thank you

Re: Aaaaargh!

Posted: Sat Feb 26, 2011 4:21 am
by maharach
i don't seem to understand how to initialize tables at login, i'm trying to add a function to my init() script -
function venoms()
venoms = {}
venoms.w = "wyg"
venoms.s = ...
end

and it doesn't seem to be doing anything. and for the alias i'm using this version -
send("outpouch " ..venoms[matches[2]])
if balance.forests==1 then
send("blowpipe " .. venoms[matches[2]] .. " " .. target)
else
send("spit " .. venoms[matches[2]] .. " at " .. target)
end

i'm assuming this isn't the way to go, heh

Re: Aaaaargh!

Posted: Sat Feb 26, 2011 5:31 am
by Omni
maharach wrote:i don't seem to understand how to initialize tables at login, i'm trying to add a function to my init() script -
function venoms()
venoms = {}
venoms.w = "wyg"
venoms.s = ...
end

and it doesn't seem to be doing anything. and for the alias i'm using this version -
send("outpouch " ..venoms[matches[2]])
if balance.forests==1 then
send("blowpipe " .. venoms[matches[2]] .. " " .. target)
else
send("spit " .. venoms[matches[2]] .. " at " .. target)
end

i'm assuming this isn't the way to go, heh
You're setting the variable venoms to both a function and a table.

Re: Aaaaargh!

Posted: Sat Feb 26, 2011 5:55 am
by maharach
ah, so don't put it in a function?

Re: Aaaaargh!

Posted: Sat Feb 26, 2011 6:10 am
by Omni
maharach wrote:ah, so don't put it in a function?
just change the names for either the function or the table.