Page 1 of 1

confused about print() statements

Posted: Thu Jan 18, 2018 3:58 pm
by randy0428
I'm VERY confused.

In main.lua I have function love.draw(), the first line of which is:
print("Game has started.").
This is followed by a love.graphics.draw statement that draws the background image.

After this there is a series of if statements to call a draw function depending on the game state, one of which is “splash”.

In the splash.lua file there is a draw function which draws the screen for this scene. It essentially works fine. When the game state is splash, the game draws everything in the splash draw function. The problem is that the above mentioned print statement (print("Game has started.")) doesn’t print in the console as expected, since it is in the love.draw() function BEFORE any of the if statments. Additionally, at the beginning of the main.lua file, immediately after defining gameState = "splash", there is a print statement, print("gameState = " , gameState), which also DOESN’T print in the console.

The funny thing is, when these print statement weren’t printing, in my attempts to figure out why, I added a print statement, print("in stateSplash.draw()"), to the stateSplash.draw() function in the splash.lua file. With this statement in place ALL of the print statements printed: this statement, the one at the beginning of main and the one in the if block in love.draw().

I commented out the print statment in stateSplash.draw() and again, NONE of the print statements printed. I don’t understand this behavior.

Re: confused about print() statements

Posted: Thu Jan 18, 2018 4:29 pm
by zorg
Me neither, which can mean two things.
1. Less likely: Löve (or something that it uses internally) has a bug somewhere;
2. More likely: Your code (or a lib you might be using) has a bug somewhere.

While the information's amount that you have written to describe the symptoms and stuff is a lot, i don't think we (or at least i) can go on just that amount of info, since it doesn't contain too much meaningful things; for example:
- How the processing flow goes between functions, files, etc.
- Whether this isn't just an issue of the console not being enabled (shouldn't be since it did print for you at one point)
- Whether or not you're re-defining print somewhere
etc.

I think you should attach the .love file for us to check out.

Re: confused about print() statements

Posted: Thu Jan 18, 2018 4:33 pm
by erasio
I know that some consoles don't update in real time but simply stack up prints and display them at a later time (in my case this was usually after the game closed).

The solution was to add this at the top of the main.lua:

Code: Select all

io.stdout:setvbuf("no")
Which explicitly states to not buffer but to display live instead.

Maybe it solves your problem too.

SOLVED: confused about print() statements

Posted: Thu Jan 18, 2018 4:52 pm
by randy0428
Thanks zorg and erasio.

Erasio, your solution worked for me. I added "io.stdout:setvbuf("no")" at the beginning of main.lua and commented out the print statement that was "allowing" the other print statements to print and the other print statments printed. I then commented out "io.stdout:setvbuf("no")" and again, they didn't print.

So that solved the problem. I'll keep "io.stdout:setvbuf("no")" there so I don't have a problem in the future.

Thanks again.