–1 vote
in Engine by (75 points)
reshown by

1 Answer

+3 votes
Best answer

When you change a node type from the editor using Change Type, what the editor node is actually deleting your node, creating a new one, and somehow assign some of the properties they have in common.

This operation is not a light one, and information may be lost when you do this, so I'd advise you to consider alternatives before doing this at runtime in your game.

I'm not aware of a function in GDScript doing this, but the whole thing can be done with a few lines of code:

# Replacing a Sprite with an AnimatedSprite.
# I assume you have the `parent` node in a variable.
# -----------------

# Get the old node
var old_node = parent.get_node("TheSprite")

# Remove it from the tree

# Destroy it (not immediately, some code might still use it)

# Create new node
var new_node = AnimatedSprite.new()

# Add the new node to the tree, under the same parent

# Move it to same order within children
parent.move_child(new_node, old_node.index)

# Set properties you want to be the same
new_node.name = old_node.name
new_node.position = old_node.position
new_node.rotation = old_node.rotation
new_node.scale = old_node.scale
# Etc...

There are variants of this, so you may want to change this code to suits your needs.

by (29,120 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.