is there any function I can use to restart my program?

I know the close() function closes our program, but is there any way to restart it without closing and without using the buttons in the bottom left?

Hello @edat44. Unless you are using ElapsedTime ‘raw’, you can code a ‘restart’ by calling the code that you use to set things up. For example:


function setup()
    initialize() -- A helper function to set things up
end

function draw()
    -- do something, up to 60 times a second
end

function touched(touch)
    if touch.state == BEGAN then -- Restarts if viewer touched
        initialize()             -- Set things up, once again
    end
end

function initialize()
    -- Set things up
end

(Update) I have added to the Issue Tracker a request for a resetTime() function to be added to the Codea API (as #178).

@mpilgrem Thank you, I tried this and was successful in one of my programs, but another one of my programs uses ElapsedTime, so it doesn’t work

@edat44 you could encasulate the elapsedTime value if it is your only problem:

--# Main
function setup()
    elapsedTime2 = ElapsedTime2()
end
function draw()
    tNormal = ElapsedTime
    tResetable = elapsedTime2:read()
    watch("tNormal")
    watch("tResetable")
end
function touched(touch)
    elapsedTime2:init()
end

--# ElapsedTime2
ElapsedTime2 = class()

function ElapsedTime2:init()
    self.t0 = ElapsedTime
end

function ElapsedTime2:read()
    return ElapsedTime - self.t0
end

Touch the screen to reset the ElaspedTime2 value.