# LUA - multiple files and functions question

This topic is 4614 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, My LUA stuff is coming along quite nicely although I just ran into a brick wall I can't break down, so I would like some help! I have multiple lua files, one for my main menu, called mainmenu.lua. in the mainmenu.lua file I create a button for my GUI which specifies a callback to be called when the button is pressed. My GUIButton class is a seperate LUA script file and is a class derived from my C++ class, CBaseGUIObject. The problem is, when I pass in the callback, the function is defined in mainmenu.lua and so my call to loadstring crashes my program. I was wondering if there is any way to get access to functions in other files, and I can't use require() because, first of all I don't want to put a bunch of require calls in my GUI object files(each one will be a seperate lua file), and second I tried it just for the heck of it and it didn't work anyway. here is some code to show you how it's set up: mainmenu.lua
-- Load in the asset definitions
require("../scripts/assetdefs.lua");
require("../scripts/gui/guibutton.lua");

-- Callback for the button
function buttcallback()
end

-- A button!
button1 = GUIButton(.4, .55, .2, .1, .5, buttonBack, "Button1", "buttcallback");

and, my code that calls the callback
		if (event.x > x and event.x < (x + width) and event.y < y and event.y > (y - height)) then
-- Load the function for the callback and execute it
f();
end

Thanks for any help!

##### Share on other sites
Lua doesn't have any conception of "different source files", or even of a "source file", for that matter. A "script", by which I mean a file containing lua code, is treated as a function (in effect, "function(...)" is prepended to the text, and "end" is appended to the text). The "require" function loads a file (as a function) and executes it. If that file contained the statement "print('foo')", it would print 'foo' at that point. If that file contained the statement "function bar(x) return x-1 end", it would set the global variable "bar" to refer to a function which returns its argument minus one. Since all of the "scripts" are executed in the same global namespaces, functions which they contain will be able to refer to each other.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 14
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632152
• Total Posts
3004470

×