How do I make my player teleport to a node that deletes itself after colliding, like ender pearls from minecraft?

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

So I’m trying to make my player teleport to the place where the ender pearl hit.I’m doing this in 2d btw.

:bust_in_silhouette: Reply From: vnmk8

connect a signal when the pearl collides, when the signal is triggered set the player’s position to the pearl’s position

it returns a "invalid set index’position’{onbase: ‘null instance’) with value of type ‘Vector2’."error when I try to run this code by connecting a signal to the pearl:

func _on_Area2D_body_entered(body):
node.position=self.position

Koro422 | 2021-07-12 02:40

well, I am teleporting but not in the area the pearl collided but after the pearl got thrown. What should I do?

Koro422 | 2021-07-12 03:25

on the pearl’s script:

onready var player = get_node("player")

  func _on_Area2D_body_entered(body):
    if body.is_in_group("wall"):
      player.global_position = global_position
      queue_free()

something like that with the queue_free() function the pearl should be deleted as well

vnmk8 | 2021-07-12 03:40

I meant to say that I want my player to teleport to the point where the pearl collides. The pearl also disappears after it collides.

Koro422 | 2021-07-12 03:41

Umm… while using the corrected code, I’m not getting teleported towards the walls rather the walls are getting teleported towards me.

Koro422 | 2021-07-12 03:45

I edited the code again please see the last code that I wrote.

vnmk8 | 2021-07-12 03:47

it returns “invalid set index’position’{onbase: ‘null instance’) with value of type ‘Vector2’.” error again.

Koro422 | 2021-07-12 03:56

that means it couldn’t find the player node so you have to find the correct path, so for instance if your player is in the game node the path path would be

get_node("/root/game/player")

vnmk8 | 2021-07-12 03:59

My player is in the player scene so I tried to use:

get_node("/root/res://player.tscn/player")

and I also tried to use:

get_node("/root/res://player.tscn")

and neither of those worked. So what am I doing wrong?

Koro422 | 2021-07-12 04:06

you have to reference the player’s node not the player’s scene that is different the node path in the scene is different from the scenes path in the project res:// is used for files system resources.

vnmk8 | 2021-07-12 04:14

but how do I reference a player’s node from another scene?

Koro422 | 2021-07-12 04:20

i advise you to watch tutorials or read books on godot because those are a lot of things to cover in the answers

vnmk8 | 2021-07-12 04:30

yea you’re right, I’ll try to learn and figure something out. I’m a total beginner now so I can’t really understand basic things. thank you for everything.

Koro422 | 2021-07-12 04:34

you’re welcome

vnmk8 | 2021-07-12 04:36