heygleeson / godot-ldtk-importer

LDTK Importer for Godot 4
MIT License
135 stars 13 forks source link

Add Children to Entities in Post Import Script? (more of a question than issue) #11

Closed grebhun closed 8 months ago

grebhun commented 8 months ago

This is more of a question than an issue, so apologies for the out of place issue.

I'm using the Post-Import script for the EntityLayers to replace entity layers with my own custom BoundingBox Scene (based on an Area2D which needs a collision shape child). That is working correctly.

However, I would like to add a CollisionShape2D as a child to that BoundingBox node and have both appear in the final import. Currently, the BoundingBox appears in the final world, but does not have any children.

I believe this is from the owner not being set, but I am not sure how to set the owner of the CollisionShape2D correctly in the post-import script. I thought this would be handled by code that is run later, but it appears not.

Here's my current import script:

@tool

## Entity Post Import Example, showcasing how to handle EntityRefs.

const Util = preload("res://addons/ldtk-importer/src/util/util.gd")
const boxScene = preload("res://scenes/bounding_box.tscn")

func post_import(entity_layer: LDTKEntityLayer) -> LDTKEntityLayer:
    var entities: Array = entity_layer.entities
    for entity in entities:
        # Create entity node (simple example)
        var bounding_box = boxScene.instantiate()
        bounding_box.position = entity.position

                # Create RectangleShape2D from entity shape
        var new_rect: RectangleShape2D = RectangleShape2D.new()
        new_rect.size = entity.size

                # Create CollisionShape2D with shape
        var new_collision_shape: CollisionShape2D = CollisionShape2D.new()
        new_collision_shape.position = entity.position
        new_collision_shape.shape = new_rect

                # Add CollisionShape2D as child of BoundingBox (this is what is not working)
        bounding_box.add_child(new_collision_shape)
        new_collision_shape.owner = bounding_box # This doesn't seem to do anything

        entity_layer.add_child(bounding_box)

        # Update 'iid' to reference this entity node
        Util.update_instance_reference(entity.iid, bounding_box)

        # Add unresolved reference (e.g. EntityRef field)
        if "Entity_ref" in entity.fields:
            var ref = entity.fields.Entity_ref
            if ref != null:
                bounding_box.ref = ref
                print("adding unresolved ref")
                Util.add_unresolved_reference(bounding_box, "ref")

    return entity_layer

Am I missing an obvious addition that makes the owner get set later on in the import process?

grebhun commented 8 months ago

I actually was able to fix this by setting the owner to the parent of the EntityLayer (Level) or the world (parent of Level). I should have known that I had the root node availabe to me through the relationships of the EntityLayer Node itself 😅