+1 vote

I have this teleport node that the player will collide with, that will hopefully make change the position of the player. But everytime I run the game, when it collides with the node, it crashes! Can someone help me please?

extends KinematicBody2D

#const GRAVITY = 200.0
const WALK_SPEED = 1200
var MyPos = self.get_pos()
#var teleportTile = get_node("Teleport1").get("teleportPos") The code where everything changed...

var velocity = Vector2()
func _ready():
    set_fixed_process(true)

func _fixed_process(delta):

    var motion = velocity * delta
    move(motion)
    if is_colliding():
        var collider = get_collider()
        if collider.is_in_group("Tile"):
            get_node("SamplePlayer").play("TileOn2")
            collider.queue_free()
        elif collider.is_in_group("Teleporter"):
            set_global_pos(Vector2(get_node("Teleport1").get("teleportPos")))
            get_node("SamplePlayer").play("Finish")
            collider.queue_free()

    velocity.x += delta #* GRAVITY
    velocity.y += delta #* GRAVITY

    if (Input.is_action_pressed("ui_left")):
        velocity.x = -WALK_SPEED
    elif (Input.is_action_pressed("ui_right")):
        velocity.x =  WALK_SPEED
    elif (Input.is_action_pressed("ui_down")):
        velocity.y = WALK_SPEED
    elif (Input.is_action_pressed("ui_up")):
        velocity.y = -WALK_SPEED
    else:
        velocity.x = 0
        velocity.y = 0
in Engine by (163 points)

2 Answers

+3 votes

Any time you get the error "Attempt to call function XXX in base 'null instance' on a null instance" it means you have tried to get_node a node that doesn't exist.

get_node("Teleport1").get("teleportPos")

Is there a node called "Teleport1" that's a child of this node (running the script)?

by (21,718 points)

No, it's not a child of the script.

+3 votes

You can travel through nodes with directory traversal syntax

get_node("../../Teleport1") 

This isn't your exact position, figure yours out by looking at the node tree and guessing from there...

Bellow targets it from root node of the current main scene:

get_node("root/something/Teleport1")

You probably know this already, but if you don't, learn to use print and the debugger to debug code, it will help you with solving this.

This answer has more details on this, to not repeat myself:
https://godotengine.org/qa/2476/can-a-node-under-a-instanced-scene-call-get_node-by-path

by (24 points)
edited by

P.S. unfortunately I can't edit other users questions at my level of trust currently, so it was better to add this as a second answer. But feel free to mark the first persons answer as "correct" if mine help you solve it, cause it was inspired by his... I will not shy from a +1 if that helps you, i need some trust on this website ;)

just noticed one other thing: your commented out "#var teleportTile" line near the start has to be inside of _ready when uncommented, because initialization of other nodes is only guaranteed in _ready if I understand gdscript correctly.

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.