0 votes

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.

in Engine by (133 points)

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

like this ?

func spawn_Player1(num):
    add_tree("res://TopPlayer/TopPlayer1.tscn")

what does "The method "add_tree" isn't declared in the current class" mean? how do i declare that

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

1 Answer

+1 vote
Best answer

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))
    add_child(newplayer)
by (490 points)
edited 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 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.