Page 1 of 1

[SOLVED] require fails when used in thread

Posted: Tue Apr 21, 2015 7:16 pm
by Fang
Hey all. Got a threading-related issue here that's not race conditions!

Long story short, I need to include (require) a file in a thread. Whenever I try this, the thread throws an error saying it can't find the file, even though it is required in the exact same way in other files (that run in the main thread).

Below is a very bare-bones example you can use to recreate the problem. (This example is also attached as a .love file.)

/main.lua:

Code: Select all

require "something" -- This works fine.

function love.load()
	thread = love.thread.newThread("thread.lua")
	thread:start()
end

function love.threaderror(thread, err)
	error("Thread error!\n" .. err)
end
/thread.lua

Code: Select all

require "something" -- This throws a "Module 'something' not found" error.
/something.lua
Can be empty, as long as the file actually exists.

Is this known behavior? Am I overlooking something? Is there anything I can do to fix this?

Thanks for your help!

Re: require fails when used in thread

Posted: Tue Apr 21, 2015 7:26 pm
by bartbes
When a Thread is started, it only loads the love.thread module. Every other module has to be loaded with require.
Though it may not be obvious, this means love.filesystem isn't loaded either, which is necessary if you want to require files from the normal paths.

Re: require fails when used in thread

Posted: Tue Apr 21, 2015 7:34 pm
by Fang
Ah, that does the trick. I came across this on the wiki page on require, and was wondering if that was being left out as well, and how I'd re-include it.
LÖVE adds two loaders (before the Lua loaders) that search for modules in the game and save directory.
Adding the following line to the top of thread.lua fixed it:

Code: Select all

require "love.filesystem"
Thanks, bartbes!