Set the player.tscn's position on a new loaded scene

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

onready var PLAYER = preload("res://PlayerFile/Player.tscn")
func _on_ToHallway_body_entered(body):
	if == "Player":
		var player = PLAYER.instance()

Im trying to add the player scene to another world but the game crashes and shows the following error message:

Invalid call. Nonexistent function ‘set_pos’ in base ‘KinematicBody2D (’.

The scene loads if I remove the last line of code but my goal is to set the player.tscn’s location to a certain coordinate on the screen, but the game crashes. I tried to make a function set_pos which takes in 2 variables(x, y) but the error message still persists. How can I go about this?

:bust_in_silhouette: Reply From: jgodfrey

There are a few problems in your attempt.

First, there isn’t a set_pos function. It’s called set_position. And, it doesn’t accept 2 arguments. Instead, it takes a single Vector2 object.

So, this should work:

player.set_position(Vector2(100, 100))

Or, even easier, just set the position property directly:

player.position = Vector2(100, 100)

It still doesn’t work. I’ll explain how I set it up again.

The Player.tscn is a child of both the World(1&2), and the Portal Script with the code below is the Script of the Parent Nodes of World1 & 2.

extends Node2D

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

func _on_ToRoom2_body_entered(body):
         if == "Player":
	       player.set_position(Vector2(100, 100))
 func _on_ToRoom1_body_entered(body):
         if == "Player":
	       player.set_position(Vector2(50, 50))

Now, why doesn’t the player’s position update? Is it updating in the previous scene?
Sorry for the late reply.

Sreenington | 2020-04-14 04:27