Thread Tools Display Modes
01/01/18, 09:21 PM   #1
Ni Lucian
Join Date: Jan 2018
Posts: 7
Basic Questions

Below you will see my code. But, most of my questions are more broad stroke.

How are packages recognized in LUA and what are the references?
E.G:
Code:
 if i have an .lua document at   /addon/myAddon/main.lua  
and another document at /addon/myAddon/somethingDir/something.lua
how would I reference something.lua functions?
EVENT_MANAGER <-- kind of two questions with this one that came about.

1. LUA doesn't have classes what do I call documents that house my code?

2. does the ESO client call EVENT_MANGER method and I am overriding it? If not am I calling the RegisterForEvent method inside EVENT_MANGER "class"?







Test code below doesn't display anything when leader is changed. Don't need an answer to this as I am hoping my above questions will cover this, but this issue is what made me ask the above questions.

Code:
Nilucian = {}

Nilucian.name="Nilucian"


function initialize(eventCode,addon)
    if addon ~= Nilucian.name then return end
end



function Nilucian:runIt(eventCode,leaderName)
if addon ~= Nilucian.name then return end
	d("test")
	d("new leader:  "+leaderName) 
	end


EVENT_MANAGER:RegisterForEvent(Nilucian.name, EVENT_ADD_ON_LOADED, initialize)

EVENT_MANAGER:RegisterForEvent(Nilucian.name, EVENT_LEADER_UPDATE, Nilucian.runIt)
  Reply With Quote
01/01/18, 09:29 PM   #2
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 659
Your txt file determines this, with the files loaded in the order they are listed.

For example: a lot of us use libraries in a lib folder. So if you want to use lib\whatever\whatever.lua, you'd put in your txt file:

lib\whatever\whatever.lua
your_addon.lua

Then your addon can properly call the variables in that other file.
  Reply With Quote
01/01/18, 10:20 PM   #3
Dolgubon
 
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 408
Originally Posted by Ni Lucian View Post
Below you will see my code. But, most of my questions are more broad stroke.

How are packages recognized in LUA and what are the references?
E.G:
Code:
 if i have an .lua document at   /addon/myAddon/main.lua  
and another document at /addon/myAddon/somethingDir/something.lua
how would I reference something.lua functions?
EVENT_MANAGER <-- kind of two questions with this one that came about.

1. LUA doesn't have classes what do I call documents that house my code?

2. does the ESO client call EVENT_MANGER method and I am overriding it? If not am I calling the RegisterForEvent method inside EVENT_MANGER "class"?







Test code below doesn't display anything when leader is changed. Don't need an answer to this as I am hoping my above questions will cover this, but this issue is what made me ask the above questions.

Code:
Nilucian = {}

Nilucian.name="Nilucian"


function initialize(eventCode,addon)
    if addon ~= Nilucian.name then return end
end



function Nilucian:runIt(eventCode,leaderName)
if addon ~= Nilucian.name then return end
    d("test")
    d("new leader:  "+leaderName) 
    end


EVENT_MANAGER:RegisterForEvent(Nilucian.name, EVENT_ADD_ON_LOADED, initialize)

EVENT_MANAGER:RegisterForEvent(Nilucian.name, EVENT_LEADER_UPDATE, Nilucian.runIt)
What languages are you coming from? It might help to know what your preconceived ideas of languages are.


So first of all, with your code snippet in runit function, The variable add-on is nil. There's no need at all to have that check there. The check is in the addonacticated event because that gets called whenever any add-on is activated, but you only want your code to run once, when your add-on is activated. Also, when in doubt, just add some d() to see what's happening. You'd have at least noticed that the check is passing, and that it is what the error is.



Second, about packages. I suggest reading about scope in Lua, if you haven't already. Functions in another file can only be accessed if they are global, or in a global table. If you use local when you create it and do not later assign it to a global variable name you will be unable to use it. Its similar to private and public functions in OOP. There's no need to say import file or load package or whatever. It'll be loaded and available anyway.


