Quantcast
table behaviour - ESOUI
Thread Tools Display Modes
09/22/20, 07:15 PM   #1
Mipps
Join Date: Sep 2020
Posts: 10
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?
  Reply With Quote
09/22/20, 07:49 PM   #2
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 660
function.OnAddOnLoaded() is not a thing
  Reply With Quote
09/23/20, 02:24 AM   #3
Mipps
Join Date: Sep 2020
Posts: 10
Originally Posted by Rhyono View Post
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

Last edited by Mipps : 09/23/20 at 02:27 AM.
  Reply With Quote
09/23/20, 05:01 AM   #4
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,480
"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.
__________________
https://www.imperialtradingcompany.eu/ - My Addons - The Vault (Early updates and experimental projects) - My patreon - My blog
>siri.exe MyAddon
Does your addon work? [y/n] n
There is a typo in there.
  Reply With Quote
09/23/20, 05:05 AM   #5
Baertram
 
Baertram's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 3,597
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


.

. 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

Last edited by Baertram : 09/23/20 at 05:13 AM.
  Reply With Quote
09/23/20, 07:33 AM   #6
Wheels
AddOn Author - Click to view addons
Join Date: Feb 2017
Posts: 60
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'.
  Reply With Quote
09/23/20, 08:27 AM   #7
Mipps
Join Date: Sep 2020
Posts: 10
many spelling problems in my example due to hastly thrown together "forum exapmple":

Originally Posted by Wheels View Post
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'.

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

Last edited by Mipps : 09/23/20 at 08:32 AM.
  Reply With Quote
09/23/20, 08:36 AM   #8
Mipps
Join Date: Sep 2020
Posts: 10
interestingly I got it to work with a metatable which name is not equal to the addon name...?
  Reply With Quote
09/23/20, 09:47 AM   #9
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,480
Originally Posted by Mipps View Post
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.
__________________
https://www.imperialtradingcompany.eu/ - My Addons - The Vault (Early updates and experimental projects) - My patreon - My blog
>siri.exe MyAddon
Does your addon work? [y/n] n
There is a typo in there.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » table behaviour

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off