Why does this freeze Love?

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
User avatar
Buddy4point0
Prole
Posts: 35
Joined: Wed Jan 12, 2011 9:29 pm
Location: Virginia

Why does this freeze Love?

Post by Buddy4point0 »

I'm trying to make my game pause, so I've done:

Code: Select all

play = true

function love.run()
	while true do
		if play then
			blah blah blah
			blah blah blah
			blah blah blah
		else
			if love.keyboard.isDown("return") then
				play = true
			end
		end
	end
end
The game pauses, but when you press return to go back to the game it freezes love.
Shouldn't it just continue on with the script, and go back to the game since "play" is true again?
╚»ßuddy4point0 ■
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Why does this freeze Love?

Post by nevon »

Any reason you're redefining love.run?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Why does this freeze Love?

Post by Robin »

The problem is probably that you're not processing any events.

But what's wrong with the simpler

Code: Select all

function love.update(dt)
   if not paused then
     -- blah
   end
end

function love.keypressed(key, unicode)
    if key == 'p' then
        paused = not paused
    end
end
Help us help you: attach a .love.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Why does this freeze Love?

Post by thelinx »

Don't forget to define "paused" somewhere!
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Why does this freeze Love?

Post by Taehl »

In Robin's code (which I would advise using), you don't need to define 'paused'.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Why does this freeze Love?

Post by thelinx »

Taehl wrote:In Robin's code (which I would advise using), you don't need to define 'paused'.
oh yeah.
User avatar
Buddy4point0
Prole
Posts: 35
Joined: Wed Jan 12, 2011 9:29 pm
Location: Virginia

Re: Why does this freeze Love?

Post by Buddy4point0 »

nevon wrote:Any reason you're redefining love.run?
I don't know! I'm new to love, can you explain what the benefits are of redefining it and not redefining it?
Robin wrote:The problem is probably that you're not processing any events.
Well that wasn't my entire code! I was processing events lol.
Robin wrote:But what's wrong with the simpler

Code: Select all

function love.update(dt)
   if not paused then
     -- blah
   end
end

function love.keypressed(key, unicode)
    if key == 'p' then
        paused = not paused
    end
end
Okay, thanks. I used a variation of this and it works. I still don't really understand why it didn't work before though.
thelinx wrote:Don't forget to define "paused" somewhere!
Paused was never used in my code! I used "play" which would be set to true or false, and checked if it was true before the game operated.
The only thing that was outside of the "play == true" check was that if play was false, you can hit enter to set "play" back to true. :awesome:
But I've used a variation of Robins script which works great.
╚»ßuddy4point0 ■
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Why does this freeze Love?

Post by kikito »

Buddy4point0 wrote:
nevon wrote:Any reason you're redefining love.run?
I don't know! I'm new to love, can you explain what the benefits are of redefining it and not redefining it?

Redefining love.run is unnecessary 95% of the time. Unless you really know what you are doing, or you have a need that the default love.run implementation doesn't provide, you don't need to touch it at all. I once also thought I needed to modify it, but then I realized I didn't.

Instead, use love.load to load up resources (images, sounds, etc), use love.update to move things around, and love.draw for drawing them. That should be more than enough for starting with LÖVE. Once you dominate them, re-evaluate your need of modifying love.run; you will probably still don't need to.
When I write def I mean function.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Why does this freeze Love?

Post by nevon »

Buddy4point0 wrote:
nevon wrote:Any reason you're redefining love.run?
I don't know! I'm new to love, can you explain what the benefits are of redefining it and not redefining it?
Redefining love.run gives you more control over the main loop (updating, user input, drawing, etc.) but I'd say it's completely unnecessary for 99% of all Lövers. Most people simply use some variation of:

Code: Select all

function love.load()
    --initializing stuff
end

function love.update(dt)
    --Updating said stuff
end

function love.draw()
    --Drawing stuff
end

function love.mousereleased(x,y,button)
    --processing mouse clicks
end

function love.keyreleased(key, unicode)
    --processing key presses
end
There are other callbacks as well, such as love.focus, love.mousepressed, love.keypressed, love.joystickpressed, love.joystickreleased and love.quit, but the point is that Löve already provides you with a game loop (love.run), so unless you have a good reason to change that, you probably shouldn't.

EDIT: Argh! Ninja'd by Kikito.
User avatar
Buddy4point0
Prole
Posts: 35
Joined: Wed Jan 12, 2011 9:29 pm
Location: Virginia

Re: Why does this freeze Love?

Post by Buddy4point0 »

I see. I was modifying it so I could pause everything except the draw functions.
But now I see that if I had everything in the update function and simply paused love.update it does the same thing! :crazy:
╚»ßuddy4point0 ■
Post Reply

Who is online

Users browsing this forum: No registered users and 93 guests