Script Categories help.

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

Hello!

I’ve been reading in the Godot Docs about exporting variables
https://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/gdscript_exports.html

And noticed at the end of the page a section concerning adding Categories to the Script (as well as grouping exported variables (properties) together) and they give an example of it. I want to try out making my own category but even with the example, I don’t know how to make the category appear or put exported variables/properties into the category. I find it weird that to make a category (or to group properties together) you need to use a function. How do you call the function to make a visible change to the inspector? How do you put variables into the section? I would be grateful for any help.

Here’s the example on the Godot Docs

func _get_property_list():
    var properties = []
    properties.append(
        {
            name = "Debug",
            type = TYPE_NIL,
            usage = PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_SCRIPT_VARIABLE
        }
    )
    return properties

In order to make it visible in the Inspector it must belong to an object in the scene. So the above func _get_property_list(): must be attached via script to an object in the scene.

tool extends MeshInstance

func _get_property_list():
	print_debug("_get_property_list")
	return [
		{
			name = "MyProps/Debug",
			type = TYPE_BOOL,
			usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE
		},
		{
			name = "MyProps/Debug2",
			type = TYPE_BOOL,
			usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE
		}
	]

Of course you also should override _get and _set to handle the property path correctly and read from and assign to variables you want.

5pectre7 | 2021-11-21 21:41

:bust_in_silhouette: Reply From: whiteshampoo

Here you go. With some examples:

extends Node
tool

var first : int = 1
var second : int = 2
var ingroup_more : int = 3
var ingroup_numbers : int = 4
var half : float = 0.5
var quarter : float = 0.25

func _get_property_list() -> Array:
	return [{
			name = "Integers",
			type = TYPE_NIL,
			usage = PROPERTY_USAGE_CATEGORY 
		},{
			name = "first",
			type = TYPE_INT,
			usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE
		},{
			name = "second",
			type = TYPE_INT,
			usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE
		},{
			name = "Group",
			type = TYPE_NIL,
			hint_string = "ingroup_",
			usage = PROPERTY_USAGE_GROUP
		},{
			name = "ingroup_more",
			type = TYPE_INT,
			usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE
		},{
			name = "ingroup_numbers",
			type = TYPE_INT,
			usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE
		},{
			name = "Floats",
			type = TYPE_NIL,
			usage = PROPERTY_USAGE_CATEGORY
		},{
			name = "half",
			type = TYPE_REAL,
			hint = PROPERTY_HINT_RANGE,
			hint_string = "0.4, 0.6",
			usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE
		},{
			name = "quarter",
			type = TYPE_REAL,
			hint = PROPERTY_HINT_EXP_EASING,
			usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE
		}]