Unable to call function from other file

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
VladiMatt
Prole
Posts: 4
Joined: Sun Feb 17, 2013 9:19 pm

Unable to call function from other file

Post by VladiMatt »

Hi there, I'm new to using LOVE, but I've already got a fair bit of Lua experience for writing addons for the Half-Life 2 modification "Garry's Mod".
Now, GMod's Lua structure has a number of key differences from LOVE (including what's brought me here to the LOVE forums today), but overall it's pretty much the same thing.

The one thing that's completely stumped me is calling functions from separate files - in GMod it's just

Code: Select all

include("lua_file_here.lua")

--other code
CallFunctionFromSeparateFile()
--other code
--etc
Now, include() doesn't work in LOVE, but the other night I tried doing exactly that with loadfile() instead and it worked perfectly, I was able to call a function from that file to set defined in 'main.lua', but I don't have the original code I used. I've attached a .love file which does pretty much exactly the same thing I did the other night, but for some reason it gives me an error that says the function doesn't exist.

I know I could have each individual function I want to call from the file in question be it's own separate file, and call the function as a chunk, but I'd rather not have 1,000 lua files with just one function each, because most of the functions are quite small and are only called once or twice in the entire game.

Anybody have any idea what's going on, or did I do a really poor job explaining the situation?

Thanks in advance!
Attachments
testfunc.love
.love file created specifically to show what I'm trying to do; it's not the actual code I'm using for my game.
(464 Bytes) Downloaded 149 times
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Unable to call function from other file

Post by Taehl »

Yeah, "include" is one of the many changes Garry made to make Lua more C-like (*gags a little*). Ahem. The normal way to do it in regular Lua is using "require". Require has the advantage of only loading a file once, if needed.

So if you have a script in your game's folder called "helper.lua", you would use

Code: Select all

require("helper")     -- note the lack of ".lua"
And if you had a script called "Tcontent.lua" in the folder "MyGame/lib", you would use

Code: Select all

require("lib.Tcontent")     -- note the . instead of /
Note that when you load the files, their global variables (like those functions you want) will be added to the global namespace. Local variables will not be accessible (without using tricks). It's considered good form to make the scripts you're requiring return their function or namespace, like this:

Code: Select all

-- func.lua
local function add(a,b) return a+b end
return add

Code: Select all

-- main.lua
add = require"func.lua"
Also, welcome to Love! I, too, learned Lua for Gmod before finding my way here.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
VladiMatt
Prole
Posts: 4
Joined: Sun Feb 17, 2013 9:19 pm

Re: Unable to call function from other file

Post by VladiMatt »

Oh jeez, I should've known it was something simple like that. That may have actually been what I did the other day, but I think when I tried using require() earlier today, I kept ".lua" at the end of the name and it gave me an error, so I assumed that didn't work.

Thanks a billion! :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], Semrush [Bot] and 0 guests