Thread: Basic Questions
View Single Post
01/01/18, 10:20 PM   #3
Dolgubon
 
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 409
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