love.graphics not working when called from a callback function

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Nawias
Prole
Posts: 9
Joined: Sun Apr 10, 2022 4:13 pm
Location: Poland

love.graphics not working when called from a callback function

Post 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.
User avatar
pgimeno
Party member
Posts: 3637
Joined: Sun Oct 18, 2015 2:58 pm

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

Post by pgimeno »

Does it work in normal Löve rather than lovepotion?

If not, can you provide a full runnable example?
User avatar
Nawias
Prole
Posts: 9
Joined: Sun Apr 10, 2022 4:13 pm
Location: Poland

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

Post 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
User avatar
pgimeno
Party member
Posts: 3637
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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,
  ...
)
User avatar
Nawias
Prole
Posts: 9
Joined: Sun Apr 10, 2022 4:13 pm
Location: Poland

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], t3hgreen and 6 guests