+3 votes

I want to export a button that can be clicked in the inspector without having to create an editor plugin. For instance, a "Generate Points" button that will fill an array with random points.

Godot version 3.2.3
in Engine by (11,901 points)

1 Answer

+3 votes
Best answer

Godot currently doesn't expose a way to expose a clickable action within the
Inspector. While you could create an editor plugin, you can also expose a
boolean property with a setter that performs the desired action. The setter
should be made to keep the variable's original value at all times, which means
the checkbox will always remain unchecked. The user will still be able to press
it to trigger the desired action.

The script must be in tool mode for this to work.
Otherwise, the setter won't be run while in the editor.
See Running code in the editor for more information.

This can be useful to create editor tooling when you're in a hurry:

# Notice the setter method.
export var bake_shadows = false setget do_bake_shadows

# In GDScript, setters always need to accept at least one parameter.
# To make it easier to call this method from code, you can make the parameter optional
# by giving it a default value.
func do_bake_shadows(_p_bake_shadows = null):
    print("Do stuff here...")

    # Notice how we never set the variable of `bake_shadows` in this method.
    # Therefore, `bake_shadows` will always remain set to `false` when changed externally.
    # Since exported variables will trigger the setter when modifying the value
    # from the inspector, this `do_bake_shadows()` method will be called
    # whenever the user tries to enable the "Bake Shadows" property.
by (11,901 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.