godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

Add methods to access the parent of the tree and add a child immediatelly #349

Open Mautar55 opened 4 years ago

Mautar55 commented 4 years ago

Describe the project you are working on: A bullet hell filled with objects beign create and destroyed with controlled transformations.

Describe the problem or limitation you are having in your project: On every project i work on, i instantiate some elements directly at the root of the scene as children nodes, this is a standard feature of almost every game. The problem comes (and came) when i had to teach how to use nodes to reach the root and add children to them. Many find weird the node tree at the beggining and feel odd to need to reach the parent and transform it to the corresponding position.

var instance instance = preloaded_resource.instance() instancia.transform = transform get_tree().get_root().add_child(instance)

Describe how this feature / enhancement will help you overcome this problem or limitation: New functions for instancian, adding and tranforming will make the code cleaner and easy to understand. Specially for newcomers.

Show a mock up screenshots/video or a flow diagram explaining how your proposal will work: child_to_root

Describe implementation detail for your proposal (in code), if possible: I propose add 4 new standard functions to keep the code cleaner and understandable:

func child_to_root(instance):
    get_tree().get_root().add_child(instance)

func child_local_to_root(instance):
    instance.transform = get_global_transform()
    get_tree().get_root().add_child(instance)

func instance_to_root(resource):
    get_tree().get_root().add_child(preloaded_resource.instance(resouce))

func instance_local_to_root(resource):
    var instance = resource.instance()
    instance.transform = get_global_transform()
    get_tree().get_root().add_child(instance)

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

Is there a reason why this should be core and not an add-on in the asset library?: It has use cases to most of the projects.

Calinou commented 4 years ago

You can use get_node("/root") or $"/root" as a shorthand for get_tree().get_root().

Xrayez commented 4 years ago

This one is tightly related to #260 proposal.

Mautar55 commented 4 years ago

This one is tightly related to #260 proposal.

True. That one is even a lot better. Should I delete this one?

Xrayez commented 4 years ago

This proposal talks about being able to simplify root node instancing specifically so I'd keep this open for discussion in my opinion.


Meta: I previously talked about how different proposals can be seen as different use cases supporting the same idea, this is one of these instances (no pun intended!).