Love2D Library Tips/Gotchas
Posted: Fri May 24, 2019 3:19 pm
These are just some things I encountered while working, problems or head scratchers that I dealt with as a beginner and couldn't
find an answer to on the forum. Posting here in case anyone does like I did and searches the forums.
- when using mousereleased/pressed event with HUMP Gamestate, the docs say the parameter order is (button, x, y)
but it's actually (x, y, button)
- on that note, if you're using HUMP Gamestate, you need to add 't' as the first argument to gamestatename.mousepressed / mousereleased as of love2d 11.2 i.e..
- Gamestate.registerEvents() only needs to be called *once* for the entire program, in love.load
If you call it again, elsewhere in the program, it can and will cause bugs
- When using flux, in your love.update event, make sure to call flux.update(dt) otherwise your tweens won't run
- I use the following code in my update event
- use love.graphics.captureScreenshot(os.time() .. "-screenshot.png") for saving screenshots, the printscreen button itself doesn't work
- If using both push and hump camera together, then in the draw event be sure Push:Start() comes first.
- colors are in fractions NOT in integers. So either do
or
- I encountered a bug where push translation of screen-to-world coordinates was causing a crash, so use HUMP's camera screen-to-world-coordinates transformation instead. i.e.
- Note, in Gamestate, main.lua is NOT a state. If you put stuff in main, main will continue to run even after switching states. For example, if you want your start screen (before your mainmenu) to display a logo or something, be sure to make that its own state, have main.lua be
mostly empty, and then have love.load call Gamestate.switch(somenewstate)
- If you're planning on loading data (such as level data) from the game directory itself, create a bat file. I'm not 100% on the details, but without fused, it'll load your game or app data from something like %appdata%/user/love/appname or something (at least on windows)
instead of the game folder itself. So use the following in a bat file until you're ready to turn it into a .love archive..
- for menubars in slab, it took a little reading of the docs and trying a few things (some things were a little unclear) but this is the menubar code I used..
Thats all for now, any other things that might trip up beginners I'll post here in this thread. Others are welcome to do the same.
find an answer to on the forum. Posting here in case anyone does like I did and searches the forums.
- when using mousereleased/pressed event with HUMP Gamestate, the docs say the parameter order is (button, x, y)
but it's actually (x, y, button)
- on that note, if you're using HUMP Gamestate, you need to add 't' as the first argument to gamestatename.mousepressed / mousereleased as of love2d 11.2 i.e..
Code: Select all
gamestatename.mousereleased = function(t, x, y, button)
If you call it again, elsewhere in the program, it can and will cause bugs
- When using flux, in your love.update event, make sure to call flux.update(dt) otherwise your tweens won't run
- I use the following code in my update event
Code: Select all
acc = acc + dt
if acc >= ticks then
acc = acc - ticks --assures we dont lose any ticks in case acc is above ticks, instead of setting it to 0
end
- If using both push and hump camera together, then in the draw event be sure Push:Start() comes first.
- colors are in fractions NOT in integers. So either do
Code: Select all
love.graphics.setColor(1.0, 1.0, 1.0, 1.0) -- 1.0 is an example value, could be anything in the 0-1 range
or
Code: Select all
love.graphics.setColor(someinteger/255, someInteger/255, someInteger/255)
Code: Select all
camera:worldCoords(x, y)
- Note, in Gamestate, main.lua is NOT a state. If you put stuff in main, main will continue to run even after switching states. For example, if you want your start screen (before your mainmenu) to display a logo or something, be sure to make that its own state, have main.lua be
mostly empty, and then have love.load call Gamestate.switch(somenewstate)
- If you're planning on loading data (such as level data) from the game directory itself, create a bat file. I'm not 100% on the details, but without fused, it'll load your game or app data from something like %appdata%/user/love/appname or something (at least on windows)
instead of the game folder itself. So use the following in a bat file until you're ready to turn it into a .love archive..
Code: Select all
start love.exe "nameofthegamesfolderinlove" --fused
Code: Select all
if slab.BeginMainMenuBar() then
if slab.BeginMenuBar("File") then
if slab.BeginMenu("File") then
selected = nil
cursor = nil
if slab.MenuItem("Debug") then
print("debug pressed!")
end
if slab.MenuItem("New World") then
print("PASS")
end
if slab.MenuItem("Export") then
print("PASS")
end
if slab.MenuItemChecked("Render Grid?", renderGrid) then
print("PASS")
renderGrid = not renderGrid
end
slab.EndMenu()
end
slab.EndMenu()
end
slab.EndMainMenuBar()
end
Thats all for now, any other things that might trip up beginners I'll post here in this thread. Others are welcome to do the same.