Page 1 of 1

love.graphics not working when called from a callback function

Posted: Sat Sep 03, 2022 3:39 pm
by Nawias
I'm starting development on a small game to finally try love2d for real (apart from exploring what can be done with LovePotion). I'm using Middleclass because I just can't think non-oop anymore, especially trying to learn new language (lua) and I want to structure my code to make it maintainable and extensible. I made a simple state machine/manager for global state, which works like this (regarding drawing):

Code: Select all

--- main.lua
function love.load()
  StateMachine:addState(1, introState)
  StateMachine:addState(2, mainState)
  StateMachine:setState(1)
end

function love.draw(screen)  ---lovepotion-specific parameter
  StateMachine:draw(screen)
end

Code: Select all

--- StateMachine.lua
function StateMachineClass:draw(screen)
  if (_currentState == nil) then return end
  _states[_currentState]:draw(screen)
end

Code: Select all

--- State.lua
function State:initialize(draw)
  self.draw = draw;
end

Code: Select all

--- introState.lua - 
local function draw(screen)
  love.graphics.print("intro")
  print("intro")
end

return State:new(
  draw
)
The result is weird. The code in the callback functions executes no problem, but nothing is drawn. I put some debug print() statements to check if it's doing anything at all, and it is - just not the love.graphics part.
Image

Any clue why this might be happening? If I put the same statements into main.lua love.draw() function, or even like my library dslayout - outside of main.lua, it works without a hitch.

Re: love.graphics not working when called from a callback function

Posted: Sat Sep 03, 2022 4:08 pm
by pgimeno
Does it work in normal Löve rather than lovepotion?

If not, can you provide a full runnable example?

Re: love.graphics not working when called from a callback function

Posted: Sat Sep 03, 2022 4:22 pm
by Nawias
I'm not testing it on lovepotion yet, I'm developing it first on regular Löve. As for the code, here's the repo: https://github.com/Nawias/TasmanQuest

Re: love.graphics not working when called from a callback function

Posted: Sat Sep 03, 2022 4:33 pm
by pgimeno
Ah, thanks for the code. Something's broken in your abstraction; when I modify love.draw() like this:

Code: Select all

function love.draw(screen)
  print("draw start")
  StateMachine:draw(screen)
  print("draw end")
end
this is what is printed:

Code: Select all

intro
draw start
draw end
intro
draw start
draw end
...
This means that the draw function in introState.lua gets called, but not by StateMachine:draw().

EDIT: Ah, I see. You swapped your update and draw parameters in State:initialize:
State.lua:

Code: Select all

function State:initialize(load, unload, draw, update, ...)
introState.lua:

Code: Select all

return State:new(
  load,
  unload,
  update,
  draw,
  ...
)

Re: love.graphics not working when called from a callback function

Posted: Sat Sep 03, 2022 5:58 pm
by Nawias
Really... How could I have been so dumb? :death: I should be getting more sleep...
Sorry for an unneccessary thread! And thank you for your help!
I'll be making the repo private for the time being, I don't want to opensource the game, at least not yet.