Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide instructions for resource change notifications in tools #9283

Merged
merged 29 commits into from
Apr 27, 2024
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
880ee65
providing guidance for being notified about resources changing in tools
betalars Apr 26, 2024
d9110c9
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
e946116
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
db98e9d
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
8e68565
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
51b5e5d
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
819fa1c
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
db1ccc4
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
2fe6cc9
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
4ed8f80
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
ff05309
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
f6eed4d
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
62c2f4e
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
0cb5f3d
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
b45338a
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
44fed48
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
f8d7320
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
e9ee39f
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
b3b64d0
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
0c9d756
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
9361c4a
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
ed7d465
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
5a896b9
suggesting to use changed instead of creating a new signal
betalars Apr 27, 2024
f51aef4
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
0658f71
guidance on disconnecting old resources when new one is set
betalars Apr 27, 2024
f78ba0f
Update tutorials/plugins/running_code_in_the_editor.rst
betalars Apr 27, 2024
5d4b4d4
further formatting enhancements
betalars Apr 27, 2024
1d84250
fixing typo
betalars Apr 27, 2024
5880494
using pre-existing shorthand for emitting signal
betalars Apr 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions tutorials/plugins/running_code_in_the_editor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,174 @@ angle add a setter ``set(new_speed)`` which is executed with the input from the
to run in the editor too. Autoload nodes cannot be accessed in the editor at
all.

Getting notified when resources change
--------------------------------------

Some times you want your tool to use a resource. However, when you change a
property of that resource in the editor, the ``set()`` method of your tool will
not be called.

.. tabs::
.. code-tab:: gdscript GDScript

@tool
class_name MyTool
extends Node

@export var resource: MyResource:
set(new_resource):
resource = new_resource
_on_resource_set()

# This will only be called when you create, delete, or paste a resource.
# You will not get an update when tweaking properties of it.
func _on_resource_set():
print("My resource was set!")

.. code-tab:: csharp

using Godot;

[Tool]
public partial class MyTool : Node
{
private MyResource _resource;

[Export]
public MyResource Resource
{
get => _resource;
set
{
_resource = value;
OnResourceSet();
}
}
}

// This will only be called when you create, delete, or paste a resource.
// You will not get an update when tweaking properties of it.
private void OnResourceSet()
{
GD.Print("My resource was set!");
}

To get around this problem you first have to make your resource a tool and make it
emit the ``changed`` signal whenever a property is set:

.. tabs::
.. code-tab:: gdscript GDScript

# Make Your Resource a tool.
betalars marked this conversation as resolved.
Show resolved Hide resolved
@tool
class_name MyResource
extends Resource

@export var property = 1:
set(new_setting):
property = new_setting
# Emit a signal when the property is changed.
changed.emit()

.. code-tab:: csharp

using Godot;

[Tool]
public partial class MyResource : Resource
{
private float _property = 1;

[Export]
public float Property
{
get => _property;
set
{
_property = value;
// Emit a signal when the property is changed.
EmitChanged();
}
}
}

You then want to connect the signal when a new resource is set:
betalars marked this conversation as resolved.
Show resolved Hide resolved

.. tabs::
.. code-tab:: gdscript GDScript

@tool
class_name MyTool
extends Node

@export var resource: MyResource:
set(new_resource):
resource = new_resource
# Connect the changed signal as soon as a new resource is being added.
resource.changed.connect(_on_resource_changed)

func _on_resource_changed():
print("My resource just changed!")

.. code-tab:: csharp

using Godot;

[Tool]
public partial class MyTool : Node
{
private MyResource _resource;

[Export]
public MyResource Resource
{
get => _resource;
set
{
_resource = value;
// Connect the changed signal as soon as a new resource is being added.
_resource.Changed += OnResourceChanged;
}
}
}

private void OnResourceChanged()
{
GD.Print("My resource just changed!");
}

betalars marked this conversation as resolved.
Show resolved Hide resolved
Lastly, you should to disconnect the signal as the old resource being used and changed somewhere else
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Lastly, you should to disconnect the signal as the old resource being used and changed somewhere else
Lastly, remember to disconnect the signal as the old resource being used and changed somewhere else

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay I am afraid this is going to need a new PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you can't update something already merged, but no worries, missed it too

would cause unneeded updates.
betalars marked this conversation as resolved.
Show resolved Hide resolved

.. tabs::
.. code-tab:: gdscript GDScript

@export var resource: MyResource:
set(new_resource):
# Disconnect the signal if the previous resource was not null.
if resource != null:
resource.changed.disconnect(_on_resource_changed)
resource = new_resource
resource.changed.connect(_on_resource_changed)

.. code-tab:: csharp

[Export]
public MyResource Resource
{
get => _resource;
set
{
// Disconnect the signal if the previous resource was not null.
if (_resource != null)
{
_resource.Changed -= OnResourceChanged;
}
_resource = value;
_resource.Changed += OnResourceChanged;
}
}

Reporting node configuration warnings
-------------------------------------

Expand Down