Godot Version
godot4.4 dev3
Question
i have some base code abort all entitys,something like:
var item_res:ItemResource
func set_item(item:ItemResource):
item_res=item
and so on.
i don’t care the node is character3D or rigidbody3d or just node3d.the code is not related to node types。
i need to call set_item after the node is instantiate() like:
var p=node_path.instantiate()
p.set_item(item)
add_child(p)
but because the entitys are diffrent node kinds,so i can’t inherit the shared code.
i think i need something likethis:
interface_name BaseEntity[Node3D]
var item_res:ItemResource
func set_item(item:ItemResource):
item_res=item
and the code in the entity like:
class_name Build_Entity:
extends Character3D
extendsinterface BaseEntity
#some codes abort build
In my approach, as long as subsequent users belong to interface_name BaseEntity[Node3D], any class that inherits from Node3D—such as Character3D, RigidBody3D, or any other subclass—can merge and share the code of this interface. There should be no logical issues at all.
i don’t want to write the base code in the sub node(like components),because this increases the complexity of external calls
what’s the best way to do this?anyone can help me?
Why wouldn’t groups work? During creation or in the editor, add the nodes to a group that you can use call_group()
to call a function in all the nodes.
What I mean is that although most of my functions have been componentized by sub nodes,but there are still some fundamental, general-purpose code that needs to be shared。
All of my entity data is defined in resources (and I might use tables later). So, when initializing an entity, I also need a shared interface to pass the data in, allowing the entity to load based on the table data.
The entity could be a tree, a monster, an NPC, etc., each with different modular components such as damage, AI, interaction, etc. The functional code is fully decoupled, but the initialization-related code is shared. However, since their base types are different — like CharacterBody, StaticBody, RigidBody, etc. — the challenge is how to handle this shared code efficiently.
Using call_group here may not be elegant. It’s like in the previous example for instance generation:
var p = node_path.instantiate()
p.set_item(item)
add_child(p)
These are some initialization steps that need to be performed for each instance (there might be more, this is just an example), and it doesn’t seem suitable for call_group
Can’t you use static method?
class_name Utils
func static initialize(node_path:NodePath):
pass
Could you have a singleton for a controller? Maybe have 1 function that takes in a resource and outputs an object that all scenes can call to get the objects they need.
Could you not have 1 method name that does the initializing for a scene, but all the scenes have the same method name so that you can just call the method for every scene and have it work?
Are you trying to take in data from a resource and build the scene from the data in the resource?
What exactly are you trying to do with the data, and how is it stored?
What is the code flow that you want, starting from the resource object and ending with the final object fully loaded?
I feel the previous responses missed the point. I’ve researched extensively and found that many others have noticed this issue. For example, there are several proposals on GitHub discussing this, such as:
Proposal #4872: Suggests adding a more explicit interface system to GDScript.
Proposal #758: Discusses implementing a trait or mixin system for better code reuse.
I want to know the official stance on this crucial scripting syntax feature. Is there any plan to integrate this into GDScript? This is extremely important.