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!

asked Jan 9 in Engine by verbaloid (47 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.

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 How to use this Q&A? before posting your first questions.