You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hints that a [Callable] property should be displayed as a clickable button. When the button is pressed, the callable is called. The hint string specifies the button text and optionally an icon from the [code]"EditorIcons"[/code] theme type.
2938
+
[codeblock lang=text]
2939
+
"Click me!" - A button with the text "Click me!" and the default "Callable" icon.
2940
+
"Click me!,ColorRect" - A button with the text "Click me!" and the "ColorRect" icon.
2941
+
[/codeblock]
2942
+
[b]Note:[/b] A [Callable] cannot be properly serialized and stored in a file, so it is recommended to use [constant PROPERTY_USAGE_EDITOR] instead of [constant PROPERTY_USAGE_DEFAULT].
ERR_FAIL_NULL_MSG(object, vformat(R"(Failed to get property "%s" on a previously freed instance.)", p_property));
42
+
43
+
const Variant value = object->get(p_property);
44
+
ERR_FAIL_COND_MSG(value.get_type() != Variant::CALLABLE, vformat(R"(The value of property "%s" is %s, but Callable was expected.)", p_property, Variant::get_type_name(value.get_type())));
45
+
46
+
const Callable callable = value;
47
+
ERR_FAIL_COND_MSG(!callable.is_valid(), vformat(R"(Tool button action "%s" is an invalid callable.)", callable));
Copy file name to clipboardExpand all lines: modules/gdscript/doc_classes/@GDScript.xml
+35Lines changed: 35 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -669,6 +669,41 @@
669
669
[b]Note:[/b] Subgroups cannot be nested, they only provide one extra level of depth. Just like the next group ends the previous group, so do the subsequent subgroups.
Export a [Callable] property as a clickable button with the label [param text]. When the button is pressed, the callable is called.
678
+
If [param icon] is specified, it is used to fetch an icon for the button via [method Control.get_theme_icon], from the [code]"EditorIcons"[/code] theme type. If [param icon] is omitted, the default [code]"Callable"[/code] icon is used instead.
679
+
Consider using the [EditorUndoRedoManager] to allow the action to be reverted safely.
680
+
See also [constant PROPERTY_HINT_TOOL_BUTTON].
681
+
[codeblock]
682
+
@tool
683
+
extends Sprite2D
684
+
685
+
@export_tool_button("Hello") var hello_action = hello
686
+
@export_tool_button("Randomize the color!", "ColorRect")
687
+
var randomize_color_action = randomize_color
688
+
689
+
func hello():
690
+
print("Hello world!")
691
+
692
+
func randomize_color():
693
+
var undo_redo = EditorInterface.get_editor_undo_redo()
[b]Note:[/b] The property is exported without the [constant PROPERTY_USAGE_STORAGE] flag because a [Callable] cannot be properly serialized and stored in a file.
700
+
[b]Note:[/b] In an exported project neither [EditorInterface] nor [EditorUndoRedoManager] exist, which may cause some scripts to break. To prevent this, you can use [method Engine.get_singleton] and omit the static type from the variable declaration:
701
+
[codeblock]
702
+
var undo_redo = Engine.get_singleton(&"EditorInterface").get_editor_undo_redo()
703
+
[/codeblock]
704
+
[b]Note:[/b] Avoid storing lambda callables in member variables of [RefCounted]-based classes (e.g. resources), as this can lead to memory leaks. Use only method callables and optionally [method Callable.bind] or [method Callable.unbind].
if (!variable_type.is_variant() && variable_type.is_hard_type()) {
4695
+
if (variable_type.kind != DataType::BUILTIN || variable_type.builtin_type != Variant::CALLABLE) {
4696
+
push_error(vformat(R"("@export_tool_button" annotation requires a variable of type "Callable", but type "%s" was given instead.)", variable_type.to_string()), p_annotation);
4697
+
returnfalse;
4698
+
}
4699
+
}
4700
+
4701
+
variable->exported = true;
4702
+
4703
+
// Build the hint string (format: `<text>[,<icon>]`).
0 commit comments