0 votes

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!

in Engine by (53 points)

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.

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?

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

To be completely clear, assuming I the node tree looks like this:
Scene tree
Where the Node2D would be the player and theTargetNode 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.

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"):

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.

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


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

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

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.

ok ,thank you

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.