Reply
Thread Tools Display Modes
Unread 10-07-11, 10:41 AM   #1
Dziriki
Zombie
Join Date: Oct 2011
Posts: 2
Call a function in another codefile

Hello, I am relatively new to programming in LUA. I cannot figure out how to call a function located in a secondary .lua file inside the addon folder.

I have 3 files in the folder.
addon.lua
objectfactory.lua
RiftAddon.toc

I add both lua files to RiftAddon "RunAtStartup" section.
But every time I try to call a function in objectfactory I get this error:

Error: attempt to index global "function" (a nil value) In addon/addon.lua

Any advice would be greatly appreciated.

Last edited by Dziriki : 10-07-11 at 10:41 AM. Reason: Typos.
Dziriki is offline   Reply With Quote
Unread 10-07-11, 01:44 PM   #2
Aieny
Veiled Ripper
 
Aieny's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2011
Posts: 40
Place objectfactory.lua first in the RunAtStartup list. Thus:
Code:
RunAtStartup = {
    "objectfactory.lua",
    "addon.lua",
}
__________________
Lua Code:
  1. local function signature()
  2.     print("Aieny Celenovia")
  3.     print("www.riftcrafter.com")
  4. end
Aieny is offline   Reply With Quote
Unread 10-07-11, 01:45 PM   #3
bmcquist
Zombie
Join Date: Aug 2011
Posts: 1
From the error it looks like it's trying to call a function from addon.lua and not objectfactory.lua. You might want to check to make sure you dont have functions of the same names in the two files. Also, make sure your functions are public and not private.
bmcquist is offline   Reply With Quote
Unread 10-07-11, 01:51 PM   #4
Aieny
Veiled Ripper
 
Aieny's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2011
Posts: 40
The issue is that, if the codefile for the function is not loaded first, then the function won't be defined when it's called. The main addon file should always be the LAST file in the list.

Of course, I'm assuming the order of the files in the TOC, but that wasn't explicitly stated.
__________________
Lua Code:
  1. local function signature()
  2.     print("Aieny Celenovia")
  3.     print("www.riftcrafter.com")
  4. end
Aieny is offline   Reply With Quote
Unread 10-08-11, 02:12 AM   #5
Dziriki
Zombie
Join Date: Oct 2011
Posts: 2
hmmm... I still can't make it work.

I have placed the lua files in the correct order in RiftAddon.toc

Code:
RunOnStartup = {
	"objectfactory.lua",
	"addon.lua",
}
then, inside the objectfactory.lua I have the following function:

Code:
function create_object(objectname)
    creating objects... creating obkects...
end
I am not using the "local" tag on the function, so it should be public, right?
then inside addon.lua I call this function as:

Code:
create_object(testobject)
and I still get the same error:

Code:
attempt to call global 'create_object' (a nil value)
 In addon/addon.lua

Last edited by Dziriki : 10-08-11 at 02:34 AM.
Dziriki is offline   Reply With Quote
Unread 10-08-11, 05:18 AM   #6
Aieny
Veiled Ripper
 
Aieny's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2011
Posts: 40
If you'd like, PM me with the full code and I'll take a look and see what's going on. What you've posted should work; the only thing I can think of is that there is an error in the objectfactory.lua file that is stopping it from completely loading (thus the function is undefined).
__________________
Lua Code:
  1. local function signature()
  2.     print("Aieny Celenovia")
  3.     print("www.riftcrafter.com")
  4. end
Aieny is offline   Reply With Quote
Unread 10-09-11, 07:52 AM   #7
Neglected
Bomani Harbinger
AddOn Author - Click to view addons
Join Date: Jun 2011
Posts: 5
Quote:
Originally Posted by Aieny View Post
If you'd like, PM me with the full code and I'll take a look and see what's going on. What you've posted should work; the only thing I can think of is that there is an error in the objectfactory.lua file that is stopping it from completely loading (thus the function is undefined).
This - I really hope the code you posted for create_object is pseudo code.. :P
__________________
Quote:
<+Nobgul> Only a real man can appreciate pink
Neglected is offline   Reply With Quote
Reply

Go BackRiftui » Developer Discussions » Lua Help » Call a function in another codefile

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