Calculation inside 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
chute
Prole
Posts: 18
Joined: Sat Sep 17, 2011 6:49 pm

Calculation inside love.draw?

Post by chute »

Hi all.

First off, I apologize, as I'm sure I've seen the answer to this before, I just don't remember where, what the answer is, or what to search for.

Besides dt in love.update and drawing functions in love.draw, is there any real difference between love.update and love.draw?

can I just use:

Code: Select all

function love.update(dt)
delta = dt
end
and do all of the rest of my coding in love.draw?

Thanks!
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Calculation inside love.draw?

Post by dreadkillz »

In the default love.run, love.draw gets called after love.update. There's no difference if you put everything in love.draw or love.update except you can't draw stuff outside of love.draw. You can pass dt to love.draw if you want. It's good practice though, I believe, to separate your game logic from your drawing operations.

https://love2d.org/wiki/love.run

No love.update:

Code: Select all

function love.run()

    if love.load then love.load(arg) end

    local dt = 0

    -- Main loop time.
    while true do
        if love.timer then
            love.timer.step()
            dt = love.timer.getDelta()
        end
        if love.graphics then
            love.graphics.clear()
            if love.draw then love.draw(dt) end -- pass dt to love.draw
        end

        -- Process events.
        if love.event then
            for e,a,b,c in love.event.poll() do
                if e == "q" then
                    if not love.quit or not love.quit() then
                        if love.audio then
                            love.audio.stop()
                        end
                        return
                    end
                end
                love.handlers[e](a,b,c)
            end
        end

        if love.timer then love.timer.sleep(1) end
        if love.graphics then love.graphics.present() end

    end

end
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: Calculation inside love.draw?

Post by Ensayia »

Technically there isn't anything stopping you from calling 'calculation' in love.draw() however by default love.draw does not have access to delta time (dt). THe developers have split game logic and drawing to separate callbacks to keep things neat.

It's not very good practice to use love.draw() as an update loop.

EDIT: Also, unless you have a very good specific reason there's no need to modify love.run().
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Calculation inside love.draw?

Post by Inny »

The reason that dt is given to love.update is because you're going to use it for moving sprites, and things like AI and collision detection use sprite locations.

Personally, I prefer making dt available for love.draw, so that animations can be done there. But, I stay with the spirit of the variable and pass it as a parameter to functions, rather than accessing a global variable. If I should need to not-animate, for some reason, I can pass a dt of 0.
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: Calculation inside love.draw?

Post by Ensayia »

Just an FYI, you can call love.timer.getDelta() anywhere. This is how love.run() assigns it to dt.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Calculation inside love.draw?

Post by Lafolie »

Ensayia wrote:Just an FYI, you can call love.timer.getDelta() anywhere. This is how love.run() assigns it to dt.
Since things are designed so that little-to-no calculations are performed in the draw step, you shouldn't need to use getDelta very often since you can just pass/expose dt to your object/function. If you're referencing it a lot I guess you could trim a few calls by using dt instead of manually getting it from getDelta. References are cool.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
chute
Prole
Posts: 18
Joined: Sat Sep 17, 2011 6:49 pm

Re: Calculation inside love.draw?

Post by chute »

Thanks all for your help!
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests