Thread: Basic Questions
View Single Post
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