Dumb Question - main.lua runs function defs?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
3amigos
Prole
Posts: 1
Joined: Sun Jun 08, 2014 6:49 pm

Dumb Question - main.lua runs function defs?

Post 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?
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post 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!
scrolly
Prole
Posts: 5
Joined: Thu Feb 27, 2014 6:10 am

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

Post 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!
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

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

Post 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.)
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 171 guests