Adding simple Scene on Function

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rayu

I have my Function

func spawn_Player1(num):

and i want to add a scene whenever this function is called.
the Scene is a simple Sprite nothing special.

When the scene is loaded and instantiated, attach it to the scene tree with add_tree().

Ertain | 2020-11-17 09:49

like this ?

func spawn_Player1(num):

what does “The method “add_tree” isn’t declared in the current class” mean? how do i declare that

Rayu | 2020-11-17 10:01

Whoops, sorry, it’s add_child() (I was pretty sleepy at the time). Andy Campbell has the correct answer.

Ertain | 2020-11-17 18:06

:bust_in_silhouette: Reply From: AndyCampbell

There are some great tutorials about this kind of thing. I recommend you check out some of those to get a structure to start your game.

That might have been a slight typo - I think you want add_child not add_tree.

To call this you need a new Node, and you call it from the Node which you want to be the Parent. You can create a new Node from your scene file using instance().

Here is example code (this might have some typos)

var Player = preload("res://TopPlayer/TopPlayer1.tscn")

func spawn_Player1(num):
	var newplayer = Player.instance()
	# do things to setup your new player instance here, for example position
	newplayer.position = Vector2(rand_range(0,500),rand_range(0,500))