How do I create an object through code?

Godot Version

4.2.2

Question

I’m making a mechanic for transitioning between rooms. And one of its last stages is that a special function creates an object of the game character at the coordinates specified by the trigger from the previous room. This is not difficult to do. But the problem is that I don’t know how to make a function create a certain object in the tree of the scene in whose script it was launched. And regardless of what kind of scene it is.

If I understand your question correctly, you want to get a node regardless of the scene it is in, and add a child node to it.

I would recommend using groups for this. Set a group to the node you want to get, then to get it in your code

@onready var someNode = get_tree().get_first_node_in_group("nameOfYourGroup")

If you were asking about how to add a child, you will need to instantiate a scene

I didn’t quite understand your answer (or rather, the code). I need my function to add a specific object (in my case, a character) to the tree of the scene in which the function was run. For example, we have the scene “Room1”. In the script for this scene, our function was involved (let it be called “spawn_player”). “spawn_player” takes a character object from the project file system and adds it to the scene tree, to the coordinates specified in the “target” (Vector2) variable. I’ve been racking my brains for 2 hours trying to figure out how to do it. (sorry if there are any mistakes in the text, I’m not a native English speaker and I use Google Translate)

1 Like

where is the function called from? can you show some example code? It’s probably easily doable if I understand what you are trying to do. And your English is flawless, way better than most natives

The function is called from the global file “change_scene”. The “target” variable is also there. And the value is written to this variable in the code of the trigger that starts the transition between rooms. By the way, my English is so literate only because not a single word of mine has been translated by me personally. I always have a tab open with Google Translate. (if I wrote myself, you would see a clear example of the A1 level)

Ok, the function is declared in the global autoload change_scenecalled? I mean, what invokes it? Some object in your level? The player? Or is it called on _ready in a scene?

Also, why do you add the player via script in the desired position instead of just having it inside the scene in the first place, and then you only would change the scene and the player would already be there? I assume you have some good reason to do this.

(In the meantime I’m working on some code examples)

I believe I have a solution for you.

I created an Autoload script called change_scene and here are its contents:

extends Node

@onready var player = preload("res://player.tscn")

func spawnPlayer(position:Vector2, scene = self):
	var playerInstance = player.instantiate() # creates an instance of the player, obviously
	playerInstance.global_position = position # sets the position before the player is visible on screen
	scene.get_tree().root.add_child.call_deferred(playerInstance) # adds the player to the scene
	
	# this is not sorcery - bear with me
	# `scene` by default is self, which is the node that calls the function
	# then I just get the scene tree of this node using `get_tree()`
	# then, I get its root node
	# then a child is added, using call_deferred. Simple add_child didn't work in 
	# this case, so I had to call it once the node has set up its children already
	# that's it 

This does what you described, the player scene is assigned into a variable

Then a function to spawn the player. Accepts the position as Vector2 and the default scene it will add the player to will be the scene that calls the function, which is self

Then in the scene you want to spawn the player in, you can just use

func _ready() -> void:
	ChangeScene.spawnPlayer(Vector2(500, 500))

Is this what you were looking for? I’m happy to clarify if you need anything. Can send you the little project I made to test this out

I tried to make the movement of a player prepared in advance on the stage. But for some reason, it stopped working. It just ended up in the wall (no matter what coordinates I specified). And being in the wall, it obviously cannot move.

Additionally:
I sincerely thank you for your solution. I lost the Internet for the whole day, and during this time I managed to design the necessary code on my own. However, thank you very much for wanting to help. I really appreciate it! Have a good day.

1 Like