Third, about 1. The other files are just that, and there isn't really a standard of what name to give the files.


About 2. I highly highly highly suggest against overwriting any event manager functions. Every add-on uses that and changes you make can mess up tons of stuff if you don't know really really well what you're doing. By which I mean make tons of addons and then think again and probably don't do it.


Thankfully, don't think you're trying to do that. Event_manager is a global table. It is created by ESO, and ESO adds the functions we use to it. A good analogy is in fact that you are calling the registerforevent method from the class, but remember it isn't exactly a class.
  Reply With Quote
01/02/18, 05:43 PM   #4
Ni Lucian
Join Date: Jan 2018
Posts: 7
That helps a lot thank you. So, to answer your question my background is Java HEAVY. I have experience with C#, MYSQL, and some top level browser stuff.

So, my only left over question then is.

Code:
EVENT_MANAGER:RegisterForEvent(Nilucian.name, EVENT_LEADER_UPDATE, Nilucian.runIt)
Is that a function I am referencing then? In that case you can make calls to functions outside of any function? Or to relate it to Java; you can execute code outside of a method or construct and just have code executed within the class itself? Does this mean I could theoretically use d() outside of a function and somehow it will be called? If I was going to make this question concise I would say: when/where is control (or a thread) passed to my files? Is there some main function?



Second, about packages. I suggest reading about scope in Lua, if you haven't already. Functions in another file can only be accessed if they are global, or in a global table. If you use local when you create it and do not later assign it to a global variable name you will be unable to use it. Its similar to private and public functions in OOP. There's no need to say import file or load package or whatever. It'll be loaded and available anyway.
So, I have read on scope and it doesn't seem to foreign, but how do I reference a global variable form another file? It seems like- if I am understanding you correctly- you only have to declare a global variable and it is then accessible by all files? In that case if you were re-assigning that variable in one file it would effect how all other files receive it? Essentially a public static variable in java only it can be of any type.



Your txt file determines this, with the files loaded in the order they are listed.

For example: a lot of us use libraries in a lib folder. So if you want to use lib\whatever\whatever.lua, you'd put in your txt file:

lib\whatever\whatever.lua
your_addon.lua

Then your addon can properly call the variables in that other file.
Does this mean I have to load my files in such an order that each file only references files after it in the compile or load order?
  Reply With Quote
01/02/18, 06:32 PM   #5
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 659
Originally Posted by Ni Lucian View Post
That helps a lot thank you. So, to answer your question my background is Java HEAVY. I have experience with C#, MYSQL, and some top level browser stuff.

So, my only left over question then is.

Code:
EVENT_MANAGER:RegisterForEvent(Nilucian.name, EVENT_LEADER_UPDATE, Nilucian.runIt)
Is that a function I am referencing then?
To make it easier, let's say you have a string named bacon with the value of "delicious" For whatever reason, you want to see if "ous" is in that string. So you can do bacon:find("ous"). Or, you could do string.find(bacon,"ous").

So the format is the object:allowedfunction(functionparams)

Originally Posted by Ni Lucian View Post
In that case you can make calls to functions outside of any function? Or to relate it to Java; you can execute code outside of a method or construct and just have code executed within the class itself? Does this mean I could theoretically use d() outside of a function and somehow it will be called?
As prior mentioned, it has to be global or you're using it within scope. d() while a good example is actually finnicky in that the chat manager needs to load before you use it. In general, if your addon has initialized, d is good to go. So a global function can be used anywhere, but if a function is defined using local, it is limited to that file (or addon, based on the method below). Much like javascript, the code is run on the page as it is loaded. So if you put a function and nothing ever calls it, it doesn't do anything. You put pie="awesome" at the top and you now have a global variable. Slap a local before it and your lua file (functions included) can access it, because it's local to your file.

