ZBrushCentral

Question: Recursion routine call

I’m hoping that you can do recursion with ZScript:

[VarDef, myNumber, 1]

// 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 …

[RoutineDef, fibonacci,

[If, n <= 2
, // Then…
[VarSet, n, 1]
, // Else…
[VarSet, n, [RoutineCall, fibonacci, n-1] + [RoutineCall, fibonacci, n-2]]
]
,n]

[IButton, “Fibonacci”, “Fibonacci Sequence”,

// call the routine
[RoutineCall, fibonacci, myNumber]
[Note, myNumber, 1]

]/End of ZScript/

I get a closing bracket error - but I suspect it’s actually setting n as the routine call which is wrong. Any pointers, Marcus?

Can’t you just use a loop?
Or maybe http://docs.pixologic.com/user-guide/customizing-zbrush/zscripting/command-reference/#VarAdd

There’s an error in you script in that you can’t set a variable with a routine call inside the command. There’s no need - if the routine is defined as

[RoutineDef,fibonacci,
//code
,n]

then the routine will set a new value for the ‘n’ variable which will be passed back out.

But zscript does not allow recursive calls and you’ll get an error along the lines of “the number of inputs exceeds the number allowed by the target routine”.

@Doug - Yes that are other ways to cook up Fibonacci numbers, this however was a test in Zscript recursion.

@Marcus - Thanks for pointing that out I’ll steer away from recursion from now on.

Yes, need steer away from recursion