Notifications
Clear all

[Closed] closing floaters?

When I write macroscripts with dialogs, I use the following code at the start of the script to make sure that any old versions of the dialog are closed before the new one opens. As you do.

try (destroydialog mydialog) catch()

When using a floater I know you can use

if MyFloater.open == true then
   	(
   		closerolloutfloater MyFloater
   	)	

But if I try to use the second bit of code in a macroscript, it complains that MyFloater is undefined. But If I don’t try to close the floater and then try to open another one, the second one is empty.

What’s the prefered method of closing floaters that are in macroscripts? I need to use floaters cause I want to use multiple rollouts.

Cheers,
Cg

3 Replies

In both cases (dialogs and floaters), make sure the variable used to store the dialog or floater is declared as global IN THE BEGINNING of the MacroScript. This will make it visible to any other script’s code and to the MacroScript if executed later. Because of this though, you should make sure the variable is UNIQUE in a way that nobody on Earth would ever come up with the same name for his floater. A good practice is to use your own initials and the exact name of the functionality of the tool, for example “Cg_CreateStrangeGeometryOutOfNothing_Floater” or something creative like that. This will ensure that your code will never collide with any other scripts you or others might write in the future.

Here is an example:

macroScript CloseFloater category:"Forum Help"
 (
 	global theFloaterUniqueName
 	rollout test "Test"
 	(
 		label lbl_test "Did I mention this was a test?"
 	)
 	try(closeRolloutFloater theFloaterUniqueName)catch()
 	theFloaterUniqueName = newRolloutFloater "Test Floater" 200 60 
 	addRollout test theFloaterUniqueName
 )

This is one of the RARE cases where the use of a global variable is encouraged. It also allows you to access any controls inside rollouts of that floater from the command line or other scripts, for example to create tools that set values and press buttons in other tools.

In the case of a dialog, the location of the closing code should be RIGHT AFTER the global definition, because the rollout is both the holder of the controls AND the dialog itself. If you would reevaluate the script and then try to destroy the dialog after the redefinition of the rollout, it would access a different value in memory and would fail to destroy the previous dialog. If you don’t reevaluate the code and just press the MacroScript icon without closing the dialog, nothing bad will happen because the existing rollout is already open and creating a new dialog does nothing. So the destroyDialog() is there mainly to avoid leaving duplicates during development when you re-run the source code very often, creating new instances of the dialog’s rollout.

macroScript CloseDialog category:"Forum Help"
 (
 	global theDialogUniqueName
 	try(destroyDialog theDialogUniqueName)catch()
 	rollout theDialogUniqueName "Test"
 	(
 		label lbl_test "Did I mention this was a test?"
 	)
 	createDialog theDialogUniqueName 200 50
 )

If a floater gets docked to something I don’t think you can just “destroy dialog”. Don’t you have to unregister it first too?

Once again, Bobo to the rescue. I honestly don’t know how you get the time. If I ever bump into you, the beers are on me. Promise.

Thanks mate,

Cg.