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 ➜ Variables & Local Variable in LUA

Variables & Local Variable in LUA

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


Pages: 1 2  

Posted by Tkl1129   Hong Kong  (43 posts)  Bio
Date Thu 07 Jul 2011 05:33 AM (UTC)
Message
I'm using the Immediate Box to testing code...

I found that if I set a = 1,
and then do another caluculation like b = 1+2+3...just for an example, when I close the box, and try /print(a)
it show "1", if like that, why I'm able to SetVariable to Mush, and do local var in script, only type a = xxx also shown as a global var...

Thanks, I'm newbie on Lua & Mush :D
Top

Posted by Fiendish   USA  (2,535 posts)  Bio   Global Moderator
Date Reply #1 on Thu 07 Jul 2011 06:32 AM (UTC)
Message
It looks like the Immediate box and the input bar just share the same local scope.

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

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #2 on Thu 07 Jul 2011 06:41 AM (UTC)
Message
If you're just using "a = 1", that sets 'a' as a global. You need 'local a' first so Lua knows you want it to be a local. Globals are accessible from anywhere (aliases, triggers, any kind of script except other plugins), which is why you can see 'a' from other places.

If you were to use 'local a = 1' in the Immediate window, you wouldn't have access to that local after the Immediate code is finished running.


SetVariable() is useful for a few things. The big three I know of are: (1) exposing values to other plugins that use GetPluginVariable(); (2) saving the plugin's state between MUSHclient sessions; (3) expanding into the match text of a trigger with "Expand variables" enabled. If you're not doing any of these things, plain Lua variables are probably better. Also SetVariable/GetVariable values can only be strings. Lua values can be strings, numbers, tables, functions, et cetera.

'Soludra' on Achaea

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

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #3 on Thu 07 Jul 2011 07:15 AM (UTC)
Message
Fiendish said:

It looks like the Immediate box and the input bar just share the same local scope.


They do.

See http://gammon.com.au/forum/?id=10863 for a discussion of variables.

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #4 on Thu 07 Jul 2011 07:19 AM (UTC)
Message
Although I should amend that to "scope" not "local scope".

I think the immediate window will be a compilation block and local variables will only have scope there.

- Nick Gammon

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

Posted by Tkl1129   Hong Kong  (43 posts)  Bio
Date Reply #5 on Thu 07 Jul 2011 07:33 AM (UTC)
Message
Oh..-.-"

Thats mean unless I'm able to share value via different groups or plugins that I will use SetVaribles in mush, if just for normal coding, I use a = 1 is more convinience, right? I still have few questions.

1. Do the Lua Global Variables eat memory? since I don't know how many I'd add before, is that a good practice that always "nil" to delete it? :D

2. In same script
a = 1 -- LUA Global Var
Local a = 2 -- Lua Local Var
print(a) -- return local var

if in this case, I'm sure better won't set in same name is better but if really got same name, can I get the value at the same time?...

I just reading the Programming in Lua ...Ch.1 to 4...-_-!..drive me crazy as I'm only know zscript before...my god~~~
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #6 on Thu 07 Jul 2011 07:43 AM (UTC)
Message
Anything eats memory, but values are garbage-collected eventually. Setting a variable to nil will encourage that.


print(a)


prints either a local or global variable.

- Nick Gammon

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

Posted by Tkl1129   Hong Kong  (43 posts)  Bio
Date Reply #7 on Thu 07 Jul 2011 07:47 AM (UTC)
Message
yeah..It print(a) --> LUA local value,
but how do I get the LUA Global value "1"?

Global a is "1"
Local a is "2"

after those code print(a) = 2

then I have no chance to get Global one in this case?
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #8 on Thu 07 Jul 2011 07:51 AM (UTC)

Amended on Thu 07 Jul 2011 07:53 AM (UTC) by Twisol

Message
Tkl1129 said:
Thats mean unless I'm able to share value via different groups or plugins that I will use SetVaribles in mush, if just for normal coding, I use a = 1 is more convinience

Well, sharing values between plugins is relatively rare, but if that's your aim then SetVariable and GetPluginVariable are definitely what you want. For normal coding, the choice between global and local depends on how long you need the value. If you store the result of a calculation in a variable and only Note() it immediately afterwards, you'd want a local. But if multiple aliases/triggers/times/etc. want use a single variable, a global is the simplest solution.

Tkl1129 said:
1. Do the Lua Global Variables eat memory? since I don't know how many I'd add before, is that a good practice that always "nil" to delete it? :D

If they take more room than a local, it's probably a very small amount. Locals can be used very quickly with a single Lua operation, but globals take an extra operation to load the value from the globals table. In practice it's not going to make any difference.

