Riftui

Riftui (https://www.riftui.com/forums/index.php)
-   Lua Help (https://www.riftui.com/forums/forumdisplay.php?f=16)
-   -   Call a function in another codefile (https://www.riftui.com/forums/showthread.php?t=135)

Dziriki 10-07-11 10:41 AM

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.

Aieny 10-07-11 01:44 PM

Place objectfactory.lua first in the RunAtStartup list. Thus:
Code:

RunAtStartup = {
    "objectfactory.lua",
    "addon.lua",
}


bmcquist 10-07-11 01:45 PM

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.

Aieny 10-07-11 01:51 PM

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.

Dziriki 10-08-11 02:12 AM

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


Aieny 10-08-11 05:18 AM

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).

Neglected 10-09-11 07:52 AM

Quote:

Originally Posted by Aieny (Post 476)
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


All times are GMT -6. The time now is 05:43 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2022 MMOUI