Using a variable in if

Posted by Meraki on Fri 30 May 2014 01:48 PM — 3 posts, 13,164 views.

#0
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.
Amended on Fri 30 May 2014 01:54 PM by Meraki
#1
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
Australia Forum Administrator #2

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"))