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.
 Entire forum ➜ MUSHclient ➜ General ➜ Variable not updating?

Variable not updating?

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


Posted by Mike.mac.kenzie   (13 posts)  Bio
Date Tue 13 Feb 2024 04:15 AM (UTC)
Message
So I'm trying to use variables to do some different things, but I'm having a problem with variables not updating. When I use the following code, it always notes the variables as the same thing even though I have changed the variable to a new thing.

SetVariable("thing", "1")
Note("@thing") -- shows 1
SetVariable("thing", "2")
Note("@thing") -- shows 1

then when I run that again, both notes show as 2 instead of one. How do I get the proper numbers to show?
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #1 on Tue 13 Feb 2024 05:16 AM (UTC)

Amended on Tue 13 Feb 2024 05:17 AM (UTC) by Fiendish

Message
Nobody should ever use the @thing notation for variables inside script code, because basically nobody understands what it actually does, what it's for, and when to not use it.

Use Note(GetVariable("thing")) instead and it will do what you expect.

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

Posted by Mike.mac.kenzie   (13 posts)  Bio
Date Reply #2 on Tue 13 Feb 2024 08:34 PM (UTC)
Message
What if I wanted to update the variable and then run an ifcheck on it right after? How would I get that to work correctly?

SetVariable("thing", "1")
if @thing == 1 then
Send("dosomething")
end

Wont this also not work correctly?
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #3 on Tue 13 Feb 2024 09:09 PM (UTC)

Amended on Tue 13 Feb 2024 09:12 PM (UTC) by Nick Gammon

Message

The expanding of variables is done at the start of script execution. Thus, changing a variable and testing it in the same script will always return the original variable contents.

So this won’t work:

SetVariable("thing", "1")
if @thing == 1 then
    Send("dosomething")
end

This will:

SetVariable("thing", "1")
if GetVariable ("thing") == "1" then
    Send("dosomething")
end

Note that variables are stored as strings, not numbers, which is why I quoted the “1”.

What is easier anyway is to use Lua variables, if you don’t need them to persist from one session to another, ie.

thing = 1
if thing == 1 then
    Send("dosomething")
end

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #4 on Wed 14 Feb 2024 05:43 AM (UTC)
Message
Here is a video explaining variables in general:

http://www.gammon.com.au/forum/?id=10863

- Nick Gammon

www.gammon.com.au, www.mushclient.com
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.


3,277 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.