# Help with first game please

### Godot Version

3 version

Hi everyone, i want to create a player health where a bullet collides with him and he dies, when i wrote my code there is error invalid get index position of my enemy.
thats my enemy code

# Movement speed

export var speed = 180
onready var player = get_parent().get_node(“Player”)
var player_position

func _ready():
# Initialize player_position with the player’s current position
player_position = player.position

func _physics_process(delta):
# Update player_position with the player’s current position
player_position = player.position

``````# Check the distance between the current position and the player's position
if position.distance_to(player_position) > 3:
# Calculate the angle in radians
var angle_rad = (player_position - position).angle()
# Convert the angle to degrees
var angle_deg = rad2deg(angle_rad)
# Apply the -30 degrees offset
var final_angle_deg = angle_deg + 90
# Set the node’s rotation_degrees to the calculated angle in degrees
rotation_degrees = final_angle_deg

# Move the plane towards the player
var direction = (player_position - position).normalized()
position += direction * speed * delta
``````

And thats my player code

var speed = 500
var acceleration = 800
var deceleration = 1000
var velocity = Vector2.ZERO
var axis = Vector2.ZERO
var rotation_speed = 500
var target_rotation = 0.0
var current_rotation = 0.0
var return_speed = 5
var MAX_ROTATION_DEGREE = 30

func _ready():
self.connect(“area_entered”, self, “_on_Area2D_area_entered”)

func _physics_process(_delta):

``````axis.x = Input.get_action_strength("right")-Input.get_action_strength("left")
axis.y = Input.get_action_strength("down")-Input.get_action_strength("up")

axis = axis.normalized() * speed

velocity = velocity.linear_interpolate(axis, 0.025)

velocity = move_and_slide(velocity)
``````

func _process(delta):

``````if Input.is_action_pressed("right"):
target_rotation = deg2rad(MAX_ROTATION_DEGREE)
elif Input.is_action_pressed("left"):
target_rotation = deg2rad(-MAX_ROTATION_DEGREE)
else:
target_rotation = 0.0

current_rotation = lerp_angle(current_rotation, target_rotation, return_speed * delta)
rotation = current_rotation
rotation = clamp(rotation, -PI/8, PI/8)
``````

func _on_Area2D_area_entered(area):
if area.is_in_group(“Bullet”):
die()
print(“bullet”)
if area.is_in_group(“Enemy”):
die()
print(“enemy”)

func die():
print(“you are dead”)
queue_free()

Try exporting the player instead. The parent of a node will not be ready, and it’s siblings are not guaranteed to be ready either, do not try to access siblings on ready.

Can you please explain it in code,i dont understand very well.

Seems like you are using Godot 3, not Godot 4

``````# godot 3.x
export(Node) var player

# godot 4.x
@export var player: Node
``````

Once exported you can set what the player is in the Inspector.

Yes i am using godot 3,with this code if my player will collide with enemy it will be deleted? Without error ?

I don’t see any reason why the enemy script would delete itself. Only the player uses `queue_free` on itself.

Yeah i mean player not enemy with my code without your script,when my player collides with enemy,game stops and there is error index position … In my enemy script.but now i think it will work

Ah well that’s going to happen if you delete the player and expect the enemy to carry on. You’ll have to tell the enemy (or enemies) to check if the player exists, update them if the player was deleted, or not delete the player at all if appropriate (like a game-over screen).

The laziest solution is the first one, done like so.

``````func _physics_process(delta):
# Update player_position with the player’s current position
if is_instance_valid(player):
player_position = player.position``````
1 Like

Sadly now i cant try this,i will try later i think it will work.Thanks very much.

1 Like

Good luck! tell us how it goes!

1 Like

Thanks sooo much,it worked.Can you help me with one more thing ?

1 Like