Elseif Problem

Posted by Ahiram on Sun 24 Apr 2011 01:51 AM — 13 posts, 44,654 views.

#0
<alias
match="^harv$"
enabled="y"
group="General"
regexp="y"
ignore_case="y"
expand_variables="y"
send_to="12"
sequence="100"
>
<send>if harvestterrain == forest then
Execute ("harvest ginseng;harvest myrrh;harvest lobelia;harvest elm;harvest echinacea;harvest ginger")
elseif harvestterrain == hills then
Execute ("harvest hawthorn;harvest bayberry")
end</send>

This is what I have. Simple and ugly. The problem is that it pays no attention to what the variable actually says and tries to harvest all of it. I deduce that the == is just checking to see whether the variable has a value or is nil, so its going down the list. I'm not sure how to make it stop. I tried putting quotations around forest and hills, but the result of that is that the script doesn't fire at all.

For the sake of completeness, the rest of what I have is posted below.


<triggers>
<trigger
custom_colour="15"
enabled="y"
match="^A ginseng plant \(ginseng\)"
regexp="y"
send_to="12"
sequence="100"
><send>SetVariable ("harvestterrain", "forest")</send>
</trigger>
<trigger
custom_colour="15"
enabled="y"
match="^A myrrh bush \(myrrh\)"
regexp="y"
send_to="12"
sequence="100"
><send>SetVariable ("harvestterrain", "forest")</send>
</trigger>
<trigger
custom_colour="15"
enabled="y"
match="^A lobelia wildflower \(lobelia\)"
regexp="y"
send_to="12"
sequence="100"
><send>SetVariable ("harvestterrain", "forest")</send>
</trigger>
<trigger
custom_colour="15"
enabled="y"
match="^A red elm tree \(elm\)"
regexp="y"
send_to="12"
sequence="100"
><send>SetVariable ("harvestterrain", "forest")</send>
</trigger>
<trigger
custom_colour="15"
enabled="y"
match="^A purple coneflower \(echinacea\)"
regexp="y"
send_to="12"
sequence="100"
><send>SetVariable ("harvestterrain", "forest")</send>
</trigger>
<trigger
custom_colour="15"
enabled="y"
match="^A wild ginger plant \(ginger\)"
regexp="y"
send_to="12"
sequence="100"
><send>SetVariable ("harvestterrain", "forest")</send>
</trigger>
<trigger
custom_colour="15"
enabled="y"
match="^Clusters of nuts \(nuts\)"
regexp="y"
send_to="12"
sequence="100"
><send>SetVariable ("harvestterrain", "forest")</send>
</trigger>
<trigger
custom_colour="11"
enabled="y"
match="^A hawthorn plant \(hawthorn\)"
regexp="y"
send_to="12"
sequence="100"
><send>SetVariable ("harvestterrain", "hills")</send>
</trigger>
<trigger
custom_colour="11"
enabled="y"
match="^A bayberry tree \(bayberry\)"
regexp="y"
send_to="12"
sequence="100"
><send>SetVariable ("harvestterrain", "hills")</send>
</trigger>
</triggers>
<!-- End Triggers -->
<!-- Variables -->
<variables>
<variable name="harvestterrain">Default</variable>
</variables>
<!-- End Variables -->
<!-- Aliases -->
<aliases>
<alias
match="^checkterrain$"
enabled="y"
group="General"
regexp="y"
ignore_case="y"
expand_variables="y"
send_to="12"
sequence="100"
>
<send>ColourNote ("white", "green", "Current terrain is @harvestterrain")</send>
</alias>
<alias
match="^harv$"
enabled="y"
group="General"
regexp="y"
ignore_case="y"
expand_variables="y"
send_to="12"
sequence="100"
>
<send>if harvestterrain == "forest" then
Execute ("harvest ginseng;harvest myrrh;harvest lobelia;harvest elm;harvest echinacea;harvest ginger")
elseif harvestterrain == "hills" then
Execute ("harvest hawthorn;harvest bayberry")
end</send>
</alias>
</aliases>
<!-- End Aliases-->

Sorry for the hideous formatting.
USA Global Moderator #1
If you use SetVariable to store the value, then you need to also use GetVariable to recall the value. And yes you need the quotation marks.
#2
Beautiful, thank you.
USA #3
local terrain = GetVariable("harvestterrain")
if terrain == "forest" then
  -- ...
elseif terrain == "hills" then
  -- ...
end


Also, unless you're doing this to learn (which is awesome), I already wrote this. [1]

[1] http://jonathan.com/achaea/plugins/Harvester.plugin.zip
#4
Gonna bump this thread because my new problem is similar. And yeah, I want to do as much of this for myself as possible. Learning is tricky, but rewarding in the end.

Hopefully this here is self-explanatory, but just in case, I shall explain what I'm trying to do. I'm trying to make an alias that checks my morph, and if the morph is capable of flipping on reflexes, and reflexes is disabled, then it will flip it on. If its in a morph capable of reflexes, and reflexes is enabled, it will shut it off. If its not in a morph that will allow reflexes, it morphs into one and flips on reflexes. Here's what I have.

