[SOLVED] Yo dawg I heard you like coroutines

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
DataBrain
Prole
Posts: 5
Joined: Fri Jul 03, 2015 5:37 pm

[SOLVED] Yo dawg I heard you like coroutines

Post by DataBrain »

Hey, so the current system I'm using for my game runs basically everything on nested coroutines to avoid stack overflow and such.

This is code that I put in main.lua, which is called by other scripts which are being required by this script:

Code: Select all

_G.GlobalData = {}
local CurrentLevel
local QueueLevel -- This will avoid stack overflow from recursion
local QueueLevelArgs = {}
function GlobalData.SetLevel(name, ...)
	QueueLevelArgs = {...}
	-- (Asset unloading code snipped)
	QueueLevel = name
	-- (Asset loading code snipped)
end

function love.update(deltaTime)
	if QueueLevel ~= nil then
		local temp = QueueLevel
		QueueLevel = nil
		-- (Some other loading code here snipped)
		if CurrentLevel then
			coroutine.yield(CurrentLevel)
		end
		if temp == "EXIT" then return end
		CurrentLevel = coroutine.create(function() require(temp)(unpack(QueueLevelArgs)) end)
		coroutine.resume(CurrentLevel)
	end
end
When I run something like

Code: Select all

GlobalData.SetLEvel("MainMenu")
it will load up the MainMenu.lua script.

However, inside of THAT script, I'm also repeating the coroutine system between each menu screen. And when I try to yield the current menu screen (To end all code running there, which is why I'm using coroutines in the first place), i get an "attempt to yield across C-call boundary" error.

Is there an overall better way to go about this and avoid stack overflow? My current system (If you took out the error) loads each part separately and efficiently, ending all code that isn't needed. But is there a more efficient way to organize my threads than coroutines?
Last edited by DataBrain on Sat Jul 04, 2015 1:58 pm, edited 1 time in total.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Yo dawg I heard you like coroutines

Post by airstruck »

Can you post an example of how your code looked when it was overflowing the call stack? There are probably better options than coroutines for avoiding that. Might be best to restructure things so you're not using so much recursion, but you could also probably do something simple like taking advantage of tail call elimination.
DataBrain
Prole
Posts: 5
Joined: Fri Jul 03, 2015 5:37 pm

Re: Yo dawg I heard you like coroutines

Post by DataBrain »

time thief wrote:Can you post an example of how your code looked when it was overflowing the call stack? There are probably better options than coroutines for avoiding that. Might be best to restructure things so you're not using so much recursion, but you could also probably do something simple like taking advantage of tail call elimination.
Alright, I've been searching everywhere, and I've found this.

I guess I was worried about the wrong problem, because you can call functions back and forth indefinitely. The "Attempt to yield across C-call boundary" error is a different problem for another thread.

I kind of solved my own problem.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest