@tool extends Sprite2D class_name PlantPartBuilder @export var part_name: String @export var part_index: int @export_tool_button("Load resource") var load_resource_button = load_resource @export var type: PlantPart.PartType @export var root: PlantAttachBuilder @export var attaches: Node @export_tool_button("Save as resource") var save_as_resource_button = save_as_resource func load_resource(): var destination := "res://entities/plants/resources/plant_parts/" + part_name + ".tres" print("Loading: ", part_name, " n°: ", part_index, " at: ", destination) var part_group: PartGroup = ResourceLoader.load(destination) as PartGroup if part_group.parts.size() <= part_index: printerr("No part at ", part_index) return var plant_part := part_group.parts[part_index] if plant_part: texture = plant_part.texture type = plant_part.type root.position = plant_part.root.position root.attach_types = plant_part.root.attach_types var attaches_children := attaches.get_children() for i in maxi(attaches_children.size(), plant_part.attaches.size()): if i < attaches_children.size() && i < plant_part.attaches.size(): attaches_children[i].position = plant_part.attaches[i].position attaches_children[i].attach_types = plant_part.attaches[i].attach_types elif i >= attaches_children.size(): var new_child = PlantAttachBuilder.new() new_child.name = "attach" + str(i) new_child.position = plant_part.attaches[i].position new_child.attach_types = plant_part.attaches[i].attach_types attaches.add_child(new_child) new_child.set_owner(self) elif i >= plant_part.attaches.size(): attaches_children[i].free() else: printerr("Invalid code path") func save_as_resource(): var destination := "res://entities/plants/resources/plant_parts/" + part_name + ".tres" print("Saving: ", part_name, " n°: ", part_index, " at: ", destination) var part_group: PartGroup = ResourceLoader.load(destination) as PartGroup if not part_group: part_group = PartGroup.new() if part_group.parts.size() <= part_index: part_group.parts.resize(part_index + 1) var attaches_vec2: Array[PlantAttach] for attach in attaches.get_children(): attaches_vec2.append(PlantAttachBuilder.to_plant_attach(attach)) var plantPart := PlantPart.new() plantPart.init(texture, type, PlantAttachBuilder.to_plant_attach(root), attaches_vec2) plantPart.resource_name = part_name part_group.parts[part_index] = plantPart.duplicate_deep() var err := ResourceSaver.save(part_group, destination, ResourceSaver.FLAG_REPLACE_SUBRESOURCE_PATHS) if err != OK: printerr("Error saving resource: ", error_string(err)) else: part_group.take_over_path(destination)