godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.48k stars 21.26k forks source link

Group related functions doesn't work well with tool enabled #35743

Open nonunknown opened 4 years ago

nonunknown commented 4 years ago

Godot version: 3.2 OS/device including version: ubuntu 19.10 Issue description: I'm creating a plugin related to group managing, but when use a function like node.add_to_group(groupname) the groups built'in tab doesnt recognize this (sometimes it works, but if you first add a number string:

groups

Steps to reproduce:

Create a plugin Create a way to add_groups in-editor

ImprovedGroups.gd

tool
extends EditorPlugin
class_name ImprovedGroups

var dock

func _enter_tree():
    # Initialization of the plugin goes here
    # Load the dock scene and instance it
    dock = load("res://addons/improved_groups/Groups.tscn").instance()

    # Add the loaded scene to the docks
    add_control_to_dock(DOCK_SLOT_RIGHT_UR, dock)
    dock.configure(self)

func _exit_tree():
    # Clean-up of the plugin goes here
    # Remove the dock
    remove_control_from_docks(dock)
     # Erase the control from the memory
    dock.free()

func get_selected_node()-> Node:
    return get_editor_interface().get_selection().get_selected_nodes()[0]

ManagerGroups.gd (the dock scene root script)

tool
extends Control
class_name ManagerGroups

export var path_group_name:NodePath
export var path_apply_children:NodePath
export var path_add:NodePath

var le_group_name:LineEdit
var cb_apply_children:CheckBox
var bt_add:Button

var improved_groups:ImprovedGroups = null

func configure(caller:ImprovedGroups):
    le_group_name = get_node(path_group_name)
    cb_apply_children = get_node(path_apply_children)
    bt_add = get_node(path_add)

    improved_groups = caller
    print("configured")
    pass # Replace with function body.

func add_group():
    var selected_node = improved_groups.get_selected_node()
    var add_to_children = cb_apply_children.pressed
    var group_name = le_group_name.text
    if group_name == "":
        printerr("Group name cant be empty")
        return
    if selected_node == null:
        printerr("Select a node first")
        return

    if (selected_node.is_in_group(group_name)):
        printerr("Node "+selected_node.name+" already in this group")
        return
    else:
        selected_node.add_to_group(group_name,true)
        prints("Added Group: ",group_name," to node: ",selected_node.name)

    le_group_name.text = ""
    pass

func _on_bt_add_pressed():
    add_group()
    pass # Replace with function body.

** Update

checked godot's source, possibily could be cuz of this line:

groups_editor.cpp line 195

void GroupDialog::_add_group(String p_name) {
    if (!is_visible()) {
        return; // No need to edit the dialog if it's not being used.
    }
timothyqiu commented 4 years ago

Currently, the update of group editor and scene tree editor are manually trigged.

https://github.com/godotengine/godot/blob/3c3ed67c3944a555d18fa8b68603f3a68416e27e/editor/groups_editor.cpp#L591-L598

I think maybe a "group_updated" signal should be added to Node. So that the group editor and scene tree editor can update themselves when the signal is emitted.

nonunknown commented 4 years ago

I was thinking about a workaround for now, maybe if we could go to groups tab via script , the code listed on top of this post would work too. Btw I found a bug when you call get_selected_node() and try to add a group to its children i.e "test", the code runs fine, but if you go to anyone of this children you cant add a group called "test" again, but if you run the game and list this child's groups its shows empty.

KoBeWi commented 3 years ago

Can anyone still reproduce this bug in Godot 3.2.3 or any later release?

If yes, please ensure that an up-to-date Minimal Reproduction Project (MRP) is included in this report (a MRP is a zipped Godot project with the minimal elements necessary to reliably trigger the bug). You can upload ZIP files in an issue comment with a drag and drop.

donn-xx commented 1 year ago

Not 100% sure if this is the right bug report, but in Godot 4.0.2 (using @tool) I am seeing that somenode.add_to_group("foo") does not show that group in the groups inspector. It does seem to work overall, but it's not saved as so.

KoBeWi commented 1 year ago

The issue seems still valid in 4.1. Here's a simpler test code:

@tool
extends Node

func _ready() -> void:
    await get_tree().create_timer(5).timeout
    print("gr")
    add_to_group("gr", true)

Open the scene, click the node and go to Group tab. The group is added, but you need to manually refresh the group list and scene tree. I think we just need to expose some way to update these nodes from code 🤔

@donn-xx add_to_group() has a second argument that makes the group persistent. It's false by default.