The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

There is a project in it there is a player enemies and a bullet. The script is to the enemy and to the player. The game does not want to start and writes that the error in the script of the enemy 11 tape.

script for the player:

enter code hereextends KinematicBody2D

var movespeed = 500
var bullet_speed = 1500
var bullet = preload("res://bullet.tscn")

func ready():
pass # Replace with function body.
func _physics
var motion = Vector2()

if Input.is_action_pressed("up"):
    motion.y -= 1
if Input.is_action_pressed("down"):
    motion.y += 1
if Input.is_action_pressed("right"):
    motion.x += 1
if Input.is_action_pressed("left"):
    motion.x -= 1

motion = motion.normalized()
motion = move_and_slide(motion * movespeed)

if Input.is_action_just_pressed("LMB"):

func fire():
var bulletinstance = bullet.instance()
instance.position = getglobalposition()
bulletinstance.rotationdegrees = rotationdegrees

func kill():

func onArea2Dbodyentered(body):
if "Enemy" in
kill()enter code here

the code for the enemy is,

`enter code here`extends KinematicBody2D

var motion = Vector2()

func _ready():
pass # Replace with function body.

func physicsprocess(delta):
var Player = getparent().getnode("Player")

position += (Player.position - position)/50


func onArea2Dbodyentered(body):
if "bullet" in
queue_free()enter code here

error in the enemy code in this line:

position + = (Player.position - position) / 50

writes here such error.
Invalid get index 'position' (on base: 'null instance').

and this is - E 0: 00: 00.631 get_node: Node not found: Player.
Condition "! Node" is true. Returned: __null
scene / main / node.cpp: 1381 @ get_node () @ _physics_process ()

so does anyone know a fix for this?

in Engine by (12 points)
reshown by

What is the error, specifically? And, please edit your post and format the code so it's easy to read in the forum. To do that, select a block of code and press the { } button in the forum's editor...

(Like jgodfrey said, it would really help if you can format your code in the post so it is easier to read. Just select each block of code and choose the {} button, and leave the pieces which are not code unformatted). Currently it's a mix of formatted and unformatted which makes it really hard to understand.

The error says that your Player variable is null. So, when you try to call Player.position it fails (because the Player variable is null and does not have a position property)

It looks like your Enemy script sets the Player variable here

func physicsprocess(delta):
var Player = getparent().getnode("Player")

(side note: physicsprocess is called every physics frame (by default 60 times a second) - if the Player node will be consistent, it might make sense to set that variable outside the physics loop and store it instead of searching the node tree for it it every frame. You might do that in your _ready function. To make that work, declare Player before all the functions then assign the value in _ready.)

I do not know how you have configured your node tree, however I recommend you experiment to confirm that that line of code exactly finds the Player node. When you say "getparent().getnode("Player")" from the Enemy script that means go up one node in the Node Tree and find a direct child called Player. If that is not the exact location of Player (for example if your Enemy is in another branch), it will be null.

Also, check the order of creation - if the Enemy is created before the Player, there can be a time when that is null. If that condition is possible you could just check if Player is null and if yes, skip movement and wait for the next frame.

Also, another thing to check - when you use this command:

position += (Player.position - position)/50

That command will try to directly set the position of the Enemy object. That is like teleporting your Enemy 1/50th of the way towards the player. It is best practice to not directly set position, instead set a direction and use moveandslide or moveandcollide to move the Enemy. This way you will get the benefit of collision detection in the physics engine. I can see you are trying to use moveandslide in the Enemy script, but I can not see where you set the motion variable for Enemy (we can see it for Player) - from the code above, it looks like motion in the Enemy script will always be an empty Vector2
Try changing the line to

motion = (Player.position - position)  
motion = motion.normalized()   

Like in your player script (however that will not work until your Player variable is populated correctly)

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.