Jump to content
  • Advertisement
Sign in to follow this  

LUA - multiple files and functions question

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

If you intended to correct an error in the post then please contact us.

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

-- Callback for the button
function buttcallback()

-- 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 = loadstring(self.callback.."()");
Thanks for any help!

Share this post

Link to post
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!