If and If and If

Posted by Deacla on Mon 12 Jul 2010 07:16 AM — 7 posts, 27,811 views.

USA #0
I did a lot of searching around to find how to make an if statement with multiple clauses I'm trying to do something like this:

if x = 0 and y = 0 and z = 0 then

  Do_Stuff()

else

  Do_Stuff()
  Do_Other_Stuff()

end

I'm trying to do this in lua but can't find the correct language. I've also tried nesting them like this:

if x = 0 then
  if y = 0 then
    if z = 0 then

      Do_Stuff()

    else
  else
else

  Do_Stuff()
  Do_Other_Stuff()

end
end
end

But, i must have it out of order because that doesn't work either. Thanks in advance guys.
USA #1
Nope, you just need to use ==. One equals sign is assignment, two equals signs is comparison. Like so:

if x == 0 and y == 0 and z == 0 then
  Do_Stuff()
else
  Do_Stuff()
  Do_Other_Stuff()
end


Of course, I wouldn't be doing my job if I didn't point out that since both branches execute Do_Stuff() immediately, you can factor it out:

Do_Stuff()

if x == 0 and y == 0 and z == 0 then
  ; -- nothing
else
  Do_Other_Stuff()
end


And we can reverse the sense of the condition to clean it up a bit:

Do_Stuff()
if x ~= 0 or y ~= 0 or z ~= 0 then
  Do_Other_Stuff()
end


I know your example was contrived, but, well... I'm a perfectionist. I hope I helped clear up if-statements a bit for you!
Amended on Mon 12 Jul 2010 07:29 AM by Twisol
USA #2
Thank you! as always your advice was perfect. And for your prize, here is what I was actually doing:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="MM_Extended_Prompt"
   author="Brian Lake"
   id="01f94fbc27ac45564681f20a"
   language="Lua"
   purpose="Extends Prompt Functionality"
   date_written="2010-06-26 10:04"
   requires="4.37"
   version="1.0"
   >
<description trim="y">
<![CDATA[

Extends the prompt to show the amount of change
 in HP, SP and ST per round. 

]]>
</description>

</plugin>

<triggers>
  <trigger
   enabled="y"
   match="^(?:.+|)\<(?P<hp>[0-9]{1,4})hp (?P<sp>(?1))sp (?P<st>(?1))st\>"
   omit_from_output="y"
   regexp="y"
   send_to="14"
   sequence="90"
  >
  <send>
HP = %1
SP = %2
ST = %3

  for _,v in ipairs(TriggerStyleRuns) do
    ColourTell(RGBColourToName(v.textcolour), RGBColourToName(v.backcolour), v.text)
  end

if (HP - Old_HP) ~= 0 or (SP - Old_SP) ~= 0 or (ST - Old_ST) ~= 0 then

  ColourTell("mediumblue","black", "[ ")

  if (HP - Old_HP) > 0 then

    ColourTell("green","black", (HP - Old_HP))
    Old_HP = HP
  elseif (HP - Old_HP) == 0 then
    ColourTell("silver","black", (HP - Old_HP))
    Old_HP = HP
  else
    ColourTell("firebrick","black", (HP - Old_HP))
    Old_HP = HP
  end

  ColourTell("silver","black"," - ")

  if (SP - Old_SP) > 0 then
    ColourTell("green","black", (SP - Old_SP))
    Old_SP = SP
  elseif (SP - Old_SP) == 0 then
    ColourTell("silver","black", (SP - Old_SP))
    Old_SP = SP
  else
    ColourTell("firebrick","black", (SP - Old_SP))
    Old_SP = SP
  end
  
  ColourTell("silver","black"," - ")

  if (ST - Old_ST) > 0 then
    ColourTell("green","black", (ST - Old_ST))
    Old_ST = ST
  elseif (ST - Old_ST) == 0 then
    ColourTell("silver","black", (ST - Old_ST))
    Old_ST = ST
  else
    ColourTell("firebrick","black", (ST - Old_ST))
    Old_ST = ST
  end

  ColourTell("mediumblue","black"," ]")

end

Note() -- finish the line</send>


 
 </trigger>

</triggers>

<script>
<![CDATA[
]]>
function OnPluginInstall ()
  
Old_HP = 0
Old_SP = 0
Old_ST = 0
 
end -- OnPluginInstall
</script>
</muclient>

If you can't immediately tell, it's a little plugin to extend the prompt in Materia Magica to show the amount of change in Hit Points, Spell Points, and Stamina per round, if and only if there has been any change.
Australia Forum Administrator #3
Yes, very nice. And you could make a huge big piece of text appear on the screen with "+15 HP" in HUGE letters.

Template:post=9225
Please see the forum thread: http://gammon.com.au/forum/?id=9225.

USA #4
Ah, I see! Well, here's another trick for you. Just as I moved something that was common to the start of every branch to before the if statement, you can move common lines from the end to after the if statement. Like so:

if (HP - Old_HP) > 0 then
  ColourTell("green","black", (HP - Old_HP))
elseif (HP - Old_HP) == 0 then
  ColourTell("silver","black", (HP - Old_HP))
else
  ColourTell("firebrick","black", (HP - Old_HP))
end
Old_HP = HP


In general it's an excellent idea to DRY* up your code, because it makes it much easier to maintain and make changes later.

* DRY stands for Don't Repeat Yourself, though it's often used as an adjective or verb.
USA #5
@Nick Thanks for that link! I can use that to throw status affects up in my face like WEBBED or BLIND or SILENCED. It would even have a nice role-playing aspect to it...

@Twisol I am in total agreement about every single piece of code I have ever written. I AM only a hobbyist. I am offender #1 when it comes to dirty code. You're quite active around here. Have you seen any of my earlier posts on Materia Magica plugins that i wrote? my HP gauge plugin is a BEAST!! But it tracks every useful stat in the game.
USA #6
I think I have, but I have a terrible memory. I'm usually pretty quick and dirty at first too, but if something is just details that obscures the function's actual behavior, I usually push it into its own function for clarity. Basically, I just keep things compartmentalized so nothing gets so monolithic that you get bogged down in the details. I think that's one of the most important things to get right.

And just for the record, I'm also a hobbyist. Most likely, the only major difference between us is how long we've been hobbyists! :)