Page 1 of 1

attempt to yield across metamethod/C-call error

Posted: Sun Feb 15, 2009 10:37 am
by rbisso
Hey all,

(Crazy that Valentine's Day (more or less) is my first post on the Love forums, eh?)

In order to simulate spawning a function in another thread, I'm using Lua coroutines. I'm creating the coroutine as per normal, yielding within a loop that's in the coroutine-ized function, and then resuming at the bottom of my update loop. When it goes to resume, it gives me the error:
attempt to yield across metamethod/C-call boundary
Anyone ever get this? How did you deal with it? Thanks so much for your help :D

Code: Select all

function update( dt )
...
	resume_coroutines()
end

...

function rotate_enemy(which_enemy, num_angle)
	local cur_heading = which_enemy.heading
	local goal_heading = which_enemy.heading + num_angle
	local angle_increment = num_angle / which_enemy.rate_rotation

	
	while( cur_heading ~= goal_heading )do
		cur_heading = cur_heading + angle_increment
		which_enemy.heading = cur_heading
		coroutine.yield()
	end
end
co_rotate_enemy = coroutine.create(rotate_enemy)

...

function resume_coroutines()  -- register my coroutines here...
	if(coroutine.status(co_rotate_enemy) ~= 'dead' )then
		coroutine.resume(co_rotate_enemy)
	end
end


Re: attempt to yield across metamethod/C-call error

Posted: Sun Feb 15, 2009 9:24 pm
by rude
I've never used coroutines, so frankly, I have (almost) no idea.

HOWEVER: The problem might be related to the fact that the main loop is in C. The global functions update and draw are called each frame. I don't have a solution to your problem, but maybe this will give someone else an idea. (FYI: main loop will be in Lua next version).

Re: attempt to yield across metamethod/C-call error

Posted: Mon Feb 16, 2009 1:21 pm
by Sslaxx
Does this mean it'll then become the coder's responsibility to create/update the main loop?

Re: attempt to yield across metamethod/C-call error

Posted: Mon Feb 16, 2009 1:43 pm
by bartbes
As written somewhere else, there is a default which will do exactly the same as the C code is doing now, however you can create your own main loop.

Re: attempt to yield across metamethod/C-call error

Posted: Mon Feb 16, 2009 2:32 pm
by rude
@Sslaxx, bartbes, and anybody else who cares:

Code: Select all

-- Love.init is the one and only function to be called from C.
-- This function is compiled as Lua-code into the executable, and 
-- can't be changed, because it's the function that loads your code (main.lua).
function love.init()
   -- loads modules, sets up write directory, and other secret stuff.
   -- loads the main.lua file from your game. 
   -- calls love.run()
end

-- This function is also compiled into the executable, but it is called AFTER main.lua
-- is loaded, so you can overwrite it if you wish.
function love.run()
   -- load()
   while running do
      -- get events, call callbacks
      -- update()
      -- draw()
   end
end

-- You CAN completely ignore the existence of love.run, and use these like normal
-- (yes, they will be moved to the love-table).
function love.load() ... end
function love.update(dt) ... end
function love.draw() ... end

Re: attempt to yield across metamethod/C-call error

Posted: Mon Feb 16, 2009 3:02 pm
by Sslaxx
Always struck me as odd as to why update/init/load weren't in the love table.

Re: attempt to yield across metamethod/C-call error

Posted: Mon Feb 16, 2009 3:20 pm
by bartbes
Well, it always made sense to me in the way that those callbacks aren't actually part of the API/SDK/whatever we need to call it provided by love. They are required, but they're not part of.

Re: attempt to yield across metamethod/C-call error

Posted: Mon Feb 16, 2009 9:01 pm
by Star Crunch
I don't have a solution to your problem, but maybe this will give someone else an idea. (FYI: main loop will be in Lua next version).
Unless you plan to target something REALLY exotic, your next version could be built on http://luajit.org/coco.html and this issue should never show up.