But yes, it's a good idea to set a global to nil when you're done using that value. It'll at least save space in the globals table. Locals are deleted automatically when they go out of scope, so you don't have to worry about them.

Tkl1129 said:
2. In same script
a = 1 -- LUA Global Var
Local a = 2 -- Lua Local Var
print(a) -- return local var

if in this case, I'm sure better won't set in same name is better but if really got same name, can I get the value at the same time?...

Yes, that's how the code will behave. This is called "shadowing", because the global of the same name is basically hidden in the local's shadow. You can't access a shadowed variable until the name shadowing it goes out of scope. However, globals have a workaround in that you can use _G["a"] to access a global by name. _G is the name given to the special table where globals are stored. Of course, if you had a local named _G as well you'd be out of luck! (Also, the 'local' keyword must be all-lowercase. ;D)

More precisely, 'local' creates a new slot for a local variable, and binds the name to that slot. Until the new binding goes out of scope, you can't access the shadowed variable.
local a = 42  -- slot 0
do
  local a = 50  -- slot 1
  print(a)  -- prints 50
end
print(a)  -- prints 42


Tkl1129 said:
I just reading the Programming in Lua ...Ch.1 to 4...-_-!..drive me crazy as I'm only know zscript before...my god~~~

Hah, it's a pretty big change, yeah. If anything I've explained here is confusing, don't worry too much about it - I'm dipping into some lower-level details in some places. The vanilla mechanisms are really all you need to worry about.

'Soludra' on Achaea

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

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #9 on Thu 07 Jul 2011 07:59 AM (UTC)

Amended on Thu 07 Jul 2011 08:00 AM (UTC) by Nick Gammon

Message
Tkl1129 said:

... then I have no chance to get Global one in this case?


Well you do, but I would reconsider why you would want to. Perhaps different names?

Consider this:


a = 1
print (a) --> 1

local a = 2

print (a) --> 2


However you can get at the global "a" in a couple of ways:


print (_G.a) --> 1 
print (getfenv () . a) --> 1


First the _G table is a table of global variables, therefore getting key "a" from it gives global "a".

However you *could* assign nil to _G (although this might be a strange thing to do). But the getfenv function always returns the "environment" in which you are currently running (which is a table) and thus you can then dereference that to get the global variable "a".

But I recommend against such convoluted programming style.



- Nick Gammon

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

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #10 on Thu 07 Jul 2011 08:02 AM (UTC)

Amended on Thu 07 Jul 2011 08:03 AM (UTC) by Twisol

Message
Nick Gammon said:
However you *could* assign nil to _G (although this might be a strange thing to do). But the getfenv function always returns the "environment" in which you are currently running (which is a table) and thus you can then dereference that to get the global variable "a".

However you *could* assign nil to getfenv, as well. Lovely!

The most bulletproof thing to do is indeed to just use a different name. :D

'Soludra' on Achaea

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

Posted by Tkl1129   Hong Kong  (43 posts)  Bio
Date Reply #11 on Thu 07 Jul 2011 08:05 AM (UTC)
Message
Oh nice,
I know the concept now XD

slot 0 -> 1

It's a good practice that I always use "do" + "end"..I think :P

ya..as I play zmud for 5+ years, it's really hard to change my mind to accept a new language, but for long term I think LUA is more usful and powerful :P..
Top

Posted by Tkl1129   Hong Kong  (43 posts)  Bio
Date Reply #12 on Thu 07 Jul 2011 08:11 AM (UTC)
Message
*could" XD...I like this :P

and I just do a stupid thing...LUA Global, after quit Mush, reopen world, the LUA global var return to nil XD

thats mean I don't have to "nil" everytime....actually just use another name is more better -_-"

just my logical question about LUA...Thanks to ALL~~ O_O"
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #13 on Thu 07 Jul 2011 08:55 AM (UTC)
Message
Yep, no Lua variables are saved when the program ends. MUSHclient does save the contents of the SetVariable/GetVariable variables though, which as I noted is one of their three big uses. :)

'Soludra' on Achaea

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

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #14 on Thu 07 Jul 2011 10:01 AM (UTC)
Message
Twisol said:

However you *could* assign nil to getfenv, as well. Lovely!


Lol. Yes you *could* do that.

There are a lot of things you could do. ;)

@Tkl1129 - the language is Lua, not LUA. May as well get the name right.

It is very powerful, that power can be abused. Your ultimate goal would hopefully be to write scripts that work well, and also are very easy to maintain.

- 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.


80,498 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

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.