# Tween faster than 1/60 of a second.

I know that draw is called 60 times a second, and so I was wondering if tween is the same way? For example if I did something like tween.delay(1/70, function) would it actually delay for a whole 1/60 of a second?

I don’t believe it’s possible

@JakAttak I couldn’t find any way to time the tween.delay, but this might answer your question. I print “start” with a count each time draw() is called. If I do a tween.delay() for 1/70, or 1/60, then it prints “start 1”, “delay 1”, “start2”, “delay2” which means the tween is firing as fast as draw() is running. If I do a delay for 1/59, then it prints “start 1”, “start 2”, “delay 2”, “delay 2”, “start 3”, “delay 3”, which means the second draw() was called before the first tween.delay() fired. So it looks like tween.delay will run as fast as draw() or as fast as you can call it. So I think a tween.delay() of 1/70 will delay for 1/70 of a second. I only print 5 loops, that’s enough to show what I’m trying to explain.

``````
function setup()
count=0
end

function draw()
background(40, 40, 50)
count = count + 1
if count<6 then
print("start",count)
tween.delay(1/70,delay) -- 1/70, 1/60, 1/59
end
end

function delay()
print("delay",count)
end

``````

The draw event fires every 1/60 of a second, so I think that is the fastest you can go in practice - especially if you are trying to draw something!

I tried this:

``````function setup()
count=0
tcount = 0

delay()
draw()
end

function draw()
background(40, 40, 50)

count = count + 1

if count < 10 then
print("start", count)
end
end

function delay()
tcount = tcount + 1

if tcount < 10 then
print("delay",tcount)

tween.delay(1/1000, delay) -- 1/70, 1/60, 1/59
end
end
``````

I have concluded that tween will only fire as fast as draw

I believe tween is linked into the draw loop (Kinda like Codea changes the draw function to `function draw() ... tween:draw() end`) So tween events should be called with the specified time, but it will only happen during the draw event. And it is sort of unnecessary to tween faster than draw, as thats the only time where you will see your results!

Tween fire at the specified time (tween.delay can proove it). It can be faster than draw. But it wont accelerate draw.

I was just wondering because when the program lags and draw slows down (I think) I wanted to make sure tween still fires at the right rate

@JakAttak You were wondering if a tween.delay() still fires on time when a program is under a load. Well, wonder no more. I’m not sure if draw() drives the tween delay or if something else drives it, but it STOPS. Run this program and you will see the delay() function getting called every 2 seconds. It will print the delay count and the seconds. The draw() function gets called about 120 times between delays. Let the program run for about 10 seconds so you can see what’s happening. After that, tap the screen which will cause the touched() function to put a heavy calculation load on the program. You’ll see that the draw() and the tween.delay() functions stop. So you can’t count on the tween.delay() function to fire at the right rate.

``````
function setup()
dCount=0
w=WIDTH/2
h=HEIGHT/2
fill(255)
callDelay()
end

function callDelay()
count=0
tween.delay(2,delay)
end

function draw()
background(40,40,50)
count = count + 1
text("Draws called since last delay    "..count,w,h)
end

function touched(t)
if t.state==BEGAN then
print("function touched called")
for z=1,5000 do
for y=1,1000 do
s=math.sqrt(z)
end
end
end
end

function delay()
dCount = dCount + 1
tm=os.date("*t")
print("delay "..dCount,tm.sec)
callDelay()
end

``````

Thanks @dave1707

The tween.delay function can keep relatively accurate timing I have seen in use, but it will stop if you create a massive loop such as the one shown in the example above. Seeing as the tween library is implemented in Lua, I am pretty sure that it is drawn. My understanding is that It calls an internal function tween.update() every draw cycle. This compares the tween.running to tween.time and then removes the tween from the internal Tweens table if this time has elapsed, or you called for a tween.stop. All the Tweens are just stored as tables with entries for their initial, target, running, max time, and easing values, and you can even read these values or modify them while a tween is running or after it has stopped. Tweens are not entirely accurate, but it’s timing is pretty good and reliable most of the time.

Here’s another version if you want to see how FPS of draw() affects the tween.delay() function. Just adjust the sliders d1 and d2 for a different FPS. You can also adjust the tween.delay() rate. The time is displayed in the output pane. Use “clr” to clear the output window if you want. The FPS has to get really low before the tween.delay() is affected.

``````
function setup()
parameter.integer("d1",1,2000,100)
parameter.integer("d2",1,2000,100)
parameter.integer("dly",1,5,2)
parameter.boolean("clr",false)
w=WIDTH/2
h=HEIGHT/2
fill(255)
callDelay()
end

function callDelay()
count=0
tween.delay(dly,delay)
end

function draw()
background(40,40,50)
count = count + 1
for z=1,d2 do
for y=1,d1 do
s=math.sqrt(z)
end
end
text("Draws called since last delay    "..count,w,h)
text("FPS "..1/DeltaTime,w,h-50)
if clr then
output.clear()
clr=false
end
end

function delay()
print(os.date())
callDelay()
end

``````