[SOLVED] require fails when used in thread

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
User avatar
Fang
Prole
Posts: 9
Joined: Sat Apr 13, 2013 4:31 pm
Contact:

[SOLVED] require fails when used in thread

Post 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!
Attachments
threaderror.love
(450 Bytes) Downloaded 131 times
Last edited by Fang on Tue Apr 21, 2015 7:35 pm, edited 1 time in total.
~palfun-foslup
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: require fails when used in thread

Post 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.
User avatar
Fang
Prole
Posts: 9
Joined: Sat Apr 13, 2013 4:31 pm
Contact:

Re: require fails when used in thread

Post 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!
~palfun-foslup
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests