love.update() and love.draw()

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
Nihil
Prole
Posts: 2
Joined: Sat Sep 28, 2013 6:52 am

love.update() and love.draw()

Post by Nihil »

Hello. I'm kinda new in Love2d. I was only working with Flash before.

I have a question: what's the point of having two functions called after each other instead of one?
There is already a topic about it: viewtopic.php?f=4&p=64353
But I want to understand, what's the problem of using it.

For example, I have code like:

Code: Select all

local asteroids = {} 

function love.update(dt)
    for _,a in ipairs(asteroids) do
        a.x = a.x + a.sx * dt
        a.y = a.y + a.sy * dt
    end
end

function love.draw()
    for _,a in ipairs(asteroids) do
        love.graphics.draw(a.image, a.x, a.y, a.rotation)
    end
end
Instead of having one cycle I have two. Won't it make my game slower?

Thank you.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: love.update() and love.draw()

Post by Boolsheet »

You don't have to worry about speed until you really are having problems.

The idea behind love.update and love.draw is that you separate the game update logic from the game drawing. If the two are connected too tightly, it might get harder to modify things without breaking the drawing later on.

This is of course just a design choice thing. You can do it however you want. ;)
Shallow indentations.
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: love.update() and love.draw()

Post by slime »

Boolsheet wrote:This is of course just a design choice thing. You can do it however you want. ;)
To clarify, you can change love.run (the actual run loop) to do whatever you want, but the default love.run will clear the screen right before calling love.draw, so you can't actually draw to the main screen in love.update with the default love.run function.
bekey
Party member
Posts: 255
Joined: Tue Sep 03, 2013 6:27 pm

[]

Post by bekey »

-snip-
Last edited by bekey on Fri Jan 24, 2014 2:13 am, edited 1 time in total.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: love.update() and love.draw()

Post by Plu »

It more or less forces you to seperate your game logic from your drawing, which is important because you only usually draw a very small part of your game world and you don't want to waste time drawing things that aren't on the screen. It's basically a seperation of concerns thing.
Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests