How do you "stop" execution of a program?

Thanks to Codea, I have taken up coding again after many years away. Love the program and currently working on my first apps.

I have run into a problem figuring out how to stop execution of a program when a certain condition is met. I am trying to use a counter that counts down from a set starting number down to zero. When zero is reached, I want the loop to stop, run a function to determine win/lose state and take necessary actions. My code counts down fine and reaches zero, does it’s thing and prints the message on the screen. But code continues to count down, in want the code to stop executing at this point and reset wait for user inout to start over. I believe this is because Codea’s draw function is constantly being called. I have searched code samples and forums, but can’t find anything that shows how to “stop” the draw loop. My code is posted below. Any help on subject is most appreciated.

-- track gametime and set gameover state
function gametime()
    gtime = gtime - 1
    -- print (gtime)
    if gtime == 0 then 
        gameover = true
    end
end

-- reset game when countdown timer reaches zero
function resetgame()
    gtime = 100
    gameover = false
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    -- Move to the middle of the screen
    pushMatrix()
    translate(WIDTH/2, HEIGHT/2)
    -- not sure what this line does:
    popMatrix()
    -- Draw our gemMesh
    gemMesh:draw()
-- keeps track of game time   
    gametime()
    text("Time Remaining: "..gtime, 175, HEIGHT - 25)
    if gameover then
        text("OUT OF TIME!!", WIDTH/2, HEIGHT/2)
        -- sound(DATA, "ZgBAKQBIQEBAQEBAAAAAAMaB4T7x8M8+QABAf0BAQEBAc0BA")
        resetgame()
    end     
end

Try this


if CurrentTouch.state == BEGAN then
            resetgame()
end

-- track gametime and set gameover state
function gametime()
    gtime = gtime - 1
    -- print (gtime)
    if gtime == 0 then 
        gameover = true
    end
end

-- reset game when countdown timer reaches zero
function resetgame()
    gtime = 100
    gameover = false
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    -- Move to the middle of the screen
    pushMatrix()
    translate(WIDTH/2, HEIGHT/2)
    -- not sure what this line does:
    popMatrix()
    -- Draw our gemMesh
    gemMesh:draw()
-- keeps track of game time   
    gametime()
    text("Time Remaining: "..gtime, 175, HEIGHT - 25)
    if gameover then
        text("OUT OF TIME!!", WIDTH/2, HEIGHT/2)
        -- sound(DATA, "ZgBAKQBIQEBAQEBAAAAAAMaB4T7x8M8+QABAf0BAQEBAc0BA")
        if CurrentTouch.state == BEGAN then
            resetgame()
        end
    end     
end

You want a state machine.

function setup()
   state = "title"
end

function draw()
   if (state == "title") then drawTitle() end
   if (state == "play") then drawPlay() end
   if (state == "paused") then waitForUnpause() end
   else doStuff() end
end

So - the above would start by calling drawTitle() and doStuff(), and presumably at some point (an elapsed time?) doStuff would change state to “play”, and then the loop would be calling drawPlay() and doStuff(). If you do all of your drawing in the draw() methods, and your processing (changing variables and so on) in doStuff, this will allow you to run in different modes.

Look at the physics example for how this would work in practice.

There’s no stoppin the draw loop. Drawloop be drawin, playa. If you stop the drawloop, how you gonna draw the menu?

You need a state machine like bortels said.

Check this:

function setup()
    print("touch to pause.")
    secondsRemaining =30
    gameState = "game"
    prevTime = ElapsedTime
end

function draw()
    if gameState == "game" then showGame()
    elseif gameState == "paused" then showPause()
    elseif gameState == "menu" then showMenu()
    end
end

function showGame()
    if (ElapsedTime - prevTime) >= 1 then
        secondsRemaining = secondsRemaining - 1
        prevTime = ElapsedTime
    end
    if (secondsRemaining) <= 0 then
        gameState = "menu"
    end
    background(40, 40, 50)
    font("Georgia")
    fill (255)
    fontSize(20)
    textWrapWidth(100)
    text(secondsRemaining, WIDTH/2,HEIGHT/2)
end

function showPause()
    background(255, 0, 0, 255)
    font("Georgia")
    fill(255, 255, 0, 255)
    fontSize(20)
    textWrapWidth(100)
    text("Paused. Touch to resume.", WIDTH/2,HEIGHT/2)
end

function showMenu()
    background(100, 255, 0, 255)
    font("Georgia")
    fill(71, 0, 255, 255)
    fontSize(20)
    textWrapWidth(100)
    text("Menu Screen. Touch to start.", WIDTH/2,HEIGHT/2)
end

function touched(touch)
    if touch.state == ENDED then
        if gameState == "menu" then
            --reset
            secondsRemaining = 30
            gameState = "game"
        elseif gameState == "game" then
            gameState = "paused"
        elseif gameState == "paused" then
            gameState = "game"
        end
    end
end

Guys,
Thx for the assistance. Very helpful. Chuggin’ along again now!

You can also easily implement a state machine (and something like C’s switch statement) in lua by using a table.

function statePlaying()
  -- code blah!
end

function statePaused()
  -- code blah!
end

function stateGameOver()
  -- code blah!
end

stateFunctions = {
    playing = statePlaying, 
    paused = statePaused,
    gameover = stateGameOver
}

local state = "playing"

function draw()
    stateFunctions[state]()
end

Very neat solution. Thank you @TechDojo.