How to instantiate a scene

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

How to instantiate a scene in the script in this script, At the moment he is instantiating the player through the player_container, and this is causing a lot of bugs because if I put 1 more enemy he bugs completely, HELP

extends KinematicBody2D

var run_speed = 50
var velocity = Vector2.ZERO
var player = preload("res://player.tscn").instance()
onready var player_container = get_node("player_container")
var is_entered = null

func _physics_process(delta):
	velocity = Vector2.ZERO
	if is_entered:
		velocity = (player.position - position).normalized() * run_speed
		$Sprite.flip_h = false 
	velocity = move_and_slide(velocity)

func _on_Area2D_area_entered(area):
	is_entered = area
func _on_Area2D_area_exited(area):
	is_entered = null
:bust_in_silhouette: Reply From: Salvakiya

you need to add the player node as a child to something… maybe in your _ready(): function just do add_child(player)

When I set _ready, the player is instantiated and the scene is 2 players

Mydaz | 2019-12-10 03:22