How does the game loop work?

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
Stormfury
Prole
Posts: 6
Joined: Mon Nov 23, 2009 7:04 am

How does the game loop work?

Post by Stormfury »

I'm used to have a layout similar to this:

Init()
Loop:
UserInput()
Update()
Render()
CheckForGameOver()
EndLoop
Cleanup

In all of the tutorial examples, we just use one load function, and one update function. Is there a game loop built in somewhere, or must I code this looping functionality myself? I'll have to figure out deltaTime and make sure I can set a desired FPS.

Thanks for the help
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: How does the game loop work?

Post by bmelts »

load() is the equivalent of the Init() function - get everything set up.

UserInput(), as it were, is handled by separate callbacks. keypressed(), keyreleased(), mousepressed(), and mousereleased() are all called when the appropriate event happens, and you can take action there. There's also love.keyboard.isDown and similar functions for checking outside of the callbacks.

update() is where you (surprise) update everything. That function is run once a frame.

Rendering is handled in the draw() function, also run once a frame.

You would check for game over in the update() function. (e.g. if player:isDead() then love.system.exit() end)

LÖVE handles the cleanup itself.

In the upcoming LÖVE 0.6.0, you can override the default game loop if you like and replace it with your own (just declare your own love.run() ). However, the default loop works pretty well for most games.

deltaTime (which is passed to the update function as the dt variable) is just the number of seconds that have elapsed since the last frame. It's fairly trivial to write an arbitrary FPS limiter (make sure you turn vsync off, though).
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 3 guests