0 votes

In the Godot editor, changing a Node's type is as easy as right-clicking on it and changing it's type. If it's script extends from the class that is the type of the original node, you need to change it, but if it inherits from a class that both types inherit from, you don't need to change the script's extend constant.

So: assuming I've already created a script that is compatible with two different types of nodes, is there a way to switch between those node types at runtime? For example: creating a VBoxContainer that changes itself into an HBoxContainer and back again? As long as said VBoxContainer's script inherits from BoxContainer, it seems like there should be a way.

I couldn't find a changetype() or changeclass() function so I've been fiddling with replaceby() and getclass() and so far I've been able to get either nothing to happen or crashing Godot.

Godot version v3.3.3.stable.official [b973f997f]
in Engine by (15 points)

2 Answers

+1 vote
Best answer

Node type in runtime: no.

Editor is just creates new node, copies applicable properties and then deletes old one.

If you want to operate on multiple classes that are sharing common base class, just write your code for base class and it will work for inherited types.

func use_container(container : BoxContainer)
    # code compatible with just BoxContainer

use_container($HBoxContainer)
use_container($VBoxContainer)
by (1,438 points)
selected by

Thank you. Potentially, what I actually wanted to do was create a scene that could act as a KinematicBody2D or a Body2D. Something sort of like "ragdolling" or a platformer character that could be launched into the air with physics and then "recover" from the state and go back to normal platforming controls. KinematicBody2D characters can interact with RigidBody2D objects, but you can't just tell a KinematicBody2D "act like a RigidBody2D for two seconds and then act like a KinematicBody again".

Right now I have four different scenes that all use the same script, inherited from CollisionObject2D. The only difference between them is that the root node of each scene is either Area2D or one of the Body2D types. While I used the BoxContainer nodes to test my idea, ultimately I was thinking that maybe I could create a single "Actor2D" that could switch at run-time between the four different nodes that inherit from CollisionObject2D.

Knowing that this specific method is impossible just saved me hours, if not days, of work. But I'm sure I'll figure something else out.

Just to add some clarity: you can still perform these steps (of node re-creation) by yourself, but again it's up to you to take care about all its dependencies, like children, properties, signals, and possible references from outer scope.

Yep, and node re-creation certainly has it's place, but for what I'm trying to do node re-creation is not exactly what I want. What I'm trying to do is more of a state change, and I was just trying to do it incorrectly.

0 votes

The editor internally calls replace_by() to change the node's type, so that should've work fine. Can you post the code you're using to try changing the node's type?

by (50 points)

Here's the code (this is attached to a VBoxContainer with a few ColorRects inside so i can see if it actually changes or not):

tool
extends BoxContainer

export var isHBox := true setget SetBoxOrientation

var HBoxClass = HBoxContainer.new()
var VBoxClass = VBoxContainer.new()

func SetBoxOrientation(orientation):
    if orientation:
        replace_by(HBoxClass.get_class(), true)
    else:
        replace_by(VBoxClass.get_class(), true)

func _process(delta: float) -> void:
    if Input.is_action_just_pressed("ui_up"):
        SetBoxOrientation(!isHBox)

Don't try to actually use that code BTW, unless you're trying to make Godot crash. sash-rc already let me know that it's not going to work in principle.

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 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 webmaster@godotengine.org with your username.