How to teleport Player to another sprite position?

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

I’m trying to write a teleport script, how can I simply teleport PLayer to a position of a certain node?

I have this in my PLayer script:

func teleport_to(target_pos):
	position = target_pos

How do I invoke it in a keypress script? Right now there’s a line using Groups, but it doesn’t work

export (NodePath) var teleport_target = null
    
if Input.is_action_just_pressed("enter_door") and player_in_door == true:
    get_tree().call_group("Player", "teleport_to", get_node(teleport_target).position)

When I press that button there’s an error:

Invalid type in a function ‘get_node’ in base Area2D (Teleport1):
cannot convert Argument from Nil to NodePath

Please help!

Your code should work, and the error is because the teleport_target node path is Nil at that point in time. Try debugging or printing to check the value yourself. The moment it is a valid NodePath it should work.

tastyshrimp | 2020-01-09 22:36

I don’t quite understand. The debug doesn’t show anything else.
I don’t quite understand how do I put something to teleport_target variable? Is that what returns Null?

How do I assign a certain node global position as teleport_variable?

verbaloid | 2020-01-10 08:46

So the teleport_target is being initialized as null in your export line. You need to set it to a node so you can have a position. That can be done through the GUI, when you export a variable it appears in the node properties, or you can set it by code. The simplest way would be to just write a path, like res://nodes/TeleportTarget.tscn or /node/path whatever it is, you need to set it to a valid node path for the get_node to work

tastyshrimp | 2020-01-10 08:51

To be completely clear, assuming I the node tree looks like this:
Scene tree
Where the Node2D would be the player and the TargetNode is the target.

And the script has an export like you did. I could use the GUI to set the TargetNode as the Teleport Target from the Script variables section, like so:
Node section

The other way to set it would be code wise, either in the ready section or if something else passes me the NodePath needed, for example, the door could give you the next teleport target. Here’s an example of setting in the ready with a local path:
enter image description here

Hope it helps.

tastyshrimp | 2020-01-10 09:07

this isn’t working for me, I keep getting an error saying "Invalid get index ‘position’ (on base: ‘Null instance’). Can you help me out?

extends KinematicBody2D
export (NodePath) var teleport_target = null

func _ready():
   teleport_target = 'Player'

# warning-ignore:unused_argument

func _process(delta):
 if Input.is_action_just_pressed("ui_up"):
	print(get_node(teleport_target).position)

Dumemes | 2022-04-11 01:45

In general when the error refers to Null instance it means that the previous method call returned Null and Null does not have any methods. In this case it is most likely that get_node did not find any node with the expected name.
In short, make sure get_node is actually returning a node, in this case a node called Player and the position call should work.

tastyshrimp | 2022-04-11 06:49

The node Im trying to teleport it to is called Player, and its not working. Do I have it backwords?

Dumemes | 2022-04-11 16:30

Node — Godot Engine (stable) documentation in English

get node is relative, if Player is not a direct child of the node you are at it won’t work

tastyshrimp | 2022-04-11 18:10

oh that makes sense then. Is there a way to teleport to a node that’s not a child?

Dumemes | 2022-04-12 13:41

Sure it is possible, you just have to find the Node.
get_node gets the node from anywhere. You just need to find it in the tree. i believe there is also a find_node method that allows you to search for a node by name. Check the documentation or other resources on how to do that.

tastyshrimp | 2022-04-12 14:01

ok ,thank you

Dumemes | 2022-04-13 23:20