0

I'm trying to make a part of a program that's a timer through two loops. The timer will repeat a certain number of times (the it variable) hence the first loop. The second loop is the actual timer. For a certain number of seconds, every second it updates the text of a text object. The problem I face is that when I print the text of the text object to the console, it updates, but the text won't change on the screen itself until all the loops are done. Any help with explanation or anything to help guide me in the right direction would be very much appreciated.

Here is the code, don't worry about the unused parameters:

    local function sleep(s)
        local ntime = os.clock() + s/10
        repeat until os.clock() > ntime
    end
    
    local function watch(mode, it, text, texty, para)
        local text = display.newText(scene.view, "", 
    display.contentCenterX, 200, nativeSystemFont, 60)
        text:setFillColor(0,0,256)

        local i = 0
        local sec = 0
        local goal = 20
        
        text.text = sec
        while (i < it) do
            sec = 0
            while (sec < goal) do
                sec = sec + 1
                print(text.text)
                text.text = sec
                sleep(10)
            end
            i = i + 1
        end
    end
  • your script doesn't return and nothing else can happen until your script returns - like updating the screen. [timer.performWithDelay can be used to make something happen later after your script returns](https://stackoverflow.com/questions/72661382/timer-problems-in-solar2d-lua) – user253751 Jun 17 '22 at 17:47
  • I'll update the original description with more code. Could you elaborate a little bit on why it needs to return? The text is continuasly updated in the loop and the text object was declared locally, I don't understand why anything needs to be returned – Nicholas O'Neal Jun 17 '22 at 17:56
  • Because nothing else runs while your script is running – user253751 Jun 20 '22 at 08:54
  • @user253751 Per the docs, you refer to [this](https://docs.coronalabs.com/guide/media/displayObjects/index.html#screenupdates), I guess. – Dávid Laczkó Dec 28 '22 at 20:47

0 Answers0