If/then statements with strings

Posted by Ivalos on Thu 05 Aug 2010 08:05 PM — 4 posts, 21,334 views.

#0
Right now, I have a trigger for my prompt that makes me sip/etc.
I'm trying to make my character stand if he's ever prone, and I got this far:

Trigger: ^\(h: (1?[0-9][0-9])% m: (1?[0-9][0-9])%\) [(p?)(e?b?c?x?s?)]$

SetVariable ("prone", "%3")
if @prone == 'p' then
 Send("handspring")
end


and that's not working. I'm worthless at coding.
USA #1
What exactly do you mean by "not working"? What kind of error message are you getting? etc.

Try: GetVariable("prone") instead of @prone. If memory serves, @<var> substitutions are done only once, at the entrance of the script, so since you set the variable you can't use @prone.

Or, try: if "%3" == 'p'
USA #2
(EDIT: Haley-ninja'd :( )

You've hit a pretty common gotcha, actually. The Send code is pre-processed before it's run as Lua code. The @var and %wildcard syntax is not part of Lua, and MUSHclient replaces them with their true values before running the code. So in your code, @prone is actually retrieved before you set it.

Just use GetVariable("prone") or "%3" instead of @prone:

SetVariable ("prone", "%3")
if "%3" == 'p' then
 Send("handspring")
end

-- Or this:

SetVariable ("prone", "%3")
if GetVariable("prone") == 'p' then
 Send("handspring")
end



There's also another problem in your code: @ and % identifiers are inserted directly into the code, so if we assume that @prone does contain the letter p, it would look like this before going into Lua:

SetVariable ("prone", "%3")
if p == 'p' then
 Send("handspring")
end


It looks like p is a variable, not a string. So you need to quote @ and % identifiers yourself if you're going to use them as strings:

SetVariable ("prone", "%3")
if "%3" == 'p' then
 Send("handspring")
end


Final nitpick: Make sure you use double quotes "" instead of single quotes '' when quoting the @/% identifiers. If a variable contains a double quote, MUSHclient will escape it for you so it doesn't cause a script error (because Lua will think the string ended early). I don't believe it does the same with single quotes, so you need to make sure you use the doubles!
Amended on Thu 05 Aug 2010 08:20 PM by Twisol
Australia Forum Administrator #3
Twisol said:

Final nitpick: Make sure you use double quotes "" instead of single quotes '' when quoting the @/% identifiers. If a variable contains a double quote, MUSHclient will escape it for you so it doesn't cause a script error (because Lua will think the string ended early). I don't believe it does the same with single quotes, so you need to make sure you use the doubles!


Twisol is right - only double-quotes are fixed up.