+1 vote

I want to add a scene as an node to another scene in code like adding a player scene to an level scene in code

Godot version 3.2.2
in Engine by (254 points)

1 Answer

+2 votes
Best answer

First you need to understand that everything is a node.
The "Scene" itself is a node. The "Player", too.

So what you are looking for is to add a node to an other node as a child.
So more like spawning.

var myNode = preload("Drag and Drop type your path here")

func add_myNode _to_my_game(): 
     var myNode _instance =  myNode .instance() 
     get_tree().get_root().add_child(myNode_instance)

     #set position if needed
     myNode_instance.global_transform = global_transform

Thats how you can add a node to the "furthest up" node via script for example.

by (71 points)
selected by

Thanks for answering so quick, it should work.

How can i set the position ?

myNode_instance.translation = Vector3(0, 0, 0)

I think that should work

Thank you

Bot7

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.