Weapon display system

Godot Version

@onready var weapon_disp_fist = $HUD/"Weapon Fist"
@onready var weapon_disp_pistol = $HUD/"Weapon Pistol"
@onready var weapon_disp_assault = $HUD/"Weapon Assault Rifle"
@onready var weapon_disp_grenade = $HUD/"Weapon Grenade"
@onready var weapon_disp_machine = $HUD/"Weapon Machine Gun"

var weapon_slot = "Fist"

# Weapon textures variables.
const item_fist = preload("res://UI/Items/Hud Fist.png")
const item_pistol = preload("res://UI/Items/Hud Pistol.png")
const item_assault = preload("res://UI/Items/Hud Assault Rifle.png")
const item_grenade = preload("res://UI/Items/Hud Grenade.png")
const item_machine = preload("res://UI/Items/Hud Machine Gun.png")

func _process(delta):
	match weapon_slot:
		"Fist":
			weapon_disp_fist.set_deferred("texture", item_fist)
			
		"Pistol":
			weapon_disp_fist.set_deferred("texture", item_pistol)

Question

I am trying to have my hud display the current weapon in the weapon slot, and I’ve been trying to do this through changing the texture but it doesn’t work.

Is there an error, or is it silently failing?

an error

it says “attempt to call function '‘set_deferred’ in base ‘null instance’ on a null instance”

That means weapon_disp_fist isn’t properly initialized. What happens if you do:

func process(delta: float) -> void:
    match weapon_slot:
        "Fist":   $"HUD/Weapon Fist".set_deferred("texture", item_fist)
        "Pistol": $"HUD/Weapon Fist".set_deferred("texture", item_pistol)

it says “cannot call method ‘set_deferred’ on a null value”

That means it doesn’t like $HUD/Weapon Fist, which usually means it’s in another part of the scene tree. What does your node hierarchy looks like, and where is this script attached?

To be clear, what I think is happening is something like:

Scene
    Player           << script attached here
    HUD
        Weapon Fist

In that case you’d need $"../HUD/Weapon Fist" to reach that node from Player.

Player scene:


HUD scene:

Ah, ok, try:

"$HUD/Weapons/Weapon Fist"

You need the full path.

dang i forgot

It’s easy to forget, and it doesn’t fail until runtime.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.