brunothepig
brunothepig

Reputation: 43

Lua: When does it call functions?

Using the Love framework by the way.

Ok, so I'm looking to create a random map generation. I obviously only want it drawn once, so I tried to set up a very basic structure. Which isn't working and I can't figure out why.

function love.load()
    testVar = 1
end

function love.draw()
    if testVar == 1 then
        testFunction()
        love.graphics.print("Update", 20, 200)
    end

    love.graphics.print(testVar, 100, 100)
end

function testFunction()
    love.graphics.print("Success", 20, 300)
    testVar = 0
end

What that is doing is only printing "0", thanks to the command to print testVar up in the draw function. So it seems that it's updating the testVar value without actually running testFunction. Is this something to do with Lua?

For those unfamiliar with Love, love.draw is called every frame, love.load only initially.

Upvotes: 0

Views: 686

Answers (1)

Paul Kulchenko
Paul Kulchenko

Reputation: 26754

This code works as expected. It's just love.draw is called every frame (multiple times per second), so the output from the first frame is quickly overwritten by next frames. Usually you use love.update when you need to make changes to your state (for example, based on user input) and love.draw to draw that state on the screen (every frame).

Upvotes: 2

Related Questions