ZBrushCentral

Question: Routine paramaters

Diving into zscripting: Can I just check a couple of things about routines (scope, local variables etc)

In order to return a variable from a routine you need to use VarSet, rather than VarDef
Like the example below:

[IButton, “routine”, “doubles a number routine”,

[RoutineDef, double_it,
// doubles the given parameter
[VarSet, n, n*2]
,n]

// define num
[VarDef, myNumber, 4]

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

]/End of ZScript/

Incidentally, in the zscript above, the number will continue to double each time you press the button. How do you reset this, so it only happens once?

You don’t want to put your routines inside a button. There’s no point - you might just as well have the code in the button without wrapping it in a routine - and you might end up with problems. Routines are a useful way of avoiding duplicating code and you might want to access them from more than one button.

Variables that are declared in the routine are local and shouldn’t be declared elsewhere. You can reset your number by simply using [VarSet] in a button, or in the example below, reloading the zscript clears the variable and resets it to its VarDef value:

[VarDef, myNumber, 4]

[RoutineDef, double_it,
// doubles the given parameter
[VarSet, n, n*2]
,n]

[IButton, “Double It”, “doubles a number”,

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

[If,myNumber == 32,[VarSet,myNumber,3]]
[If,myNumber >= 96,[IPress,ZScript:Reload]]

]/End of ZScript/