Unable to run knockout error in script

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

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_process(delta):
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)
look_at(get_global_mouse_position())

if Input.is_action_just_pressed("LMB"):
	fire()

func fire():
var bullet_instance = bullet.instance()
bullet_instance.position = get_global_position()
bullet_instance.rotation_degrees = rotation_degrees
bullet_instance.apply_impulse(Vector2(),Vector2(bullet_speed,50).rotated(rotation))
get_tree().get_root().call_deferred(“add_child”,bullet_instance)

func kill():
get_tree().reload_current_scene()

func _on_Area2D_body_entered(body):
if “Enemy” in body.name:
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 _physics_process(delta):
var Player = get_parent().get_node(“Player”)

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

move_and_collide(motion)

func _on_Area2D_body_entered(body):
if “bullet” in body.name:
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.
<Error C ++> Condition “! Node” is true. Returned: __null
<C ++ source code> scene / main / node.cpp: 1381 @ get_node ()
Enemy.gd:9 @ _physics_process ()

so does anyone know a fix for this?

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…

jgodfrey | 2020-11-25 15:34

(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: _physics_process 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 move_and_slide or move_and_collide 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 move_and_slide 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)

AndyCampbell | 2020-11-26 00:28