I need help with error code i don't understand

Godot Version

godot 4

Question

i need help trying to fix this error code i keep getting Invalid assignment of property or key ‘damage’ with value of type ‘int’ on a base object of type ‘Nil’.
i don’t understand why i get this error code am trying to make hitbox for my combat system.

here is the player code:
extends CharacterBody2D

@export var speed: float = 100
@onready var animation = $PlayerAnimation
@onready var weapon = $WeaponFX
@onready var hitbox : $Hitbox

@export var current_item : Item:
set(value):
current_item = value

	if current_item != null:
		if current_item.animation in ["blade"]:
			set_damage(current_item.damage)
		else:
			set_damage(1)

func _ready():
get_node(“HitBox”)
pass

var lastAnimDirection : String = “down”
var time : float = 0
var can_move : bool = true:
set(value):
can_move = value
if value == false:
speed = 0
else:
speed = 100

func handleInput():
var moveDirection = Input.get_vector(“left”, “right”, “up”, “down”)
velocity = moveDirection*speed

if Input.is_action_just_pressed("attack") and time <= 0:
	Attack()

func Attack():
can_move = false
time = 0.6
if current_item == null:
animation.play(“fist” + lastAnimDirection)
else:
$Weapon.visible = true
$Weapon.texture = current_item.texture
weapon.play(current_item.animation + lastAnimDirection)
animation.play(current_item.weapon_animation + lastAnimDirection)

func updateAnimation():
if can_move:
if velocity.length() == 0:
animation.play(“idle” + lastAnimDirection)
else:
var direction = “down”
if velocity.x < 0: direction = “left”
elif velocity.x > 0: direction = “right”
elif velocity.y < 0: direction = “up”

		animation.play("walk" + direction)
		lastAnimDirection = direction

func _physics_process(delta):
if time > 0:
time -= delta
handleInput()
move_and_slide()
updateAnimation()

func set_damage(amount):
hitbox.damage = amount

func _on_player_animation_animation_finished(anim_name: StringName) → void:
can_move = true

here is my hitbox code:
extends Area2D

@export var damage : float = 1

func _on_body_entered(body: Node2D) → void:
if body.has_method(“take_damage”):
body.take_damage(damage)

any help would be appreciated.

The error says that you are trying to assign damage property on an object that doesn’t exist. The only place you are assigning damage here is in this function:

func set_damage(amount):
    hitbox.damage = amount

Which means hitbox is null.

Because the hitbox as an @onready variable, this means you are trying to assign the damage before the node is ready. Looking at where you call the set_damage method, it’s clear that the setter for current_item is causing this.

Here’s the timeline of what happens:

  • Script gets created
  • Godot assigns the current_item from the editor
  • Setter calls the set_damage function
  • Error :frowning:

The character’s _ready function didn’t run yet, so the @onready var hitbox is null by default!

You can get around this pretty easily. Put a null check into the set_damage function, so that the error doesn’t happen. And call it in your _ready method, so that the damage is ready and set for when the game runs.

Or you can trigger it with the setter again, that would be pretty cool :smiley:

func _ready() -> void:
    if current_item: set_damage(current_item.damage)
1 Like

thanks for your reply am just stuck on what you mean by put a null check in the set_damage function sorry about this am still a newbie to godot code system

func set_damage(amount):
    if hitbox:
        hitbox.damage = amount

Ah, its okay. Just check if hitbox is null before you try to set anything on it

if hitbox != null:
    hitbox.damage = amount

That’s what I’d call a null check :slight_smile:

1 Like

I would use:

if is_instance_valid(hitbox):
      # more code here....

Best wishes,

Paul

In the docs:

1 Like

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