Using print() ([edit]:not ElapsedTime) in a coroutine freezes Codea or closes it, it depends. Is it a bug or is it the behavior to be expected? (i am newbe to co-routines, so i don’t know what i should expect). PS: i am on 1.5(7), in case that’s the reason.
[EDIT] after some more test: maybe it is not ElapsedTime, but using print() that generate the problem. Print(“t0”) from the coroutine freeze Codea.
-- test coroutine
-- Use this function to perform your initial setup
function setup()
print("trying to use coroutines")
x1=0
--fps = FPS()
end
longFonc2 = coroutine.wrap(function()
local xmax=100000
local t0,dt = 0,0.01
local restart = true
local killMe = false
local x = 0
while not killMe do
if restart then
x=0
-- print("t0") -- this freeze codea
restart = false
end
t0 = os.clock()+ dt
if x<xmax then
for i=1,xmax do
x = x + 1
if os.clock()>t0 then
coroutine.yield(x,false)
t0 = os.clock()+ dt
end
end
end
restart,_,t0 = coroutine.yield(x,true)
end
end)
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
local a,b
if math.floor(ElapsedTime)~=math.floor(ElapsedTime-DeltaTime) then
a,b = longFonc2(true,false,ElapsedTime)
print(a,b)
else a,b = longFonc2() end
-- fps:draw()
-- write the x on the screen
fill(208, 208, 208, 255)
fontSize(100)
font("AmericanTypewriter-Bold")
rectMode(CENTER)
text(tostring(a),WIDTH/2,HEIGHT/2)
end