Can someone help me with this error. The error: Invalid get index 'deg_for_bullet' (on base: 'Nil').

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

I’m trying to add a gun to my player that shoots at the mouse and this one error is making me not able to do that I need help.

Please post your code so we can help you to debug it.

timothybrentwood | 2021-11-10 00:37

bullet code:

extends Area2D

export var speed := 600
var vel : Vector2

func _ready():
vel = Vector2(speed, 0).rotated(Global.player.deg_for_bullet)
pass

func _process(delta: float) → void:
position += vel * delta

func _on_pellets_area_entered(area) → void:
queue_free()
if area.is_in_group(“cockroach”):
area.die()

func _on_pellets_body_entered(body: Node) → void:
queue_free()
if body.is_in_group(“cockroach”):
body.die()

Player code:
var deg_for_bullet : float

onready var shotgun := $Position2D/shotgun
onready var gun_holder := $Position2D

func gun_stuff() → void:
var mouse_pos : Vector2 = get_global_mouse_position()
deg_for_bullet = mouse_pos.angle_to_point(shotgun.global_position)
gun_holder.look_at(mouse_pos)
if global_position.x > mouse_pos.x:
$Position2D/shotgun/Sprite.flip_v = true
else:
$Position2D/shotgun/Sprite.flip_v = false

other code:
extends Node2D

func _input(event: InputEvent) → void:
if event.is_action_pressed(“shoot”):
var bullet := preload(“res://bullet.tscn”).instance()
bullet.rotation = $player.deg_for_bullet
bullet.global_position = $player/Position2D/shotgun/muzzle.global_position
add_child(bullet)

sup0088 | 2021-11-10 03:36

:bust_in_silhouette: Reply From: timothybrentwood

Your issue is on the line:

bullet.rotation = $player.degforbullet

Two things could be going on here:

  1. The player node doesn’t exist at the NodePath player i.e. the
    node structure isn’t like this:

    |>Node2D (that the bottom script you pasted is attached to)
    |->player

  2. Your node structure is correct (looks like described above) but your player node is deleted at some point via queue_free() or free()

If it’s #1, you just need to fix your NodePath on the line that I listed so it correctly points to the player node RELATIVE to the node that script is attached to. CAPITALIZATION IS IMPORTANT WITH NodePaths!!! player is NOT the same as Player

If it’s #2 this code fix, while not being ideal, should remove the error:

# other code:
extends Node2D

func input(event: InputEvent) -> void:
	if event.isactionpressed("shoot"):
		var bullet := preload("res://bullet.tscn").instance()
		var player_node = get_node_or_null("player")
		if player_node:
			bullet.rotation = player_node.degforbullet
			bullet.globalposition = $player/Position2D/shotgun/muzzle.globalposition
		addchild(bullet)

Well I fixed the error but now its not shooting anything.

code I changed:
extends Node2D

func input(event: InputEvent) → void:
if event.is_action_pressed(“shoot”):
var bullet := preload(“res://bullet.tscn”).instance()
var player_node = get_node_or_null(“player”)
if player_node:
bullet.rotation = player_node.degforbullet
bullet.globalposition = $player/shotgun/muzzle.globalposition
add_child(bullet)

sup0088 | 2021-11-10 12:46

If you changed your code to that and it doesn’t put the bullet next to your player node then your NodePath is wrong (#1). Post a screen shot of your SceneTree (left side of your editor where all the nodes are) and I’ll be able to tell you what your NodePath should be.

timothybrentwood | 2021-11-10 13:32

enter image description here

sup0088 | 2021-11-10 21:21