ZBrushCentral

How to save variables in a script that cycles

I been trying to get this work but had no luck, the first press works and save the Var butt loses it on the 2nd press and give a error, how to save make the Var stay during the cycles >.<

[RoutineDef,Test,

//Create Memory Block to store which cycle the macro is on [If,[MemGetSize,MP_MaskCycleMemBlock],, //Create and then set to 0 [MVarDef,MP_MaskCycleMemBlock,1,0] [MVarSet,MP_MaskCycleMemBlock,0,0] //CurrentCycle ] [VarSet,CurrentCycle,[MVarGet,MP_MaskCycleMemBlock,0]] //Get Subtool [If,CurrentCycle==0, [VarSet,toolID,[IGet,Tool:Item Info]] ,] //Go To Subtool [If,CurrentCycle==1, [ISet,Tool:Item Info,toolID] ,] [If,CurrentCycle&gt;=2, [VarSet,CurrentCycle,0] [MVarSet,MP_MaskCycleMemBlock,0,CurrentCycle] , [MVarSet,MP_MaskCycleMemBlock,0,CurrentCycle+1] ]

]//Routine Test

[IButton,???,“Toggles to Subtool. (Hotkey for best use!)”,
[IShowActions,0]
[IConfig,4.73]
//[IFreeze,
[RoutineCall,Test]
//]
,1,]

I’m not sure what you are trying to do. Your code works for me but I don’t know if it’s what you intended:

1st button click : current tool is stored
2nd button click : tool is switched to stored tool
3rd button click : does nothing
4th button click : start of cycle again and current tool stored

The best way to check what’s happening is to put a Note in at some appropriate place:

[VarSet,CurrentCycle,[MVarGet,MP_MaskCycleMemBlock,0]]
[Note,[StrMerge,"CurrentCycle is ",CurrentCycle],.5]

However, the problem with a script of this sort is keeping track of when a tool is stored. You could display a Note or a message in the NoteBar to let the user know. Or you could use a different approach. For example, in the code below pressing the button will store the current tool, using the hotkey will switch to the stored tool.

[IButton,???,“Switches to stored Subtool. Press button to Store, use Hotkey to switch.”,
[IShowActions,0]
[IConfig,4.73]
//create memory if necessary
[If,[MemGetSize,MC_ToolID],[MVarDef,MC_ToolID,1,-1]]
//check if button is being pressed
[If,[IGetID,0]==[IGet,Preferences:Utilities:View window Id],

//button pressed - store tool

[MVarSet,MC_ToolID,0,[IGet,Tool:Item Info]]

,//else hotkey used so switch to tool

[If,[MVarGet,MC_ToolID,0]>0,

[ISet,Tool:Item Info,[MVarGet,MC_ToolID,0]]

]

]
,1,]

The Code is meant to save current subtool so if I change subtools and press the button for a 2nd time it will go back. But the script give the following error on Cycle1.
Not sure how to get it to not forget the toolID for the 2nd press of the button

“SCRIPT: Test.TXT
ERROR: toolID
IN:[ISet,Tool:Item Info,toolID]”

CODE

[RoutineDef,Test,

//Create Memory Block to store which cycle the macro is on
[If,[MemGetSize,MP_MaskCycleMemBlock],
//Create and then set to 0
[MVarDef,MP_MaskCycleMemBlock,1,0]
[MVarSet,MP_MaskCycleMemBlock,0,0] //CurrentCycle
]
[VarSet,CurrentCycle,[MVarGet,MP_MaskCycleMemBlock,0]]
[Note,[StrMerge,"CurrentCycle is ",CurrentCycle],.5]

//Get Subtool
[If,CurrentCycle==0,
[VarSet,toolID,[IGet,Tool:Item Info]]

,]

//Go To Subtool
[If,CurrentCycle==1,
[ISet,Tool:Item Info,toolID]
,]

[If,CurrentCycle>=1,
[VarSet,CurrentCycle,0]
[MVarSet,MP_MaskCycleMemBlock,0,CurrentCycle]
,
[MVarSet,MP_MaskCycleMemBlock,0,CurrentCycle+1]
]

]//Routine Test

[IButton,???,“Toggles to Subtool. (Hotkey for best use!)”,
[IShowActions,0]
[IConfig,4.73]
//[IFreeze,
[RoutineCall,Test]
//]
,1,]

I expect what’s happening is that you are using another plugin or macro after first pressing the button. Values stored using VarSet won’t persist if another script becomes active. You’ll need to use a memory block to store the value, then it will work OK. You can use the same block, just create it so it can store both values:

[MVarDef,MP_MaskCycleMemBlock,2,0]

// then when you store the tool ID:

[MVarSet,MP_MaskCycleMemBlock,1,[IGet,Tool:Item Info]]

// and to select the stored tool:

[ISet,Tool:Item Info,[MVarGet,MP_MaskCycleMemBlock,1]]

Thanks it works now! :smiley: