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, 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.
 Entire forum ➜ MUSHclient ➜ General ➜ Comparing 2 variables in an if statement.

Comparing 2 variables in an if statement.

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


Posted by Zack   (23 posts)  Bio
Date Thu 19 Jan 2023 06:01 AM (UTC)
Message
Hey all,
Is it possible to have 2 variables in an if statement?
Here's what I'm trying to create.
if @steak and @chicken == "0" then
Send("cook fish")
end -- if
Trying to create an auto cooking script. It looks at my inventory, sets the amount of each type of food in their own variables. That part works fine but I'm having trouble comparing the first two to see if it should ignore them to cook the third item.

I know you can compare wild cards like on a prompt.
%1 current hp
%2 max hp.
if %1/%2 ,= .15 then
Send("cast heal")
end -- if
However I wasn't having much luck with pulling from variables outside of triggers.
Please note the prompt was just an example, the first if then is what I'm working on.
I've searched the site but can't seem to find the right phrases to find the answer.
Thanks in advance.
Top

Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Reply #1 on Thu 19 Jan 2023 07:32 AM (UTC)
Message
Certainly you can have complex conditions in an "if".

It would look like this:


if @steak == "0" and @chicken == "0" then
  Send("cook fish")
end -- if

- Nick Gammon

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

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #2 on Thu 19 Jan 2023 03:24 PM (UTC)

Amended on Thu 19 Jan 2023 03:29 PM (UTC) by Fiendish

Message
I advise getting out of the habit of using @variables inside scripts and only using them when needed for variable match patterns. They're evaluated and replaced before the code gets sent to the script execution engine, which means that they will not pick up any changes that occur during your script. That's fine much of the time, but you will eventually run into a case where a script is broken because of this and you won't know why.

Use @variable expansion in match patterns because that's necessary for matching against variable contents, but inside your script the GetVariable function will provide more consistently sane results.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Reply #3 on Fri 20 Jan 2023 06:38 AM (UTC)

Amended on Fri 20 Jan 2023 06:39 AM (UTC) by Nick Gammon

Message
Further to that, it is also preferable to convert them to numbers, particularly if you are going to compare to, say less than 1000.

So it would preferably look like:




-- convert MUSHclient variables to Lua variables
local steak   = tonumber (GetVariable ("steak")) or 0
local chicken = tonumber (GetVariable ("chicken")) or 0

-- test them
if steak == 0 and chicken == 0 then
  Send("cook fish")
end -- if


The stuff about "or 0" is to allow for if the variables don't exist at all, and give them the default values of zero.

- Nick Gammon

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

Posted by Zack   (23 posts)  Bio
Date Reply #4 on Sat 21 Jan 2023 05:09 AM (UTC)
Message
Excellent, thank you all so much for the help. I'm always looking for better ways to work within MC so will incorporate the feedback.
I've been working with GetVariable but wasn't sure if one could fit it into an if then statement. I primarily used it when doing math on variables.
I appreciate your help.
I love that the community is always helpful.
Take care all.
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.


5,642 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.