Jooney
Jooney

Reputation: 39

Attempt to call function 'get_position' in base 'null instance' on a null instance. ERROR

I am trying to create a topdown shooter with a player and a bullet on a separate scene. But whenever I run the game it says

Attempt to call function 'get_position' in base 'null instance' on a null instance.

BTW I am new to Godot.
The problem seems to be in this function:

const SPEED = 300
var bullet = preload("res://Mini-Scenes/Bullet.tscn")

func shoot():
    var b = bullet.instance()
    add_child(b)
    b.set_position(position)
    b.move_and_slide(Vector2(1, 0).rotated(rotation) * SPEED)

And I also don't think that I understand how instancing works,
so my questions are:

  1. How does Instancing work?
  2. What makes the above function fail?

Upvotes: 2

Views: 7599

Answers (1)

Dylan Culfogienis
Dylan Culfogienis

Reputation: 33

Alright, in order:

  1. Instancing is simple in principle. Godot takes the base Node of a scene and all its children, and copy-and-pastes them into memory. Then, when you add this Node as child to another in the scene, _ready is called, the Nodes begin to _process and _physics_process, as well as a few other things like receive events (as a Node must be within a SceneTree in order to interact with other nodes, and the most common way to add one to a tree is making it the child of another).

For more information, see the documentation on Instancing

  1. The above function actually works flawlessly, I just created a new project in Godot 3.1.1 and duplicated your setup, no errors occur. The problem must be somewhere else in your code or your scene setup, and we'd need to see your full script and scene layout to determine the cause of the problem. If you update your question with that information, or shoot me a message on Discord (Mantissa#2558), I'll be able to fully answer your question.

Upvotes: 0

Related Questions