Love2D Library Tips/Gotchas

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
yintercept
Citizen
Posts: 64
Joined: Mon Apr 02, 2018 3:31 pm

Love2D Library Tips/Gotchas

Post by yintercept »

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

Code: Select all

 gamestatename.mousereleased = function(t, x, y, button) 
- 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

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   
- 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

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) 
- 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.

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

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.
Back in the saddle again.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Love2D Library Tips/Gotchas

Post by zorg »

Just an addendum, colors were integers previously, or rather, bytes if you want to use even more correct terminology, and they were in the range [0,255], but they have been changed to the [0,1] range since löve version 11.0
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Love2D Library Tips/Gotchas

Post by pgimeno »

yintercept wrote: Fri May 24, 2019 3:19 pm - 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)
What docs? The only docs I can find are https://hump.readthedocs.io/en/latest/gamestate.html which don't specify a parameter order, and there's even an example that uses mousereleased(x,y, mouse_btn).

In fact, the docs say this:
hump docs wrote:When using Gamestate.registerEvents(), all these callbacks will be called by the corresponding LÖVE callbacks and receive the same arguments (e.g. state:update(dt) will be called by love.update(dt)).
(it's implicit that they will also be in the same order).

yintercept wrote: Fri May 24, 2019 3:19 pm - 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) 
This is generic Lua behaviour, not hump gamestate specific behaviour. Note that the registerCallbacks function documents these functions as being defined with colon syntax. Every function with colon syntax has an implicit 'self' parameter, which you need to make explicit if you're not using that syntax for whatever reason. So these are all equivalent:

Code: Select all

function gamestatename:mousereleased(x, y, button)
  ...
end

function gamestatename.mousereleased(self, x, y, button)
  ...
end

gamestatename.mousereleased = function(self, x, y, button)
  ...
end
That applies to any library, not just hump. It's very important to identify colon syntax wherever it appears.

In hump gamestate, if you are not using registerCallbacks you can use whatever parameter list and order you prefer.

yintercept wrote: Fri May 24, 2019 3:19 pm- When using flux, in your love.update event, make sure to call flux.update(dt) otherwise your tweens won't run
Yeah, the docs say so, but in the Installation section of the README.md, not in the Usage section. It would perhaps be a good idea to report it to rxi directly, if you have a GitHub account.
User avatar
yintercept
Citizen
Posts: 64
Joined: Mon Apr 02, 2018 3:31 pm

Re: Love2D Library Tips/Gotchas

Post by yintercept »

Thank you pgimeno and zorg for the corrections. I'm not 100% familiar with lua and sometimes, as a beginner, I can't always find what I'm looking for in the docs.

> What docs?

May have been example code I was looking at on that one.

Or it may have been the dyslexia, dunno.
Back in the saddle again.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 178 guests