debuggingluaconsole.loglove2d

How to display debug info or console.log equivalent in Lua


I am creating many games using Lua and LOVE2D, but whenever I implement a new function and want to test it out, or simply want to know a value of a variable in Lua, I either display it on the game screen or just hope that it works.

Now my question is... IS THERE A WAY TO DISPLAY SOME INFO, such as A VARIABLE VALUE or something else into the terminal or somewhere else? Just like console.log in javascript which displays some content in the javascript console in the browser. So, is there a way to do this is Lua?? using LOVE2D?

I am using a Mac, so I have a terminal and not a command prompt. Is there a way to display some content there? Anywhere else would also be fine, I just need to see if those values are as expected or not.


Solution

  • Use a conf.lua file to enable the console, then you should be able to use a standard print(). You can read the wiki entry here.

    Note: You have to run Lua and Love2D via the terminal for this to work. Running Lua and Love2D like this is required for the print statements to show:

    /Applications/love.app/Contents/MacOS/love "/Users/myuser/Desktop/love2d-test-proj"
    

    You just need to add a conf.lua file to the same location where your main.lua. Your file may be as simple as this:

    function love.conf(t)
        t.console = true
    end
    

    But feel free to copy the whole configuration file from the above link and edit what you need.

    I can't be completely sure about this, because I have no access to Mac, but the console is disabled by default and even on Windows, no prints are shown until you turn it on.

    Alternatively You can also display debug info in the game itself like some games do.

    What I like to do is add something like debugVariable = {} for logging events that happen in each loop and debugPermanent = {} for events that happen rarely. Possibly add convenience functions for writing to the variables:

    function debugAddVariable(str)
      table.insert(debugVariable, str)
    end
    --..and similarly for debugPermanent
    

    Now a function to draw our debug info:

    function debugDraw()
      love.graphics.push()  --remember graphics state
    
      love.graphics.origin()  --clear any previous transforms
    
      love.graphics.setColor(--[[select color for debug info]])
      love.graphics.setFont(--[[select font for debug info]])
    
      for i, v in ipairs(debugPermanent) do
        love.graphics.print(v)
        love.graphics.translate(0, --[[fontHeight]])
      end
      for i, v in ipairs(debugVariable) do
        love.graphics.print(v)
        love.graphics.translate(0, --[[fontHeight]])
      end
      
      debugVariable = {}   --clear debugVariable to prepare it for the next loop
      love.graphics.pop()  --recall graphics state
    end
    

    And we just call this draw function at the end of our love.draw() and the texts should appear.

    Obviously, this method can be refined further and further almost infinitely, displaying specific variables, and adding graphs for some other variables to clarify the information you want to show, but that's kind of outside of the scope of the question.

    Lastly Feel free to check here for debug libraries submitted by users.