Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Looping variables

edited September 4 in Bugs Posts: 1,367

@Simeon - are loop variables, not declared in setup(), nil value by design? Also parameter.watch() variables don’t display if you run a large program in setup().


-- CheckError function setup() -- test() end function test() -- for p = 1,8 do a = 10*p end print(p,a) end

The above print nil 10.

Tagged:

Comments

  • dave1707dave1707 Mod
    edited September 4 Posts: 7,874

    @Bri_G Loop variables are local to the loop. Once you're outside the loop, they don't exist. As for parameter.watch, it's defines in setup and won't display anything until setup is complete. So if a large program is running in setup, you won't see anything for it.

  • Posts: 1,367

    @dave1707 - thanks for the feedback, I picked up on this but didn't know if that was as per design. I have a large program that runs in setup() and can not monitor progress as there is no way to output data.

    I'll hopefully be publishing something soon so you can see what I am trying to achieve. Over a few 100,000 iterations to complete the routine. Guess Codea isn't built for this. Thanks again.

  • dave1707dave1707 Mod
    edited September 4 Posts: 7,874

    @Bri_G You can put that routine in a separate function and call it from draw. Only do so many iterations at a time so that the parameter.watch routine can update. Keep track of where you left off per run and then when your 100,000 iterations are done then don't call that routine anymore. You could also do coroutine if you want to go in that direction.

  • dave1707dave1707 Mod
    edited September 4 Posts: 7,874

    @Bri_G Here's a coroutine to breakup the 100,000 iteration loop and display values with parameter.watch.

    function setup()
        parameter.watch("a")
        parameter.watch("j")
        x1=coroutine.create(xx)
    end
    
    function draw()
        background(40, 40, 50)
        coroutine.resume(x1) 
    end
    
    function xx()
        print("function xx gets called once")
        for z=1,100000 do
            if z%100 == 0 then   --exit routine every 100 iterations 
                j=math.sqrt(z)  -- do something
                a=z             -- move z to a for parameter watch, z is local
                coroutine.yield()
            end
        end
    end
    
  • Posts: 1,367

    @dave1707 - wow, what a find. Every time I run into a problem you dig out Lua facts that I had no idea were available. Tried out multiple usage in function calls and ran into an error. Placed the count reporting in a dedicated function and partially got round the problem. Came up with an error “attempt to read from outside a co-routine. Traced back to an earlier call to the main loops. Some code below to show principle I am using.


    function setup() parameter.watch("a") parameter.watch("j") x1=coroutine.create(xx) end function draw() background(40, 40, 50) coroutine.resume(x1) end function xx() print("function xx gets called once") first() second() third() end function first() -- for z=1,100000 do if z%100 == 0 then --exit routine every 100 iterations j=math.sqrt(z) -- do something updater() end end end function second() -- for z=1,100000 do if z%100 == 0 then --exit routine every 100 iterations j=math.sqrt(z) -- do something updater() end end end function third() -- for z=1,100000 do if z%100 == 0 then --exit routine every 100 iterations j=math.sqrt(z) -- do something updater() end end end function updater() -- a=z coroutine.yield() end

    Thanks again.

  • dave1707dave1707 Mod
    Posts: 7,874

    @Bri_G Ran your above code with no problems. Parameter.watch("a") doesn't give a value because you're outside the for loop. To see the value for a, change updater() to updater(z) and change function updater() to function updater(z) so you pass the value of z to the function and move it there.

  • edited October 26 Posts: 1,367

    @dave1707 - sorry, didn’t explain myself well there, the code above does work I was just trying to show how I was approaching a project with a host of nested loops. The error I was referring to was due to a second call to xx as a function in the draw() function. Latest problem bombs out of Codea - trying garbage collection now to bottom it.

  • dave1707dave1707 Mod
    Posts: 7,874

    @Bri_G You might have to look in the Lua reference manual and read the coroutine section to get an understanding of how they work.

Sign In or Register to comment.