Thread Tools Display Modes
05-15-16, 02:36 PM   #1
Platine
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Dec 2010
Posts: 72
Can't load other lua file

Hello,
I have a simple structure:

MyAddon.toc file:
Code:
## Interface: 60200
## Title: MyAddon
## Notes: Description of MyAddon
other_file.lua
MyAddon.lua
other_file.lua :
Code:
print ("Text from other_file.lua");
MyAddon.lua :
Code:
print ("Text from MyAddon.lua");
and finally I have received text on wow console:

Code:
Text from MyAddon.lua
Where is text from the first file (other_file.lua).
It isn't execute?
  Reply With Quote
05-15-16, 03:17 PM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
You need to use the vaarg to do this. That is shorthand for "variable argument" and looks like this in code:
Lua Code:
  1. -- other_file.lua
  2. local AddOn, pt = ... -- string, cross-file table, ... is the vaarg
  3. print(AddOn)
  4. -- >> MyAddon (from the ToC's Title line)
  5.  
  6. pt.otherText = "Text from other_file.lua"
  7. print(pt.MyAddonText)
  8. -- >> "Text from MyAddon.lua"
  9.  
  10. -- MyAddon.lua
  11. local AddOn, pt = ...
  12. pt.MyAddonText = "Text from MyAddon.lua"
  13. print(pt.otherText)
  14. -- >> "Text from other_file.lua"

You can also do cross-file function calls using the table.
Lua Code:
  1. -- file #1
  2. local titleName, myTable = ... -- remember both are variables, and can be called whatever you wish
  3.  
  4. function myTable:PrintText(textString)
  5.     print(textString)
  6. end
  7.  
  8. -- file #2
  9. local Bob, Nancy = ... -- lack of consistancy, but doesn't matter what we name them
  10.  
  11. local myString = "We should call the vaargs the same in all files for readability"
  12.  
  13. Nancy:PrintText(myString)
  14. -- >> We should call the vaargs the same in all files for readability

Last edited by myrroddin : 05-15-16 at 03:21 PM. Reason: grammar
  Reply With Quote
05-15-16, 03:36 PM   #3
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Also, remember that the order of your Lua files in your Toc matters. For example, in one of my AddOns, I have a core or main file, and the options you'd see in the Interface .. AddOns list comes from a second file. You don't need to do this, but it was my way of making things easier to debug with a large AddOn.

Lua Code:
  1. -- ToC
  2. ## all other stuff as expected
  3. main.lua
  4. options.lua
  5.  
  6. -- main.lua
  7. -- register events, done ADDON_LOADED, etc
  8. local addon_name, pt = ...
  9. function pt:ADDON_LOADED(...)
  10.     if ... ~= addon_name then
  11.         return -- not my AddOn
  12.     end
  13.  
  14.    pt.options = self:MyOptions() -- see options.lua
  15. end
  16.  
  17. -- options.lua
  18. local addon_name, pt = ...
  19. function pt:MyOptions()
  20.     local options = {
  21.         -- checkboxes, sliders, inputs, etc
  22.     }
  23.     return options -- populate pt.options
  24. end
  Reply With Quote
05-15-16, 05:31 PM   #4
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
If you successfully included all of your code here, then it's probably just a matter of restarting the client, which you need to do whenever new file names are added.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
05-15-16, 07:05 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Originally Posted by Lombra View Post
If you successfully included all of your code here, then it's probably just a matter of restarting the client, which you need to do whenever new file names are added.
Expanding on this, ToC file changes apply after you back out to the login screen. Adding files to the addon folders only get picked up after a full client restart.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
05-15-16, 09:45 PM   #6
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
I don't think he was wanting to print out something from the other Lua file, but something from THAT Lua file.

Sometimes print statements in the main chunk of a file don't print if the code is executed before the chat frame loads.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
05-16-16, 03:33 AM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Originally Posted by Seerah View Post
Sometimes print statements in the main chunk of a file don't print if the code is executed before the chat frame loads.
In all honesty, I've called print() from the main chunk many times in testing and haven't had it fail once.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
05-16-16, 06:18 AM   #8
Platine
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Dec 2010
Posts: 72
It is interesting.
I have made the structure of MyAddon for new, and all work properly:

MyAddon.toc
Code:
## Interface: 60200
## Title: MyAddon
## Author: Platine
## Version: v60200-1.0

File1.lua
MyAddon.lua
File2.lua
File1.lua
Code:
print ("Text from File1.lua");
MyAddon.lua
Code:
print ("Text from MyAddon.lua");
File2.lua
Code:
print ("Text from File2.lua");
Chat frame on Wow
Code:
Text from File1.lua
Text from MyAddon.lua
Text from File2.lua

It was need a full restart of Client.

Last edited by Platine : 05-16-16 at 06:30 AM.
  Reply With Quote
05-16-16, 06:35 PM   #9
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Another culprit is that some chat frame addons will clear the chat window (when changing the max number of lines displayed, for example).
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Can't load other lua file


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