[Closed] Create rollouts inside functions or not
Hi,
I’m trying to clean up a large collection of scripts, so that I can release them online. I’m not really a proper scripter (just faking it), and there are some things that have bothered me for some time:
Is it OK to create rollouts and floaters inside a function? For the past years I’ve done buildGUI() functions that does this. But when I try to update an old script to this method, I can’t fetch slider and text fields from the rollouts any longer.
I’ve always had problems bringing values in and out of rollouts. For example, this script has a character height value in one rollout spinner, that other rollouts events need to fetch before running scripts. At the moment I use a global variable to push this value in and out of the spinner when it’s changed or needed. Is this OK, or is there a better method?
Also related, I need to make certain buttons and elements disabled if a script component is missing. I’m thinking about using a global variable for this as well, and it will be checked each time the script is started. I keep reading that programmers should stay away from global variables, but very often my brain finds that to be the easiest solution
I’m just a “make it up as I go” scripter too. But I usually create functions as part of rollouts, rather than the other way around. I always define the rollout as global so when you start another instance of the dialog, you can auto shut the last instance of it down automatically.
And if I have more than 1 rollout (say within a rollout floater) and need to access functions or UI values within other rollouts, I can just call them via the global rollout name. (Which I make quite descriptive and verbose to avoid possible conflicts. This negates the need to define lots of global variables. Don’t know if this will help you, but might give you something to think about.
Cheers,
Cg.
yes, you can create a rollout inside a function. what’s after?
you can find some samples on this forum where i create rollout in function, or in structure…
Here’s an quick example of rollouts created by functions inside a structure all the rollouts are visible to each other and only one global variable:
(
clearListener()
try (closeRolloutFloater ::rollTest.rollFloat) Catch()
struct rollouts
(
public rollFloat, rollout1, rollout2, rollout3,
fn createRollout1 =
(
local r = rollout roll1 "Rollout 1"
(
on roll1 open do
(
roll1.rollStruct = this
)
local rollStruct
fn printCBs =
(
print rollStruct.rollout1.cb1.state
print rollStruct.rollout2.cb2.state
print rollStruct.rollout3.cb3.state
)
button b1 "Button 1"
checkBox cb1
on b1 pressed do printCBs()
)
r
),
fn createRollout2 =
(
local r = rollout roll2 "Rollout 2"
(
on roll2 open do
(
roll2.rollStruct = this
)
local rollStruct
fn printCBs =
(
print rollStruct.rollout1.cb1.state
print rollStruct.rollout2.cb2.state
print rollStruct.rollout3.cb3.state
)
button b2 "Button 2"
checkBox cb2
on b2 pressed do printCBs()
)
r
),
fn createRollout3 =
(
local r = rollout roll3 "Rollout 3"
(
on roll3 open do
(
roll3.rollStruct = this
)
local rollStruct
fn printCBs =
(
print rollStruct.rollout1.cb1.state
print rollStruct.rollout2.cb2.state
print rollStruct.rollout3.cb3.state
)
button b3 "Button 3"
checkBox cb3
on b3 pressed do printCBs()
)
r
),
fn createUI =
(
rollout1 = createRollout1()
rollout2 = createRollout2()
rollout3 = createRollout3()
rollFloat = newRolloutFloater "Test Float" 100 250
addRollout rollout1 rollFloat
addRollout rollout2 rollFloat
addRollout rollout3 rollFloat
),
start = createUI()
)
global rollTest = rollouts()
)
i’ve showed another techniques… for example : http://forums.cgsociety.org/showpost.php?p=6640830&postcount=7
These examples are great – thanks for the help! I will see if I can do this on my scripts as well, it is a lot cleaner than what I have been doing.
This was a lot harder to get my head around than I thought. It’s radically different from what I have at the moment, and a huge job to try and change. My current version does not work at all. Dan, your sample is the easiest to imitate, but there are a couple of things I don’t understand:
What does this line do? :
roll1.rollStruct = this
I don’t see the “this” keyword anywhere else, so what does it change?
And the same with this line:
local rollStruct
It’s a variable declaration, I reckon. But I don’t see this variable given any content anywhere! Still it’s used to hold the rollouts, how did that happen?
And also, I see the function “printCBs” being declared within each rollout but with essentially the same content. Seems like redundant code? Could this be more efficient?
My floater changes size if rollouts are closed and opened, but I can’t get a reference to the floater itself inside any of the rollout functions. Would this be rollStruct? I would assume that to be rollFloat, but I reckon that’s local to the createUI() function?
Check the Maxscript help for ‘This’ Local Variable in Scripted Controllers. In this case I believe that ‘this’ is a wrapper for the rollout (where [x] = the rollout number), which in turn is assigned to roll[x].rollStruct. So it should be something like this:
r = rollout roll[x] “Rollout [x]”
roll[x].rollStruct = r
rollStruct = roll[x].rollStruct
Result: rollStruct == r, in the context of r
So in in reality r, roll[x].rollStruct, and rollStruct all refer to the rollout created on that function, but allow the rollout to self reference.
Or I could be completely wrong. Dan or Denis am I close to understanding this correctly?
-Eric
This is used within scripted plugins to refer to the current instance so you are correct, but so far as I know, it doesn’t work within a plain rollout, only one created within the context of for example a modifier or material.