ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   table behaviour (https://www.esoui.com/forums/showthread.php?t=9387)

Mipps 09/22/20 07:15 PM

table behaviour
 
following roughly outlined lua file:

Lua Code:
  1. addonName = {}
  2. addon.name = "..."
  3.  
  4. function addonName:foo()
  5.    ...
  6. end
  7.  
  8. function.OnAddOnLoaded()
  9.    addonName:foo()
  10. end
  11.  
  12. EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_ADD_ON_LOADED, addon.OnAddOnLoaded)

(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?

Rhyono 09/22/20 07:49 PM

function.OnAddOnLoaded() is not a thing

Mipps 09/23/20 02:24 AM

Quote:

Originally Posted by Rhyono (Post 42303)
function.OnAddOnLoaded() is not a thing

As far as my understanding of lua: you can freely add new methods to a table? (line 4-6)

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

sirinsidiator 09/23/20 05:01 AM

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

Baertram 09/23/20 05:05 AM

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:
  1. local ZO_PlatformStyleManager = ZO_Object:Subclass()
  2.  
  3. function ZO_PlatformStyleManager:New()
  4.     local obj = ZO_Object.New(self)
  5.     obj:Initialize()
  6.     return obj
  7. end

ZO_PlatformStyleManager is the "class", and
this function creates an object "PLATFORM_STYLE_MANAGER " of that class:
Lua Code:
  1. local PLATFORM_STYLE_MANAGER = ZO_PlatformStyleManager:New()

objects using the : to call functions are able to use the variable "self" in their function code to reference the object variable.
Lua Code:
  1. function ZO_PlatformStyleManager:Add(object)
  2.     table.insert(self.objects, object)
  3. end
self.objects will be the table "objects" at self, where self is the created object of class ZO_PlatformStyleManager.

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:
  1. addonName = {}
and define variables, subtables, function and assign it to the table using the .
Lua Code:
  1. addonName.subTable = {}
  2. addonName.var1 = "Hello world"
  3. function addonName.myFunc(param1) end

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:
  1. function addonName.myFunc(param1)
  2.  --self is not given here! If you want to reference variables), just use the variable name
  3.  
  4. end

The object example function "ZO_PlatformStyleManager:Add(object)" from above, using the :, would look like this using the . instead:
Lua Code:
  1. function ZO_PlatformStyleManager.Add(objectVariableOfClass, object)
  2.     table.insert(objectVariableOfClass.objects, object)
  3. end
The 1st parameter of the function would be the created object of the class, and you cannot use self anymore, but need to use this passed in parameter then.
The call to the function would not be this anymore:
Lua Code:
  1. local CreatedObjectOfZO_PlatformStyle = ZO_PlatformStyle:New(param1, param2, ...)
  2. PLATFORM_STYLE_MANAGER:Add(CreatedObjectOfZO_PlatformStyle)
But this:
Lua Code:
  1. local CreatedObjectOfZO_PlatformStyle = ZO_PlatformStyle:New(param1, param2, ...)
  2. PLATFORM_STYLE_MANAGER.Add(PLATFORM_STYLE_MANAGER, CreatedObjectOfZO_PlatformStyle)


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

Wheels 09/23/20 07:33 AM

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

Mipps 09/23/20 08:27 AM

many spelling problems in my example due to hastly thrown together "forum exapmple":

Quote:

Originally Posted by Wheels (Post 42307)
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'.


Quote:

Originally Posted by sirinsidiator (Post 42305)
"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.


should be:

function addonName.OnAddOnLoaded()
...

Mipps 09/23/20 08:36 AM

interestingly I got it to work with a metatable which name is not equal to the addon name...?

sirinsidiator 09/23/20 09:47 AM

Quote:

Originally Posted by Mipps (Post 42309)
interestingly I got it to work with a metatable which name is not equal to the addon name...?

You can use any variable name you want, but since all global variables are shared between ingame code and other addons you usually want to use something that incorporates your addon name to avoid conflicts.


All times are GMT -6. The time now is 04:00 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI