[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Using a variable in if
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Using a variable in if

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


Posted by Meraki   (7 posts)  [Biography] bio
Date Fri 30 May 2014 01:48 PM (UTC)

Amended on Fri 30 May 2014 01:54 PM (UTC) by Meraki

Message
I'm having trouble with an if statement firing when comparing variables. Here's what I have:

<triggers>
<trigger
enabled="y"
match="Look in the general vicinity of * for *!"
send_to="12"
sequence="100"
>
<send>SetVariable ("location", "%1")
SetVariable ("room", "%2")

ColourNote ("black", "white", "Location: %1 Room: %2")

-- Location Jovar
if "%1" == "Jovar" then
ColourNote("black", "white", "Location Recognized: @location")
Execute("#4s 3e 3n 3w 3n e (up)")
Execute("enter icewall")
Execute("c fly")
Execute("#2(nw) 2w (nw) n (nw) e n 2(u) w 2(up)")

if "%2" == "Smithy" then
ColourNote("black", "white", "Room confirmed: @room")
Execute("#5n 2e n")
if "@quest_item" == "the crown jewels" then
Execute("get crown$recall$north$north$pq complete")
elseif "@quest_item" == "blue diamond shard" then
Execute("get shard$recall$north$north$pq complete")
elseif "@quest_item" == "bright green emerald" then
Execute("get emerald$recall$north$north$pq complete")
elseif "@quest_item" == "lost puppy" then
Execute("get puppy$recall$north$north$pq complete")
end -- quest_item if
end -- room if

if "%2" == "The Planning Room" then
ColourNote("black", "white", "Room confirmed: @room")
Execute("#5n 2e n")
Execute("open west")
Execute("west")

if "@quest_item" == "the crown jewels" then
Execute("get crown$recall$north$north$pq complete")
elseif "@quest_item" == "blue diamond shard" then
Execute("get shard$recall$north$north$pq complete")
elseif "@quest_item" == "bright green emerald" then
Execute("get emerald$recall$north$north$pq complete")
elseif "@quest_item" == "lost puppy" then
Execute("get puppy$recall$north$north$pq complete")
end -- quest_item if
end -- room if
end -- location if</send>
</trigger>
</triggers>


This solution works because it's working directly with the wildcards that are being sent in. What I want to work is this:

ColourNote ("black", "white", "Location: %1 Room: %2")

-- Location Jovar
if "@location" == "Jovar" then

As I understand it, it's supposed to compare the variable location to the word Jovar however the statement doesn't fire. I've considered using getVariable but I'm not sure how things are stored in a variable.

Could someone clear this up for me as to why this isn't matching?

Thank you in advance.
[Go to top] top

Posted by Meraki   (7 posts)  [Biography] bio
Date Reply #1 on Fri 30 May 2014 02:52 PM (UTC)
Message
I have an update from searching around a little more extensively is it possible to use the string.match?

The code would look something like this:

if string.match(@location, "Jovar") then
--Do stuff
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Sat 31 May 2014 03:08 AM (UTC)
Message

SetVariable ("location", "%1")
...
ColourNote("black", "white", "Location Recognized: @location")


Your problem here is that variable substitution is done before the script executes. That is, @location is replaced by the contents of the "location" variable before anything is executed.

Thus the new value (%1) will not be used.

You are better off using Lua variables, or using GetVariable.

eg. Lua variables:


location = "%1"
...
ColourNote("black", "white", "Location Recognized: " .. location)


or using GetVariable:


SetVariable ("location", "%1")
...
ColourNote("black", "white", "Location Recognized: " .. GetVariable ("location"))

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] 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.


2,069 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]