Alias not executing properly from prompt trigger when regex is matched.

Posted by Beginner on Fri 03 Apr 2009 11:55 AM — 10 posts, 39,472 views.

#0
Hello all, I'm very new to MUSH and MUDs in general, and I've never programmed, so I'm having a bit of trouble making this basic group of triggers and aliases working properly. Here goes

Trigger:
^(\d+)h, (\d+)m, \d+e, \d+w ([cexkdb@]*)-

Executes:
SetVariable("currenthealth", %1)
SetVariable("currentmana", %2)
Execute("HEAL")
Execute("LIMBS")

Trigger:
^Health:\s+\d{1,4}\s/\s(\d{4})\s{5}Mana:\s+\d{1,4}\s/\s(\d{4})$

Executes:
SetVariable("maxhealth", %1)
SetVariable("maxmana", %2)
SetVariable("siphealth", %1-750)
SetVariable("sipmana", %2-550)
SetVariable("mosshealth", %1-1100)
SetVariable("mossmana", %2-1600)

Alias:
HEAL

Executes:
local currenthealth = GetVariable("currenthealth")
local siphealth = GetVariable("siphealth")
local healbalance = GetVariable("healbalance")
local sipping = GetVariable("sipping")

if healbalance == "1" and sipping == "0" then

if currenthealth < siphealth then

Send("sip health")
SetVariable("sipping", 1)

end

end


Everything seems to work fine and dandy until my health becomes a 3-digit number, i.e. anything 999 and below. At that point it simply stops sipping. I've experimented and checked World Properties -> Variables when my health is below 999 and all the variables are still being set correctly, it's just not sipping. I can sit there and wait until my health, which regenerates slowly over time, goes up to a 1000 and as soon as the first prompt indicating it's a 4-digit number pops I start sipping again. Very odd, and looking at the scripting with my beginner eyes I can't see what's causing it. Any help is greatly appreciated. Thanks!

btw, here's what my prompt looks like with 4 and 3 digits of health respectively:
3877h, 2563m, 18285e, 11715w cexkdb-

865h, 2563m, 18285e, 11715w cexkdb-
USA #1
Not even sure if this is an issue, but perhaps this trigger

^Health:\s+\d{1,4}\s/\s(\d{4})\s{5}Mana:\s+\d{1,4}\s/\s(\d{4})$

Needs the (\d{4}) to be (\d{1,4}) as it might be getting a 3 digit number yet not checking it as it is looking for a 4 digit. That is the only thing I see at first glance, everything allows for 1-4 digits and that only allows for 4.

-Onoitsu2
#2
Well, I tried it even though I didn't think it would be an issue. That particular capture is my maxhealth, not currenthealth, and it's always 4 digits. Regardless, I tried changing it to {1,4} just to see if it would work, but it didn't.

Thank you anyways :)
Australia Forum Administrator #3
Your problem is this line:


if currenthealth < siphealth then


It is a problem because variables retrieved by GetVariable are strings. Thus for example, a test like:


if "900" < "1000" then


This does not give the results you expect. For strings, the left-most character is the most significant (so it compares the 9 to the 1). It therefore thinks that "900" is greater than "1000".

To fix it, change them to numbers, eg. change:


local currenthealth = GetVariable("currenthealth")
local siphealth = GetVariable("siphealth")


to:


local currenthealth = tonumber (GetVariable("currenthealth"))
local siphealth = tonumber  (GetVariable("siphealth"))


Then the compare will work correctly.


#4
Oh, that's neat. Thanks for the help, Nick. I think I should probably learn what a string is now, and some of the other intricacies of MUSH.

Thanks!
#5
Ah, sorry, now I'm getting this error message:

Run-time error
World: Achaea
Immediate execution
[string "Alias: "]:9: attempt to compare two function values
stack traceback:
[string "Alias: "]:9: in main chunk
[C]: in function 'Execute'
[string "Trigger: "]:3: in main chunk
Australia Forum Administrator #6
Can you show exactly what you did please?

See: http://mushclient.com/copying


#7
<aliases>
<alias
match="HEAL"
enabled="y"
group="autosipper"
send_to="12"
sequence="100"
>
<send>local currenthealth = tonumber GetVariable("currenthealth")
local siphealth = tonumber GetVariable("siphealth")
local healbalance = GetVariable("healbalance")
local sipping = GetVariable("sipping")
local stupidity = GetVariable("stupidity")

if healbalance == "1" and sipping == "0" then

if currenthealth &lt; siphealth then

Send("sip health")
SetVariable("sipping", 1)

if stupidity == "1" then

DoAfterSpecial(1, 'Execute("resetsipping")', 12)

end

end

end</send>
</alias>
</aliases>
Australia Forum Administrator #8
You left out some brackets. I had:


local currenthealth = tonumber ( GetVariable("currenthealth") )
local siphealth = tonumber  ( GetVariable("siphealth") )


You have:


local currenthealth = tonumber GetVariable("currenthealth")
local siphealth = tonumber GetVariable("siphealth")


Lua allows multiple statements on a single line, so what you effectively have is:


local currenthealth = tonumber;   GetVariable("currenthealth")
local siphealth = tonumber;   GetVariable("siphealth")


In other words you assigned the function "tonumber" to both currenthealth and siphealth, and then when you compared them, it complained you were comparing functions to each other.
Amended on Sat 04 Apr 2009 09:36 PM by Nick Gammon
#9
Ahhhhh ok. I'm new to scripting, so I'm still unused to dotting my i's and crossing my t's, if you will. It's not habit yet. Thank you, works perfectly now.

Sorry about posting in the wrong forum, by the time I realized it was the wrong forum I figured making a second topic would be pointless. Thanks again!