1. Home
  2. Docs
  3. General Programming
  4. Using Functions

Using Functions

Functions are great way for organizing and re-using your code.
In SceneMax we declare functions by name and do-end do code block for example:


1
2
function rotateRight = do
end do

We declared a function named rotateRight and attached to it a do-end do block.
Now let’s see a fully working program using function:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Sinbad is a model
David is a Sinbad

function rotateRight = do
  David.rotate(y+45) in 5 seconds
end do

function rotateLeft = do
  David.rotate(y-45) in 3 seconds
end do

do 10 times
  run rotateRight
  run rotateLeft
end do

Running this program will rotate Sinbad model left and right 10 times in a loop.
A function can also receive arguments. For example look at the following code:


1
2
3
4
5
6
7
8
9
10
function rotateModel(m) = do async
  m.rotate(y+45) in 5 seconds  
end do

s is a Sphere
Ninja is a model
n is a Ninja

run rotateModel(s)
run rotateModel(n)

The code above demonstrates a function which receives an argument “m” and runs the method rotate on that argument assuming it is a 3D model. Later we call (run) that function twice first sending it a Sphere and then a Ninja.
Notice that the function’s do-end do block is defined with an async attribute so both Sphere and Ninja will rotate at the same time.

Was this article helpful to you? Yes No

How can we help?