Quantcast LoTROInterface - View Single Post - Lua Beginner has some questions
View Single Post
Unread 06-14-2016, 07:59 AM
Garan's Avatar
Garan Garan is offline
The Undying
Interface Author - Click to view interfaces
Join Date: Oct 2010
Posts: 270

The first thing I noticed in your code is that you are using SetParent on Window controls. Windows are top level objects and should never be used as child controls for two reasons. First, you should always use the object class that provides the needed functionality with the minimum of overhead - since all you are looking for is a container, a simple Control object is the best choice, Windows have a ton of additional functionality and thus overhead and will cause your plugin to run less efficiently. Second, some classes may include function definitions that were never meant to actually be implemented and implementing those functions can sometimes cause unreliable functionality - the SetParent function of the Window class is such a function (since Windows are top level and inherently parentless).

Additionally, I noticed that your ButtonHider is a child of the Quickslot - this is backwards, the quickslot should be a child of the container that is used to clip it. I don't know if assigning a child control to a quickslot could cause a crash as I've never tried it (but I probably will now, just out of curiosity). Your crash is probably a combination of assigning a child to a control that was never intended as a container AND that child being a window object.

The Unload handler does not have to be treated like an event handler in that it is not a shared function (no need for AddCallback). Theoretically you could use AddCallback if you wanted a table of Unload handlers (I can think of at least one use for this) but it is not necessary. There are two ways to assign the Unload handler due to a quirk in the way Lua works. When plugins were first implemented, there was no way for a plugin to reference itself while its code was being parsed and loaded so authors took to using an Update event to assign the Unload handler using the Plugins collection in the first Update event after the plugin started actually running. Turbine has since created the "plugin" object (notice the all lowercase name) which is a temporary reference to the instance of the plugin that is only valid while the plugin is loading so you can now assign your Unload handler by referencing your plugin as "plugin". Ex:
Typically the Unload event would be defined in your main lua file. You do not need to call RemoveCallback for the Unload event.
Most of my plugins use the Plugins collection and Update handler method just because most of my plugins were created prior to Turbine developing the "plugin" reference (and I'm just too lazy to go back and change all of them).

Another minor point, I noticed you created and use your shortcut as:
Shortcut = Turbine.UI.Lotro.Shortcut( Turbine.UI.Lotro.ShortcutType.Alias, "/w Hamkaas hoifcdfgdsfgdsfgsdfgsdfgsdfgsdfgsdfgdfsgsdfgsdfgsdfgsdfgsdfgsdf");
Unless you intend to reference Shortcut later (such as reassigning it) it is better to use a local variable or create it in-line without a variable otherwise the environment will keep a copy of "Shortcut" the whole time your plugin is loaded.
MainWindow.Button[1]:SetShortcut(Turbine.UI.Lotro.Shortcut( Turbine.UI.Lotro.ShortcutType.Alias, "/w Hamkaas hoifcdfgdsfgdsfgsdfgsdfgsdfgsdfgsdfgdfsgsdfgsdfgsdfgsdfgsdfgsdf"));
lets the garbage collection clean up the temporary object and is more efficient - plugins can get bloated and contribute to excessive memory usage and lag pretty quickly so writing efficient code can be important.

Last edited by Garan : 06-14-2016 at 01:07 PM. Reason: typo
Reply With Quote