WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Passing variables from one lua file to the next (https://www.wowinterface.com/forums/showthread.php?t=52483)

Yukyuk 07-09-15 02:09 PM

Passing variables from one lua file to the next
 
Working on a new addon.
This time I try to make use the Ace libraries.

So I make use of "AceDB-3.0" to build my database.
Its defined in a Lua file called Main and it creates a database called GildamDB.

This works fine. The variables are stored and retrieved just fine.

But now I have another Lua file Called UI.
Now I want to make use of of a few variables that are inside my database.
Don't seem to get that working.

Did google searches and used to code from Ackis Recipe List to see how it is done there.
But so far no luck.

Could someone give me a pointer to the right direction?
Don't know if code is needed, but when asked I will provide :p

Choonstertwo 07-09-15 02:15 PM

Each Lua file in your AddOn is called with two arguments: the AddOn's folder name and a table unique to the AddOn (often called namespace or ns). You can use the namespace table to store your AddOn's data, it's not accessible to any other AddOn (unless you make it so).

You can retrieve these values from the vararg in the main body of a Lua file. As an example:

File1.lua
lua Code:
  1. local addonName, ns = ...
  2.  
  3. ns.foo = "bar"

File2.lua
lua Code:
  1. local addonName, ns = ...
  2.  
  3. print(ns.foo)

Assuming File1.lua is loaded before File2.lua, the text bar will be printed in the chat frame.

Torhal 07-09-15 03:39 PM

In Ackis Recipe List, I actually use:

Code:

local FOLDER_NAME, private = ...

Yukyuk 07-10-15 10:45 AM

Thank you both for your answers.

Allready found out about the addonName, ns = ... part (allthough didn't know it meant namespace).
And impleneted it the same way as Ackis Recipe List does, even with the same names. :)

Error I made was using the variable directly in the UI.lua file, outside a function.
Once I put it inside a function in the UI.lua (and call that function from the Main.lua) it works as intended.

Guess it has to do something with scanning the lua files before they are acutually executed?

Anyway another small step made on the journey that is called lua programming. :o


All times are GMT -6. The time now is 08:54 PM.

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