# Beginner question - program speed control

Hi all

I was wondering if someone could point me in the right direction. I am completely new to programming and this is my very first project, and im having some problems with the draw() function and speed control.

I am writing a conway’s game of life simulator (http://en.wikipedia.org/wiki/Conway’s_Game_of_Life). Basically, i have created a 50 x 50 grid (once i have it working i am going to try to implement an expanding grid and some form of zoom and/or scrolling). My problem is controlling the update speed. I have a function updateGrid() which iterates through each square and updates its state based on the other squares sorrounding it. I am using iparameter(speed, 0, 30) and want the grid to update between 2 x per sec and every 5 seconds based on the slide bar. My problem is getting updateGrid() to call at the correct time based on the input from speed. I really cant get my head around how to use draw() in this situation.

Any tips would be greatly appreciated.

Thanks!

You could try the following, this ties your update rate to your frame rate:

Edit: Just read more of your post, that you need less than once-per-second upates. The below is not a good way to do this. Will post another solution.

``````function setup()
frameCount = 0

iparameter( "UpdateRate", 1, 60, 30 )
end

function update()
-- Do some updating at fixed intervals
end

function draw()
background(0)

-- Do your drawing here

local frameMod = 61 - UpdateRate

if frameCount % frameMod == 0 then
update()
end

frameCount = frameCount + 1
end
``````

Here UpdateRate is how many times you want to update per-second (assuming 60 frames per second, which is the optimal-case frame rate of Codea).

(If your frame rate slows due to complexity the UpdateRate value won’t represent a true per-second rate. But that might not matter to you.)

So when UpdateRate == 1, it updates once per second. And at 60 it updates 60 times per second (so in practice, every frame).

This is a more flexible solution not tied to the frame rate (probably better).

``````function setup()
currentTime = 0

parameter( "UpdateInterval", 0.1, 5, 1 )
end

function update()
-- Update here
end

function draw()
currentTime = currentTime + DeltaTime

background(0)

-- Draw

if currentTime >= UpdateInterval then
update()
currentTime = 0
end
end
``````