<alias
match="^ref$"
enabled="y"
group="Powers"
regexp="y"
ignore_case="y"
expand_variables="y"
send_to="12"
sequence="100">
<send>morph = GetVariable ("morph")
reflexes = GetVariable ("reflexes")
if morph == "icewyrm" then
if reflexes == "off" then
Execute ("reflexes on")
elseif
if morph == "icewyrm" then
if reflexes == "on" then
Execute ("reflexes off")
elseif
Execute ("morph icewyrm;reflexes on")
end</send></alias>

Here's what I get

Compile error
Plugin: Core (called from world: Achaea)
Immediate execution
[string "Alias: "]:7: unexpected symbol near 'if'
#5
Actually, problems all over this thing. I'm gonna toy with it a moment.
#6
<alias
match="^ref$"
enabled="y"
group="Powers"
regexp="y"
ignore_case="y"
expand_variables="y"
send_to="12"
sequence="100">
<send>morph = GetVariable ("morph")
reflexes = GetVariable ("reflexes")
if morph == "icewyrm" then
if reflexes == "off" then
Execute ("reflexes on")
elseif
morph == "icewyrm" then
if reflexes == "on" then
Execute ("reflexes off")
else
Execute ("morph icewyrm;reflexes on")
end</send></alias>

I think this is closer to the truth, but still not working.
USA #7
Remember that you can use [code] tags. It makes code a lot easier to look at.
-- Remember to use 'local' within a trigger, unless
-- you're actually trying to set a global variable.
-- Local variables only last the lifetime of the
-- script/scope they're created in, which is usually
-- what you want.
local morph = GetVariable ("morph")
local reflexes = GetVariable ("reflexes")

if morph == "icewyrm" then
  -- Indenting makes code clearer.
  if reflexes == "off" then	    
    Execute ("reflexes on")
  elseif morph == "icewyrm" then
    if reflexes == "on" then
      Execute ("reflexes off")
    else
      Execute ("morph icewyrm;reflexes on")
    end
  -- Aha! You're missing two 'end's.
  -- See why indentation is nice?
  end
end


My comments/changes are in bold.
Amended on Fri 06 May 2011 11:06 AM by Twisol
#8
Thanks again for the help. It almost works. The first two parts are working great, where if you're Icewyrm, you can toggle Reflexes on and off. The last part is not. My assumption is because it checks through the rest of the script, notices that reflexes == "on" or reflexes == "off", and the rest of the script doesn't apply from there. I'm sure adding something like a


elseif
      morph !== "icewyrm" then
      Execute ("morph icewyrm;reflexes on")


would do the trick, but I seem to be blowing the syntax. One more nudge in the right direction? I tried adding another end at the bottom and flipping the !== to !=, but no go.
USA #9
You have a logic error, too. Look at the nested if-else's:

if morph == "icewyrm" then
  -- morph is icewyrm

  if reflexes == "off" then	    
    Execute ("reflexes on")
  elseif morph == "icewyrm" then
    -- why are we checking this again?
    
    if reflexes == "on" then
      Execute ("reflexes off")
    else
      Execute ("morph icewyrm;reflexes on")
    end
  end
end

My best guess is that you want:
if morph == "icewyrm" then
  if reflexes == "off" then
    Execute ("reflexes on")
  else
    Execute("reflexes off")
else
  if reflexes == "off" then
    Execute ("morph icewyrm;reflexes on")
  end
end

Looks like this can be cleaned up still:
if morph != "icewyrm" then
  Execute ("morph icewyrm")
end

if reflexes == "off" then
  Execute ("reflexes on")
else
  Execute("reflexes off")
end
#10
I like the second one. Clean, easy to read and understand. However, I'm getting a similar message as before.

Compile error
Plugin: Core (called from world: Achaea)
Immediate execution
[string "Alias: "]:3: 'then' expected near '!' 


Here's the whole thing thus far.

<alias
	match="^ref$"
	enabled="y"
	group="Powers"
	regexp="y"
	ignore_case="y"
	expand_variables="y"
	send_to="12"
	sequence="100">
	<send>local morph = GetVariable ("morph")
		local reflexes = GetVariable ("reflexes")
	    if morph != "icewyrm" then
  		Execute ("morph icewyrm")
		end
		if reflexes == "off" then
  		Execute ("reflexes on")
		else
  		Execute("reflexes off")
		end
	    </send></alias>


Also, I appreciate all the help I've gotten here thus far. Probably would've given up by now if not for the support.
USA #11
Oh, in Lua not-equal is ~=, not !=. Sorry about that.

Also, the indentation seems to be messed up in your script. Not sure if that's just me, but indentation is really, really helpful, I promise!
Amended on Fri 06 May 2011 08:07 PM by Twisol
#12
I was just about to post that. :) I'd found the answer in a Lua manual, cleared the whole thing up. Thanks for all the help.