ZBrushCentral

Delete variable in ZScript

Is it possible to delete a variable with ZScript? I know you can allocate memory blocks, but can you assign a variable to that and then delete it? I have confused them both with the following script:

// Script
[IButton, “Delete variable”, “Delete variable”,

[IConfig,4.73]

// set a string
[VarDef,s, “Gwen Stefani!”]

[MVarDef,s, 9]

[Note, s,1]

[If,[MemGetSize,s],

[MemDelete, s]

, //else

[Note,“No memory found”,3]

]
[Note, s,1]

]// End of ZScript

Cheers

It’s not necessary to delete variables. You can simply set them to an empty string or zero for numbers:

[VarDef,myStringVariable,“Hello World”] //declare a string variable

[… other code …]

[VarSet,myStringVariable,""] //empty string

[VarDef,myNumberVariable, 6.0]

[… other code …]

[VarSet, myNumberVariable, 0] //zero

You can then test the values in your code as necessary, for example:

[If,[StrLength,myStringVariable] == 0, //there’s no string stored

Also, bear in mind that local variables can go out of scope which means they are available elsewhere in your code. For example, in your code you declare a variable inside a button. That means the variable is only available to code inside the button and can’t be accessed from outside.

ZScript variables don’t persist when the script is unloaded (for example, when another plugin or script becomes active).

Memory blocks are different. They are available anywhere in your code and throughout a ZBrush session. They are a useful way to store data you want to persist.

Hi Marcus,

I’ve noticed a problem with a Zplugin that should delete the UI message string at the end, it but ends up concatenating the string on a repeat use of the button. Ironically I can’t repeat the bug, However, the plugin is similar to this:

[ISubPalette,“Zplugin:My Scripts”]

[IButton,“Zplugin:My Scripts:STRING TEST”,“TEST”,

// DEFine the string
[VarDef, s, “”] //empty
[VarSet, s, [StrMerge, s, "Config file loaded

"]]

// load hotkeys
[VarSet, hkey, “D:\Zbrush\UI\hotkeys.txt”]
[FileNameSetNext,hkey]
[VarSet,f, [FileNameExtract, hkey, 2]]
[VarSet,s, [StrMerge, s, "Hotkeys: ", f, " Loaded!
"]]
[IPress,Preferences:Hotkeys:Load]

// Show message for 2 seconds
[Note,s,2]
[VarSet, s, “”]

]//End of ZScript

I get that behaviour if I take out the last line inside the button so that the string variable isn’t reset. As you’ve got it, it works OK.

The [VarDef, s, “”] is only set once when the script is loaded. So, as long as you don’t use a different plugin and the script isn’t reloaded, that line is ignored and the string is concatenated every time you press the button.

One way you could avoid this is to simply have

[VarDef, s, “”]
[VarSet, s, “”]

at the start of the button. You then don’t need the reset at the end.