Do LUA coroutines work on the camera? - Script Writing - CHDK Forum
supplierdeeply

Do LUA coroutines work on the camera?

  • 1 Replies
  • 169 Views
Do LUA coroutines work on the camera?
« on: 02 / December / 2019, 00:56:06 »
Advertisements
If I replace
Code: [Select]
play_sound(4) in my script with 
Code: [Select]
coroutine.resume(coroutine.create(play_sound),4) the camera doesn't beep, and there isn't any error. I'm new to lua, is there anything wrong with this coroutine usage?

*

Online reyalp

  • ******
  • 12201
Re: Do LUA coroutines work on the camera?
« Reply #1 on: 02 / December / 2019, 02:27:27 »
If I replace
Code: [Select]
play_sound(4) in my script with 
Code: [Select]
coroutine.resume(coroutine.create(play_sound),4) the camera doesn't beep, and there isn't any error. I'm new to lua, is there anything wrong with this coroutine usage?
I think they may be a bit broken*, but even if they weren't, they wouldn't do what you want here. Lua coroutines are a purely Lua construction, which isn't connected the camera (or other Lua host program) task scheduling. They can be used to implement things like producer/consumer patterns or cooperative multi-tasking within a script, but they won't let you play a sound asynchronously.

coroutine.create only accepts Lua functions, not C functions like play_sound, so your example should generate an error like
Code: [Select]
bad argument #1 to 'create' (Lua function expected)

* I'm pretty sure Lua coroutines would have issues with the way our C code yields back to the camera OS when your call things like sleep, shoot() or key press functions, and possibly also with the debug hook that yields periodically to avoid hogging the CPU. Yes, I said coroutines weren't connected to host task scheduling, but we abuse Lua yield in C code.
Don't forget what the H stands for.

 

Related Topics