My latest plugin has brought up a number of issues and questions around initialization and maintenance of both global variables and UI switch and slider states.
I wanted to post a few questions / statements around that, and see if there are some recommended best practices based on previous info from Marcus and Mark. I’m basically storing a lot of memory blocks now.
Problems (when zscript looses focus):
- User switch and slider states are lost
- Global variables, switch / slider states, and memory blocks may be out of sync
Here is what I’ve started to do…
We know that the Zscript sort of re-initializes on re-focus, so I’m using memory blocks to store the users parameter values, and the switch states:
///// ----- Button Var ----- /////
[VarDef, gReFocusVar, 1]
[If, [MemGetSize,MB_FocusVar],
,
[MemCreate,MB_FocusVar,7,0]
[MemWrite,MB_FocusVar,#gReFocusVar]
]
[MemRead,MB_FocusVar,#gReFocusVar]
[If, [MemGetSize,MB_FocusVar],
[Note, [StrMerge, "Init Value: ", #gReFocusVar]]
,]
///// ----- Switch State ----- /////
[VarDef, gSwitchState, 0]
[If, [MemGetSize,MB_SwitchState],
,
[MemCreate,MB_SwitchState,7,0]
[MemWrite,MB_SwitchState,#gSwitchState]
]
[MemRead,MB_SwitchState,#gSwitchState]
When the script comes back into focus, the memory block value sets the switch or slider state.
[IButton, [StrMerge, #ZPLUGIN,":",“Set FocusVar”],"",
,1.0]
[IButton, [StrMerge, #ZPLUGIN,":",“Read FocusVar”],"",
,1.0]
[ISwitch, [StrMerge, #ZPLUGIN,":",“Switch”], #gSwitchState, “Im a switch”,
,1.0]
In Summary:
- On initialization I’m creating global variables AND memory blocks for all things I want persistent (values and ui states)
- Im setting global variable values on initialization from memory blocks
- UI Elements are responsible for setting MemBlocks, and get their state values from the initialization / re-initialization of global variables
- Routines are (usually) responsible for getting MemBlocks and setting Global Variables
- Are there easier / better ways of making sure that all the memory blocks, gvars and ui elements are in sync?
- What I’m doing seems to be working, but I wonder if there are any gotchas?
- I plan on reducing the amount of code by using arrays, haven’t done that yet though
- To make things more complicated I want to save User Settings to a File next so that they can retrieve their states and values between ZBrush sessions
[LIST=1] - I’m assuming that will just a be a set / get routine called by a button in the plugin that just sets the values of all the memory blocks, variables and button states
[/LIST]