[SOLVED] Yo dawg I heard you like coroutines
Posted: Sat Jul 04, 2015 1:04 am
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:
When I run something like 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?
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
Code: Select all
GlobalData.SetLEvel("MainMenu")
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?