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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Lua version of "elsif"? NEW: more if/then questions

Lua version of "elsif"? NEW: more if/then questions

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


Pages: 1  2 

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #15 on Wed 21 Jul 2010 03:51 PM (UTC)
Message
Quote:
When you set a variable, is it stored and ready to use by any trigger or alias that might call on it?

For MC variables, yes; for Lua variables, no. Even a 'global' Lua variable is constrained to its plugin's environment, if I remember correctly. Beyond that are the issues of block-locality that Twisol explained.

Even if the Lua global were truly global, it would only be to the Lua state and not usable by other languages, or in triggers etc.; that's where the MC variables come in.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #16 on Wed 21 Jul 2010 05:01 PM (UTC)

Amended on Wed 21 Jul 2010 05:08 PM (UTC) by Twisol

Message
David Haley said:
For MC variables, yes;

With the stipulation that you have to use GetPluginVariable() to access another plugin's variables, and you can't set them.

David Haley said:
Even a 'global' Lua variable is constrained to its plugin's environment, if I remember correctly. [...]

Even if the Lua global were truly global, it would only be to the Lua state and not usable by other languages, or in triggers etc. [...]


I'm confused by what you're trying to say. If you set a variable in a trigger or alias, and it doesn't have 'local' on it, then it's added to the Lua environment table _G. If a variable lives in _G, it's called a global. I'm not seeing how it's not "truly" global except where it can't be accessed from other plugins, which is logical because they each get their own instance of the Lua interpreter. Care to clarify?

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #17 on Wed 21 Jul 2010 05:59 PM (UTC)
Message
Twisol said:
I'm not seeing how it's not "truly" global except where it can't be accessed from other plugins

Exactly, hence it's not "truly" global... why do you say that you don't understand why it's not truly global when you add your own caveats explaining why it's not? ;)

The question was about variables across triggers, aliases, etc., and whether Lua variables can be used to do the same thing as MC variables. And the answer is no, not really, because a plugin's "global variable" is not truly global to MC.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Nick Gammon   Australia  (23,001 posts)  [Biography] bio   Forum Administrator
Date Reply #18 on Wed 21 Jul 2010 09:04 PM (UTC)
Message
Just to clarify for the original poster, plugins are intended to have separate script and variable spaces. This is so that, whether you use MUSHclient variables, or script variables, there is no clash between one plugin and another.

For example, one plugin author might have a variable "count" which is a count of mobs killed, and another one might have a variable "count" which is a count of the times you levelled.

The "main world" file (that is, outside any plugin) is effectively another script and variable space again (so plugins don't clash with non-plugins).

So, in the main world file, or inside a particular plugin, you have two ways of storing and accessing variables:


  • MUSHclient variables
  • Script-language variables (whatever the language is)


The MUSHclient variables have the advantage you can access them from triggers (like matching on @mobname in a trigger). They are also saved from one session to another, to disk, assuming you save your world file. Plugin variables are saved to disk if you enable the "save state" flag when writing the plugin.

Script language variables on the other hand are easier to use. Compare:


count = count + 1  -- script variable

SetVariable ("count",  tonumber (GetVariable ("count")) + 1) -- MUSHclient variable


Also script language variables are not saved to disk unless you take steps to do so. This is called serialization, and often takes the form of converting script variables to MUSHclient variables at the last moment, so they get saved to disk. Then next time the world is opened you have to convert them back.

Moving onto the "local" issue - Lua variables are generally speaking "global" - that is, available to every alias, plugin, or script in general. This means a count of mobs killed you might add to in a trigger, is also accessible in an alias.

However if you use the "local" keyword, then that variable is not stored in the "global environment" and becomes local to the block where it is declared. For example:


local x

x = 22
x = x + 1


This code (if in a trigger or alias script) would not change a variable "x" used "globally" elsewhere. This may well be what you want, if you are just using "x" as a "work" variable.

So, like with many things, it is a case of using the right tool for the job. Local variables have their uses, and MUSHclient variables have their uses too. Choose one that suits what you are trying to do.

- Nick Gammon

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

Posted by Caelen   (81 posts)  [Biography] bio
Date Reply #19 on Thu 22 Jul 2010 02:29 PM (UTC)
Message
It all makes sense now! I will just continue to use the slightly more lengthy mush variables for my assorted triggers, since I usually want my variables saved to the disk. I don't think I've needed a temporary variable for anything yet... I'll keep it in mind though. Never know when something I have no use for will suddenly decide it's time to be used!
[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.


49,552 views.

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

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]