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

Add SceneTree signals for adding/removing a Node to/from a group #1259

Open
willnationsdev opened this issue Jul 25, 2020 · 6 comments
Open

Comments

@willnationsdev
Copy link
Contributor

willnationsdev commented Jul 25, 2020

Describe the project you are working on:
A plugin for an ECS-like usage of nodes.

Describe the problem or limitation you are having in your project:
I use nodes and groups as a sort of state machine for system-wide process callbacks, very similar to how the Viewport node works to execute input callbacks, only, instead of each node implementing an input callback, a System node executes an operation over every node in the collection.

This works great for iterative operations like process, but not great for in-place state-change operations, e.g. going from state A to state B should trigger some logic (represented as removing group A and adding group B). I could code this logic in the individual node's script, but then I'd be sharing logic across several scripts.

Describe the feature / enhancement and how it helps to overcome the problem or limitation:

What I would prefer is if I could have a single callback that handles every node that is undergoing a group state change. Since groups are actually maintained by the SceneTree anyway, it makes sense that the SceneTree should have a signal for handling an update to its group_map data. Then, I could just connect to that signal and handle every node's mutation logic in a single place.

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:

  1. Add group_changed signal to SceneTree with a reference to the node that was added or removed to/from the group and a string for which group was updated.
  2. Emit the signal in SceneTree::add_to_group() and SceneTree::remove_from_group().
  3. Add signal to SceneTree's _bind_methods() function.

If this enhancement will not be used often, can it be worked around with a few lines of script?:

It can be worked around, but not in an architecturally clean way. At least not without traits (though that wouldn't be optimal either since it would only work with GDScript and not other languages).

Is there a reason why this should be core and not an add-on in the asset library?:
Can't be fixed with a plugin or asset.

@TheDuriel
Copy link

I don't quite agree with why you want this. But w/e.

Care must be taken that the signal is only emitted when the User changes groups. As hidden groups are heavily used within the editor, and some specialized nodes. And you could quickly be spammed by hundreds if not thousands of calls.

@Xrayez
Copy link
Contributor

Xrayez commented Jul 25, 2020

It can be worked around, but not in an architecturally clean way. At least not without traits (though that wouldn't be optimal either since it would only work with GDScript and not other languages).

Interestingly, I use "component" nodes which describe the "entity" traits (root node). And those components can be added to group(s). Retrieving all objects who share the same trait is a matter of get_nodes_in_group call, and then you just fetch the owner to get the root node. One could in theory remove and add those traits on demand, and this is where signals could be used then, if I understand correctly.

@willnationsdev
Copy link
Contributor Author

@Xrayez

One could in theory remove and add those traits on demand, and this is where signals could be used then, if I understand correctly.

Yes, you could, in theory, use the adding and removing of groups to trigger the adding and removing of component nodes. Though, I wouldn't necessarily recommend that since that's pretty magical and there wouldn't be a way to pass initialization logic alongside the state change. I'm just wanting to trigger function executions for on_add and on_remove that have to do with the system itself and not the actual components (like updating db indexes of often-used component combinations for optimization purposes, etc.).

@willnationsdev
Copy link
Contributor Author

@TheDuriel

Perhaps this would have to wait until we get an project-wide groups configuration system up and running? That way the callback would only execute for the user-defined groups configured there? Either that, or switch to a naming convention thing where groups with an underscore prefix are not added to the callbacks (that sounds much simpler and easier to work with).

@me2beats
Copy link

I have a case when I would like to know that a node of some group is created or added to the tree, but I don't want to set a script on the node.
So node_added_to_group(group:String,node:Node) signal would be useful.

@Calinou Calinou changed the title Add SceneTree signals for adding/removing a Node to/from a group. Add SceneTree signals for adding/removing a Node to/from a group Jan 9, 2024
@aXu-AP
Copy link

aXu-AP commented Sep 14, 2024

In addition to having this in SceneTree, I think Node should also have these signals: group_entered(group), group_exited(group).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants