Thread Tools Display Modes
07-09-15, 02:09 PM   #1
Yukyuk
A Chromatic Dragonspawn
 
Yukyuk's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2015
Posts: 179
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
__________________
Better to fail then never have tried at all.
  Reply With Quote
07-09-15, 02:15 PM   #2
Choonstertwo
A Chromatic Dragonspawn
 
Choonstertwo's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2011
Posts: 194
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.
  Reply With Quote
07-09-15, 03:39 PM   #3
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
In Ackis Recipe List, I actually use:

Code:
local FOLDER_NAME, private = ...
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
07-10-15, 10:45 AM   #4
Yukyuk
A Chromatic Dragonspawn
 
Yukyuk's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2015
Posts: 179
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.
__________________
Better to fail then never have tried at all.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Passing variables from one lua file to the next

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