How can I make exported variables in my script have collapsible sections in the Inspector?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jerryjrowe

For instance, when you have a Node2D selected in the Scene, the Inspector has two collapsible sections: Transform, and Ordering. Several variables are hidden until you click those words, and then you can see all of that section’s variables.

It would be very convenient if I could organize my own script’s variables in this same kind of way. How would I do it?

:bust_in_silhouette: Reply From: jgodfrey

Sounds like you’re after the Grouping Properties mechanism. It’s documented in the Grouping Properties section here:

Below is an example from the docs, slightly modified to place a few properties in the same group. In addition to the code snippet, you’ll need to make the script a tool script (add the tool keyword to the top of the code) so the below method will be called in the editor…

func _get_property_list():
	var properties = []
	properties.append({
		name = "Rotate",
		type = TYPE_NIL,
		hint_string = "rotate_",
		usage = PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SCRIPT_VARIABLE
	})

	# Example of adding to the group
	properties.append({
		name = "rotate_angle",
		type = TYPE_REAL
	})

	properties.append({
		name = "rotate_speed",
		type = TYPE_REAL
	})

	# This property won't get added to the group
	# due to not having the "rotate_" prefix.
	properties.append({
		name = "trail_color",
		type = TYPE_COLOR
	})
	return properties

Edit:
This looked like what I was after. When I do this code (with minor variations, because I have to for 4.0), the stuff shows up in the inspector…but it appears to not be edit-able?

I am not sure why.

jerryjrowe | 2022-09-22 16:21

:bust_in_silhouette: Reply From: jerryjrowe

The thing @jgodfrey said probably worked in 3.x but does not work in 4.0.
If you try it, you will see stuff in the inspector, but you won’t be able to edit values.

Bright side: There is a much easier way to do this in 4.0beta.
And after finding the name of the thing in the 4.0 beta release page gif, I was able to search the documentation for it, specifically.

For a quick answer to my own question, you can now use this:

@export_group("SomeGroupNameA")
@export var FirstOne : bool = false
@export var SecondOne : bool = false

@export_group("SomeGroupNameB")
@export var ThirdOne : bool = false
@export var FourthOne : bool = false

Leaving it in that order makes the stuff magically work the way it ought in the editor.

I’m not a 4.0 user ATM, but… very nice. ;^)

jgodfrey | 2022-09-22 19:16