keharriso / love-nuklear

Lightweight immediate mode GUI for LÖVE games
MIT License
350 stars 32 forks source link

UI functions don't seem to work from a coroutine #59

Closed JohnEmhoff closed 1 year ago

JohnEmhoff commented 1 year ago

Hello! I get an odd error when I call nuklear functions from a coroutine:

Error

main.lua:25: bad argument #1 to 'resume' (Nuklear context expected, got thread)

Traceback

[love "callbacks.lua"]:228: in function 'handler'
[C]: in function 'resume'
main.lua:25: in function 'draw'
[love "callbacks.lua"]:168: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'

Here's a minimal reproduction:

local nuklear = require('nuklear')
local nuk_ui
local co

function ui_one_frame()
   nuk_ui:frameBegin()
   if nuk_ui:windowBegin('Window', 100, 100, 600, 160,
          'border', 'movable', 'title') then
      nuk_ui:layoutRow('dynamic', 30, 1)
      nuk_ui:label('Hello, world!')
   end
   nuk_ui:windowEnd()
   nuk_ui:frameEnd()
end

function ui_stuff()
   while true do
      ui_one_frame()
      coroutine.yield()
   end
end

function love.draw()
   love.graphics.clear(0, 0, 0, 1)
   coroutine.resume(co)
   --ui_one_frame()
   nuk_ui:draw()
end

function love.load(_aa, _b)
   nuk_ui = nuklear.newUI()
   co = coroutine.create(ui_stuff)
end

As far as I can see it's failing on the call to windowBegin(). If I replace the coroutine.resume call with ui_one_frame (i.e. call the nuklear functions from the main coroutine) things work fine. Is this expected behavior?

keharriso commented 1 year ago

This is addressed in 829dc15. Let me know if this solves your issue.

New binaries can be found on the Releases page.

JohnEmhoff commented 1 year ago

Works great, thanks! Thanks for the quick fix (which looked kind of annoying as well).

keharriso commented 1 year ago

Ha, yeah it was a little annoying! 😄

Glad it's fixed.