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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  LUA 5.0 and 5.1.X with _VERSION

LUA 5.0 and 5.1.X with _VERSION

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


Posted by Onoitsu2   USA  (248 posts)  [Biography] bio
Date Fri 20 Apr 2007 11:37 AM (UTC)
Message
A while back Nick had posted for me that I could use the _VERSION internal variable to determine the version of LUA, due to some altering syntax after version 3.80 of MUSHclient.
I was wondering if this would work if placed in my plugin at the beginning where all the SCRIPT initialization takes place, i.e. prior to ANY function definitions?

if string.sub(_VERSION,1,7) == "Lua 5.1" then
string["gfind"] = string.gmatch
end -- if


This would be placed in my spellup plugin, so that IF IT WOULD WORK, I could distribute only the ONE version, and it be compatable. I have been informed by someone using 3.80 or higher that it presents an error message when using the string.gfind function, stating that it has been changed to string.gmatch, and am only inquiring as to the ability to OVERWRITE that by setting the "pointer" string.gfind to use the newly named string.gmatch... ?

I would test this myself but am still using 3.74, mostly for compatibility issues cause there are several people using 3.74-78 that use my plugins, and keep a few versions back for compatibilities sake.

Thanks,
Onoitsu2
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #1 on Fri 20 Apr 2007 04:16 PM (UTC)
Message
I'm not sure why that wouldn't work. Is the function api the same, and just the name different? Even if the api is slightly different it would be relatively easy to implement a "translation" wrapper.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

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

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Fri 20 Apr 2007 09:25 PM (UTC)
Message
You are trying to be forwards-compatible? I don't see a problem with that, in fact it could be simpler:


if string.sub(_VERSION,1,7) == "Lua 5.1" then
  string.gfind = string.gmatch
end -- if


And since there is no string.gfind in Lua 5.1, how about this...


string.gfind = string.gfind or string.gmatch


You don't need the version test, if it is version 5.1 string.gfind won't exist so it will replace it with string.gmatch.

The other thing is, it doesn't need to be done before any function definitions, just before function calls. For example, this compiles OK:


function test ()

 a, b = string.blahblahblah ("nick", "gammon", 42)

end -- test


It won't run, because there is no function string.blahblahblah, but syntactically it is fine.

Thus, you could put the line:


string.gfind = string.gfind or string.gmatch


... into the OnPluginInstall function.

- Nick Gammon

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

Posted by Onoitsu2   USA  (248 posts)  [Biography] bio
Date Reply #3 on Sat 21 Apr 2007 09:57 AM (UTC)
Message
The OnPluginInstall function is only called WHEN the plugin is installed, NOT every run of the world file in which the plugin is used, so that would not work in such a mannar, but I see where you mean it could be simplified by using the "EQUALS OR" method, but as has been previously stated on other postings, that can be misleading for people that have used other languages, and as will EVERYTHING I make, I want people to be able to learn from it the easiest way possible, even if it may not be the most efficient method for doing such a task.

I have implemented MY method, and am going to be using that in my distributed Installer, and ZIP'd versions in my spellup plugin, and others that use string.gfind.

Thanks again Nick...

-Onoitsu2
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Sat 21 Apr 2007 09:49 PM (UTC)
Message
Actually, OnPluginInstall is in fact called every time that plugin is loaded. How would MUSHclient know that it is being installed for the first time?

If you don't believe me, put the utils.msgbox below into one of your plugins, and then close the world and re-open it. Or, simply do a ReInstall from the plugin menu. Every time, the message will appear.


function OnPluginInstall ()
  utils.msgbox ("Plugin installed: " .. GetPluginInfo (GetPluginID (), 1))
end --



So, this is a logical place to put stuff that needs to be done once in the scripting space. For example:


  • Converting MUSHclient variables to script variables

  • Initialising variables (eg. creating tables)

  • Loading DLLs

  • Loading "requires" files


Admittedly you would probably get much the same result by simply putting your code in the plugin outside any function definition, which therefore will be run as the script engine is initialized.

The slight difference is that the plugin is not yet added to the internal list of plugins at that point.

- Nick Gammon

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

Posted by Onoitsu2   USA  (248 posts)  [Biography] bio
Date Reply #5 on Sun 22 Apr 2007 09:32 AM (UTC)
Message
I can guarantee from usage, that OnPluginInstall IS NOT called on EVERY opening of the world, because ALL of my plugins use that function to call the OnHelp function, which displays the help information for the plugin, and that DOES NOT happen on every opening of the world, or else that would be terribly annoying. Perhaps calls that present OUTPUT, i.e. Note, Tell, ColourNote, ColourTell, etc. are not "displayed" but may be run, but that is where I being lead to believe that OnPluginInstall is only being used WHEN it is installed into the world file. Using the Reinstall button WILL do the same thing as removing and re-adding the plugin, thereby running the OnPluginInstall function, but opening and closing a world, again DOES NOT "appear" to be using that function.

Thanks,
Onoitsu2
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Sun 22 Apr 2007 10:43 AM (UTC)
Message
I believe you. :)

That is why I did utils.msgbox in my example, and not just:


Note ("plugin installed")


The reason is that MUSHclient loads the world file first, including plugins, and *then* sets up the output window. The reason is that it has to read the world file to find the font, font size, output window width etc.

Thus, any world.Note in the OnPluginInstall function (when the world is loading) are discarded because the output window is not ready yet. That doesn't mean it hasn't run. If you reinstall the plugin they appear, because the output window has been initialized. That is why you see the message if you reinstall the plugin.

In a couple of my plugins, where I want to announce the plugin install in the OnPluginInstall I actually do:


DoAfterNote (1, "plugin installed")


That small delay lets you see the message.


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


16,504 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]