Page 1 of 1

Dumb Question - main.lua runs function defs?

Posted: Sun Jun 08, 2014 7:00 pm
by 3amigos
main.lua hello world example

function love.draw()
love.graphics.print('Hello World!', 500, 300)
end

i'm new to lua, and from what i understand, any block starting with the word 'function' is a function definition. to call it you would do something like:

love.draw(love.graphics.print('Hello World!', 500, 300))

what am i not getting? Is there a secret file that is operating on main.lua to call love.draw() for me?

Re: Dumb Question - main.lua runs function defs?

Posted: Sun Jun 08, 2014 7:13 pm
by Jasoco
Yes, there is a default lua file in the Löve application that has a function called love.run which consists of the following:

Code: Select all

function love.run()

    if love.math then
        love.math.setRandomSeed(os.time())
    end

    if love.event then
        love.event.pump()
    end

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

    -- We don't want the first frame's dt to include time taken by love.load.
    if love.timer then love.timer.step() end

    local dt = 0

    -- Main loop time.
    while true do
        -- Process events.
        if love.event then
            love.event.pump()
            for e,a,b,c,d in love.event.poll() do
                if e == "quit" 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,d)
            end
        end

        -- Update dt, as we'll be passing it to update
        if love.timer then
            love.timer.step()
            dt = love.timer.getDelta()
        end

        -- Call update and draw
        if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled

        if love.window and love.graphics and love.window.isCreated() then
            love.graphics.clear()
            love.graphics.origin()
            if love.draw then love.draw() end
            love.graphics.present()
        end

        if love.timer then love.timer.sleep(0.001) end
    end

end
It handles all the love.* calls.

Your first snippet of code is correct. The second one is incorrect.

Also, use the CODE tags please.

And this should be in Support not General.

Welcome to the forum!

Re: Dumb Question - main.lua runs function defs?

Posted: Wed Jun 11, 2014 9:28 pm
by scrolly

Code: Select all

function love.run()

    while true do
...
        if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled
...
        if love.window and love.graphics and love.window.isCreated() then
            if love.draw then love.draw() end
Doesn't checking for love.update and love.draw every loop create a (small) performance hit?

Don't almost most programs have a .update and .draw function defined?

just curious!

Re: Dumb Question - main.lua runs function defs?

Posted: Wed Jun 11, 2014 10:36 pm
by slime
scrolly wrote:Doesn't checking for love.update and love.draw every loop create a (small) performance hit?
Nope. To have a performance hit you'd need many hundreds or thousands of those checks per frame rather than just two (and that's assuming there's no branch prediction or anything going on under the hood, which there could be.)

Re: Dumb Question - main.lua runs function defs?

Posted: Thu Jun 12, 2014 10:45 pm
by Jasoco
slime wrote:
scrolly wrote:Doesn't checking for love.update and love.draw every loop create a (small) performance hit?
Nope. To have a performance hit you'd need many hundreds or thousands of those checks per frame rather than just two (and that's assuming there's no branch prediction or anything going on under the hood, which there could be.)
Yeah. The hit is super small. I used to have the same concerns as you and would create my own edited love.run function that removed the checks. But there's no reason to. For the most part just leave love.run as it is.