table behaviour
following roughly outlined lua file:
Lua Code:
(its a rough downgrade of one of the tutorials from the wiki...) ingame I get following error message: "function expected instead of nil" what I don't understand: In the tutorial it worked but here not? |
function.OnAddOnLoaded() is not a thing
|
Quote:
Anyway: in the tutorial its exactly the same...: https://wiki.esoui.com/Writing_your_first_addon strangly I can't poste img here: so here is a screenshot from the tutorial sou you guys don't need to search: https://imgur.com/a/rGXFEEF |
"function" is a keyword, not a table. When you write "function.OnAddOnLoaded()" it's invalid code. Look closely at the tutorial in your image. It has "function FooAddon.OnAddOnLoaded" in that line.
|
First of all you should decide either using . or :, but not a mixture (where not needed).
: : is used for the kind of object-oriented approach where you define a "class" and create objects of that class, and then you call functions of that object referencing it via self. -> Where the "class" is only a table which the objects use via metatables to "inherit the functions and attributes of the parent class table". Classes in ESO lua are defined using ZO_Object:Subclass() e.g. like this: Lua Code:
ZO_PlatformStyleManager is the "class", and this function creates an object "PLATFORM_STYLE_MANAGER " of that class: Lua Code:
objects using the : to call functions are able to use the variable "self" in their function code to reference the object variable. Lua Code:
As the created object of ZO_PlatformStyleManager is PLATFORM_STYLE_MANAGER (See above), self.objects will be PLATFORM_STYLE_MANAGER .objects then. If you do not plan to create a class and have an object, or even multiple objects (like a logger library/addon creates multiple loggers with base function ":log" [defined at the super class] e.g., but different functions for each logger e.g. :warning, :error [defined at each individual logger]) you shouldn't use : That's my opinion so feel free to do like you wish and prefer :p . . just is using a "table", no object-oriented approach, no metatables. You define a table Lua Code:
Lua Code:
variables using the . to call functions are not able to use the variable "self" in their function code to reference the table variable. If you want to specify any object/table you reference to, you need to name it by it's name. Lua Code:
The object example function "ZO_PlatformStyleManager:Add(object)" from above, using the :, would look like this using the . instead: Lua Code:
The call to the function would not be this anymore: Lua Code:
Lua Code:
Again this is only my opinion: You should stick to using the . way and normal tables to keep it easy, where possible. But this is totally up to you. You can create your addon as a class + object, or even without deifning it as a class (ZO_Object:Subclass()) you ca still use the : . But it does not make that much sense, or at least I did not get why it would so far :D |
Additionally to what is said above, make sure you're consistent with your table names.
On line 1 you declare a table called addonName On line 2 you try and place a string into addon.name, but the table 'addon' is not the same as 'addonName'. |
many spelling problems in my example due to hastly thrown together "forum exapmple":
Quote:
Quote:
should be: function addonName.OnAddOnLoaded() ... |
interestingly I got it to work with a metatable which name is not equal to the addon name...?
|
Quote:
|
All times are GMT -6. The time now is 08:18 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI