[Closed] Unique variables
This is either incredibly simple, or incredibly stupid.
I’m working on a script in which I’ll need to define a bunch of unique variables and store data to reference later.
That having been said, I’d like to use user-input strings to help define the name of the variable.
eg.
Prompt user to enter name: ‘theName’
Declare a string variable called ‘theName’
Simple? Silly?
Help’s much appreciated.
you’ll have to use execute()
rollout roll_test "roll_test" (
edittext edt_test "variable name:" text:"myVar"
spinner spn_test "variable value:"
button btn_show "output variable name and value"
on btn_show pressed do (
-- you'll want to test the text against other disallowed characters in variable names
-- this only makes sure it's not blank
if (edt_test.text != "") do (
execute (edt_test.text + " = " + (spn_test.value as string))
format "% = %
" edt_test.text (execute edt_test.text)
)
)
)
createDialog roll_test
What is the purpose of letting the user define the variable names? There might be a more elegant solution to your problem.
You probably want to use arrays to store variable amounts of user data. Variables are maxscript internals, and probably shouldn’t be exposed to the user.
An alternative to using the execute function is the globalVars structure (2008 or later):
globalVars.set <global var name> <value>
Martijn
Except that it’s not…
The global variable name must exist, otherwise a runtime error will be generated
( The first line of the documentation is a bit misleading in that respect, as it claims that the globalVars structure also lets you create variables. )
You can use globalVars.get / globalVars.set after the first execute() to create the global variable, of course.
I should have tested that myself, but indeed it doesn’t seem to let you create new vars.
Martijn
yeah, it’s a bit odd… almost seems like they simple forgot to add a .create() method; similar to how gamma used to be exposed… you could set display gamma, input gamma, output gamma, etc. etc. – everything except enable/disable gamma
Either way, globalVars is a much more appropriate method once the variable has been created.
Thanks for the responses everyone.
Yeah – I was -way- off on what I was trying to do. Any method which would have had user string defined variables is natively messed up :).
As suggested above (thanks!), I’m using an array to store some data now. I’m using persistent globals in order to keep the data associated with each maxfile.
If I create a new file, however, the data in the array remains, and doesn’t clear. Is there any way to keep it empty / unique with each new max file?
only on file > new? Odds are you’d want it cleared when loading an existing file as well, no?
Anyway… for file > new, it’ll be similar to this:
callbacks.addScript #systemPreNew "yourVariableName = undefined" id:YourID
Place that in a .ms file and stick it in your startup scripts folder.
the reason I use ‘pre’ is so that the variable is cleared first. That way, if you choose to do the same for loading files later, if the scene does have the variable set it should still be okay once the file is actually loaded (#filePostOpen ; see the notes in the help file on distinguishing between a scene load and a preset load… wouldn’t want the latter to clear your variable)
Ze – Thanks -so- much, exactly what I needed!
Moreover, thanks for your patience with my complete ignorance and less than ravenous exploration of the help file :).
no worries, we’re all here to help
by the way, you might want to look into Custom Attributes. You can store these with a scene file as well, without having to worry about persistent globals.
The only ‘problem’ with them is that you can only store a limited set of types, and within each parameter only a single type. So an array of integers, an array of strings, etc. – but not an array with mixed integers and strings.
That would also prevent the need for that callback