[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Variable not updating?

Variable not updating?

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


Posted by Mike.mac.kenzie   (13 posts)  [Biography] 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?
[Go to top] top

Posted by Fiendish   USA  (2,525 posts)  [Biography] 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
[Go to top] top

Posted by Mike.mac.kenzie   (13 posts)  [Biography] 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?
[Go to top] top

Posted by Nick Gammon   Australia  (23,016 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (23,016 posts)  [Biography] 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
[Go to top] 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.


1,969 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]