love.graphics not working when called from a callback function
Posted: Sat Sep 03, 2022 3:39 pm
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):
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.
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.
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
)
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.