Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ If/then statements with strings

If/then statements with strings

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Ivalos   (1 post)  Bio
Date Thu 05 Aug 2010 08:05 PM (UTC)
Message
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.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Thu 05 Aug 2010 08:17 PM (UTC)
Message
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'

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #2 on Thu 05 Aug 2010 08:17 PM (UTC)

Amended on Thu 05 Aug 2010 08:20 PM (UTC) by Twisol

Message
(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!

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Thu 05 Aug 2010 09:36 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


16,716 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.