"stateswitcher" and Images

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
BurningScript
Prole
Posts: 2
Joined: Wed Oct 02, 2019 6:58 pm

"stateswitcher" and Images

Post by BurningScript »

Hello, I'm new to Löve2D and LUA in general. My goal is to create a simple menu with a background, so I installed "stateswitcher" (https://love2d.org/wiki/Stateswitcher).

In my "menu.lua" I tried to load this image and draw it, but it won't work:

Code: Select all

Error

menu.lua:16: bad argument #1 to 'draw' (Drawable expected, got nil)


Traceback

[C]: in function 'draw'
menu.lua:16: in function 'draw'
[C]: in function 'xpcall'

My main.lua:

Code: Select all

state=require("lib.state.stateswitcher")


function love.load()

    state.switch("menu")
end


My "menu.lua":

Code: Select all

state=require("lib.state.stateswitcher")
anim = require('lib.anim8.anim8')


function love.load()
    bgimg = love.graphics.newImage("img/bg.png")
end


function love.update(dt)

end

function love.draw()

    love.graphics.draw(bgimg)

end

If I use the menu.lua code in my "main.lua" without stateswitcher it works like a charm.

What am I doing wrong?

Thanks!
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: "stateswitcher" and Images

Post by raidho36 »

I'm guessing that love.load in your menu.lua doesn't run, since it already ran in main.lua and it's only called exactly once.

You can remedy this problem by modifying the state switcher code a little; after the line where it requires target module, you would manually call love.load function.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: "stateswitcher" and Images

Post by pgimeno »

BurningScript wrote: Wed Oct 02, 2019 7:04 pm What am I doing wrong?
Hello, welcome to the forums. The problem is that this library is not very well designed; the error comes from the fact that it won't execute love.load events in the code it loads. It also has more problems: it won't clear old events when switching from one state to another, meaning that if you switch from a file that has a certain event, to a file that doesn't, that event will still be active.

My advice is to change to another library with a different design, or implement state switching yourself. If you opt for the pre-made library, one possible recommendation would be Hump. If you opt for the second one, I give some indications here: https://love2d.org/forums/viewtopic.php ... 26#p194226
BurningScript
Prole
Posts: 2
Joined: Wed Oct 02, 2019 6:58 pm

Re: "stateswitcher" and Images

Post by BurningScript »

pgimeno wrote: Wed Oct 02, 2019 10:30 pm
BurningScript wrote: Wed Oct 02, 2019 7:04 pm What am I doing wrong?
Hello, welcome to the forums. The problem is that this library is not very well designed; the error comes from the fact that it won't execute love.load events in the code it loads. It also has more problems: it won't clear old events when switching from one state to another, meaning that if you switch from a file that has a certain event, to a file that doesn't, that event will still be active.

My advice is to change to another library with a different design, or implement state switching yourself. If you opt for the pre-made library, one possible recommendation would be Hump. If you opt for the second one, I give some indications here: https://love2d.org/forums/viewtopic.php ... 26#p194226
Thank you very much! I already changed to HUMP and it works without any problem! :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 48 guests