Running Lua Script in other Main

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
Willowton
Prole
Posts: 5
Joined: Mon Dec 17, 2012 4:05 pm

Running Lua Script in other Main

Post by Willowton »

Whats the easiest way to run a function from a separate .lua file in a main method? I've been looking around for ages, and still haven't figured it out.

I'm trying to load a file called "moveOtto.lua", and it contains only one function, called "moveOtto()". So I'm doing the following;

Code: Select all

love.load()
f = loadfile(moveOtto.lua)
end

love.update(dt)
f()
moveOtto()
end
However, I'm getting the error "attempt to call global 'f', a nil value. Any advice?

On a related note, should I include "moveOtto.lua" when I compress to a .love file? I've tried both, and neither are working, but I figured its worth asking.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Running Lua Script in other Main

Post by Nixola »

moveOtto.lua should be inside single or double quotes, otherwise it will think that's a variable; I wonder why LÖVE didn't error about indexing a nil value or something...

P.S: yes, you have to include the file
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Running Lua Script in other Main

Post by Boolsheet »

There are several ways to load and execute code in Lua and LÖVE. As you discovered there is loadfile, but this function doesn't know about the LÖVE paths and will try to load files from the working directory if you give it a relative path. It also can't decode files from the zip archive.

The LÖVE equivalent of loadfile is love.filesystem.load. This function will properly search in the game and save directories.

Many lovers prefer the require function to load additional code. It is part of Lua's module loader and it does more than just load and execute. It takes a module name as a string which will be passed to searchers that look for the module. LÖVE adds such searchers so the game and save directory are covered. Once the module is found, it gets loaded, executed and require saves the first return value (or the boolean value true if nothing was returned) in a table. The next time require is called with the same module name it just returns this saved value. This is useful if you use the module in many files, but only want to load it once. (Or you can just assign your stuff to some global variable and access that everywhere. People don't like the 'abuse' of the global environment much, but I think they'll understand if you're still learning. ;) )

In case of Lua-file modules, the module name is just the filename without the extension. If you have it in a sub-directory, use the period as a separator instead of slashes. Lua will replace them with the appropriate character.

An example: Let's say you have this Lua file in "source/functions.lua".

Code: Select all

local myFunctions = {}
myFunctions.sayHello = function()
    print("Hello")
end
myFunctions.add = function(a, b)
    return a + b
end
return myFunctions
You can load and use it like this in the other files.

Code: Select all

funcs = require("source.functions")
funcs.sayHello()
print(funcs.add(5, 7))
Shallow indentations.
Willowton
Prole
Posts: 5
Joined: Mon Dec 17, 2012 4:05 pm

Re: Running Lua Script in other Main

Post by Willowton »

Thank you so much for the help.

The script I'm loading dictates the motion of my character, so it's being called just about every frame. In that case, what do I want to use? require, or love.filesystem.load?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Running Lua Script in other Main

Post by Robin »

Use require on it once, then call the function every frame.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 6 guests