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

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Lua variables scope and Mush
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Lua variables scope and Mush

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


Posted by Oligo   (26 posts)  [Biography] bio
Date Tue 22 May 2012 05:03 AM (UTC)
Message
So I'm still ramping up with Lua, but I have the basic understanding of the Lua language that when variables are created, they default to global scope unless you explicitly declare them local with the local prefix.

However, I noticed for example a table I declared in one alias and populated using tablet.insert() can be referenced in another alias. Do I have to worry about garbage collection or not being able to reference a variable that was created in another alias? Or is there a primer on how scope works within Mush.

I guess the reason this is foreign or novel to me, is because in Wintin.Net, each alias or trigger was basically a self contained class with scope only to its respective alias or trigger. Other aliases/triggers didn't have any visibility to it and couldn't instantiate it.

mud.arctic.org : 2700
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Tue 22 May 2012 07:15 AM (UTC)
Message
Can you show both aliases?

Template:copying For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Tue 22 May 2012 07:17 AM (UTC)
Message
Each trigger and alias shares the same script space. It would be hard to script if they didn't. So for example, an alias can set up a variable (eg. which mob to kill) and a trigger can check if you killed it.

However the local keyword should limit the scope of the variables somewhat. I would have to see your examples to comment further.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Worstje   Netherlands  (899 posts)  [Biography] bio
Date Reply #3 on Tue 22 May 2012 07:28 AM (UTC)
Message
You need to think of the language and of MUSHclient as seperate beasts. In this case, your confusion simply lies in understanding where MUSHclient begins, and Lua ends.

This is complicated by the fact there are basically two ways to use scripts in MUSHclient; and I'll go over them briefly.

The 'easy' way is to use the 'Send To: [Script]' option, and input your script in the 'Send:' box. I call it easy because it is quick to use. However, MUSHclient also does some fancy things like substitute things like %1 or (if expand variables is checked) @hp into their contents, and this often leads to confusion with new scripters:

if @person == "Marcus" then Note("It's Marcus.") end

gets translated to
if Marcus == "Marcus" then Note("It's Marcus.") end

which means it is interpreted as a variable... which Lua does not throw errors on, but substitutes with nil if they do not exist. Correct is:
if "@person" == "Marcus" then Note("It's Marcus.") end


Long story short: there are pitfalls with that method, and given the fact you are used to a more controlled scripting environment already I advise to avoid this method. (This method is also slower because MUSHclient can't compile the script for obvious reasons and re-use that. But that's a minimal performance hit unless the script gets executed every other line.)

The method I prefer is what I already explained in the other topic you started, where you misunderstood how to use it. Basically you declare a function that takes certain inputs in your script file, and you tell MUSHclient the name of this function inside the trigger/alias/timer Script: box. When this script needs executing, MUSHclient simply tells the scripting engine to call that function; it can't be any simpler nor faster from MUSHclient's side.

Now to answer your question about scope...

Scope depends on the language, and how the language deals with this. Lua can be complicated with scopes, but the MUSHclient way is simple: there is a global scope, and all variables (functions are variables of the function type!) are declared in that.

So whenever MUSHclient does something, you need to imagine it as MUSHclient picking off from the bottom of the script you have loaded. An even better idea is an interactive terminal like Lua and Python offer; except MUSHclient is the one typing the new lines.

You do not need to worry about garbage collection unless you let your script run for months on end, and it litters very much in the global script space (think thousands of variables). And if that is the case, you may need to put such information in a separate table which you can easily clear out when the time comes.

If you are a clean coder, you'll figure out conventions and a consistency that works well for you. In Lua, always use the 'local' specifier unless you definitely need the information at a later stage.

Sorry for the long-windedness... but I hope it helps you out. :-)
[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.


3,937 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 FutureQuest]