View Full Version : How to save spinner values

03 March 2005, 08:57 PM
Hi everyone,
I try to write a macroscript that adds some modifiers with random values. This random values will be between min. & max. values that user entered on the rollout. And there is my problem : how can I save/load that limit values to a file ?

03 March 2005, 10:59 PM
You can use a variety of functions. Here's a quick example of one I did. It's very basic with no error handling, but I've yet to come up with any issues with it:

Create a file first:
fn createIni =
iniFile = ((maxFilepath) + "file.txt")
fileS = createFile iniFile
return fileS

then you can write values to it by doing this:

print output to: fileS
close fileS

the "output" value must be converted to a string first, by using the "as string" function:

output = output as string

Um... hope this helps! I'm by no means a great scripter, I just nut out stuff as the work requires it!

EDIT: Make sure you CLOSE the file afterwards!

03 March 2005, 11:49 PM

I prefer to use setINISetting and getINISetting in these cases. is saves you the need to create/open/close the file and parse it. it works very fast and needs far less checks to make sure you don't get an error.

check out this example:

try(destroyDialog roSaveSettings)catch()

rollout roSaveSettings "Save Settings Test"
-- Define a local variable for the INI file name.
-- This way, if there's a need to change the name or directory
-- later, you only need to change it here.
local INIFilename = getDir #scripts + "\\test.ini"

-- the user interface:
spinner spMin "Min.: " range:[0,100,0] type:#integer
spinner spMax "Max.: " range:[0,100,0] type:#integer

-- This function will save the settings to the INI file.
-- We use the INIFilename varaible defined earlier as the file name.
fn saveSettings =
-- The setINISetting function saves a setting in the format for windows INI files.
-- The first argument is the file name (if this file doesn't exist, setINISetting will create it).
-- The second argument is the category (it appears in the file in square brackets [])
-- The third and forth argument are the key=value set. note that the value has to be a string.
setINISetting INIFilename "Range" "Min" (spMin.value as string)
setINISetting INIFilename "Range" "Max" (spMax.value as string)

fn loadSettings =
-- The getINISetting function is similar to setINISetting when it comes to argument
-- except, of course, for the lack of "value" (the forth argument).
-- It return the value as a string. If the value or the file does not exist, it returns an empty string.
-- Use a try()catch() pair to make sure you don't get an error if someone edited the value manually,
-- and the value cannot be converted to the right type (in this case an integer).
try(spMin.value = (getINISetting INIFilename "Range" "Min") as integer)catch()
try(spMax.value = (getINISetting INIFilename "Range" "Max") as integer)catch()

-- Load the settings when the rollout is opened.
on roSaveSettings open do loadSettings()

-- Save the settings when the rollout is closed.
on roSaveSettings close do saveSettings()

createDialog roSaveSettings


03 March 2005, 12:08 AM
Yep... that's a lot nicer! Thanks ofer_z! Goes to show how much time I have to research these things!:)

03 March 2005, 01:37 AM
wow ofer_z this was very helpfull thanksss!!:)

03 March 2005, 09:03 AM
I didn't know you could do this in max. Thanks ofer_z, this will save me a lot of time. vbmenu_register("postmenu_2068280", true);

CGTalk Moderation
03 March 2005, 09:03 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.