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 and If and If

If and If and If

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


Posted by Deacla   USA  (42 posts)  Bio
Date Mon 12 Jul 2010 07:16 AM (UTC)
Message
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.

--

working way to hard to play
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #1 on Mon 12 Jul 2010 07:24 AM (UTC)

Amended on Mon 12 Jul 2010 07:29 AM (UTC) by Twisol

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

'Soludra' on Achaea

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

Posted by Deacla   USA  (42 posts)  Bio
Date Reply #2 on Mon 12 Jul 2010 07:57 AM (UTC)
Message
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.

--

working way to hard to play
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #3 on Mon 12 Jul 2010 08:06 AM (UTC)
Message
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.


- Nick Gammon

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

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #4 on Mon 12 Jul 2010 08:10 AM (UTC)
Message
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.

'Soludra' on Achaea

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

Posted by Deacla   USA  (42 posts)  Bio
Date Reply #5 on Mon 12 Jul 2010 08:35 AM (UTC)
Message
@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.

--

working way to hard to play
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #6 on Mon 12 Jul 2010 08:45 AM (UTC)

Amended on Mon 12 Jul 2010 08:46 AM (UTC) by Twisol

Message
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! :)

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
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.


19,885 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.