Originally Posted by Ni Lucian View Post
you only have to declare a global variable and it is then accessible by all files?
Yes, ALL files. My addon included. Now if you have a multiple file addon and want to keep your variables from spilling out but also want to use a nice concise name, you can declare a global that is pretty unique:

MyAddonName = {}

Then make a local variable under it

local MAN = MyAddonName

Then if you don't want to declare every function as local and/or you want to build your variables inside there, you could do

function MAN.funcName() end

That function is now local.

MAN.defaultVars = {}

Local as well, etc.

To use this same umbrella of functions in another file of yours declare the local again

local MAN = MyAddonName

then you can call MAN.funcName() if you want or create another as well

function MAN.NewFunc() end

Or use it inside of the new one

function Man.NewFunc()
local stuff = MAN.funcName()
end

Originally Posted by Ni Lucian View Post
Does this mean I have to load my files in such an order that each file only references files after it in the compile or load order?
Yes. I'm not sure if anyone has any clever workarounds to this, but it was rather perplexing the time I defined a function that was accessing a function from a file that loads after it but I was calling it from a page that came after both, so you know they were both loaded.


I have experience in a handful of languages (those you mentioned as well) and one of the things that was a hiccup for me was returning. Most languages require a multi-return to be something (such as an array) to encapsulate it all.

return [true,false,true,true]

Lua will let you actually just do

return true,false,true,true

So when you call the function, you do it like this:

local hungry,thirsty,etc,etc2 = func()

But what if it has 4 returns and you only care about the 3rd?

local _,_,etc = func()

Notice that I can just ignore those after I have what I want.

Last edited by Rhyono : 01/02/18 at 06:43 PM.
  Reply With Quote
01/02/18, 06:52 PM   #6
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,566
It's hard to compare a compiled language like Java to a scripting language like Lua. It simply works differently. Since you don't seem to know Lua I suggest you start reading here and do a few tutorials.

In Lua you don't have to care about the concept like "inside or outside a method". What's more important is the concept of "closures". You can nest functions and then access variables outside of the inner function without having to pass the value to it (up to the global scope). Basically you are already in a file-local scope at the first line of a file and can put d() there (but it won't show anything since the chat is only initialized later).

In the beginning when the character is loaded all addons that are active will have their files loaded in the order they appear in the manifest.
Aside of this initial loading, you will always have to react to some type of event or user input - that's your entrance point for anything. Since you are single threaded, it will always call one callback after another and one line after another. You do not have to worry about anything happening in parallel.

Also datatypes do exist, but are not declared. You can just assign any type to any variable at any time or pass any value to any function (even if they do not use it). The only thing you should remember is that tables and userdata are pointers - assigning or passing them and changing something on the new variable will affect the original. All other datatypes are copied (although strings do use references internally to save memory).

You only have to care about the load order when you do stuff while files are being loaded (-> file scope). Once the EVENT_ADDON_LOADED has fired for your addon, you can be sure that all your files have loaded and just access the global variables you have created.
  Reply With Quote
01/02/18, 09:11 PM   #7
Ni Lucian
Join Date: Jan 2018
Posts: 7
Thank you very much. I appreciate all your help and knowledge. What brings such knowledgeable people to this corner of the internet out of curiosity?


(No derogatory or negative connotation is intended)
  Reply With Quote
01/02/18, 09:29 PM   #8
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 659
I think for most of us it's the result of feeling something is missing or something breaking and needing fixed, then we accidentally keep doing it. When I first started, I was a little wary of the lack of posts on here and worried it'd be impossible to get help. However, the few devs that do trawl the forum are all extremely capable of assisting.
  Reply With Quote
01/03/18, 11:12 AM   #9
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,912
Welcome to lua coding within ESO.
You can fetch some of the devs here too: https://gitter.im/esoui/esoui
It's easier to post code and ask questions in this chat.

But feel free to post your code and questions as forum threads so everyone can benefit from the answers.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Basic Questions

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