lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Jul 08, 2002 at 04:35 +0200, Benoit Germain wrote:
> Hello,
> 
> my .02EUR, just to be sure :-)
> 
> I might want man_talk() to block until a certain condition is met (say, the
> sample is played). Do I have to
> man_talk("some words")
> until man_done_talking() do
> 	yield()
> end
> 
> or is there (will there be) the possibility for C extensions to yield by
> themselves, and write:
> man_talk("some words")
> and internally have the C implementation man_talk(L) call the sound
> subsystem, and yield until the subsystem says the resource is played ? Of
> course, the lua state could not resume the C extension at the exact point
> where it yielded, but the function could check whether it was called
> normally or because of a yield() being resumed ?

You can do this in Lua with a small wrapper:

function man_talk(sample_name)
	local sound_channel = play_sound(sample_name)
	while is_playing(sound_channel) do
		yield()
	end
end

Similar in spirit to what you have below, but I think more
straightforward...

> Oh well, thinking about it, I suppose one can always do
> 
> function Yielder(cfunction)
> 	local f = function (...)
> 		local yielded = 0
> 		until %cfunction(yielded,unpak(arg)) do
> 			yield()
> 			yielded=1
> 		end
> 	end
> 	return coroutine.create(f)
> end
> 
> man_talk = Yielder(man_talk)
> 
> man_talk("some_words")

-- 
Thatcher Ulrich
http://tulrich.com