#Invalid access to property or key when trying to compare two Strings at runtime

1 messages · Page 1 of 1 (latest)

swift knot
#

Code body where error occurs.

    if body.get_parent_node_3d().get_parent() is Monster:
        print_debug("A monster is in range!")
        print_debug(body.get_parent_node_3d().get_parent().name)
        var monster_in_range = body.get_parent_node_3d().get_parent() as Monster
        if monster_in_range.owning_player == current_monster.owning_player: # ERROR HERE
            print_debug("This Monster in range is an ally, do not target.")
            return false
        else:
            print_debug("This Monster is a foe! Target them.")
            return true
    elif body.get_parent_node_3d().get_parent() is Vaite:
        print_debug("Vaite is in range!")
        print_debug(body.get_parent_node_3d().get_parent().name)
        var vaite_in_range = body.get_parent_node_3d().get_parent() as Vaite
        if vaite_in_range.player_name == current_monster.owning_player:
            print_debug("This Vaite is an ally, do not target.")
            return false
        else:
            print_debug("This Vaite is a foe! Target them.")
            return true
    else:
        print_debug("Something is in range but not a possible target.")
        print_debug(body.get_parent_node_3d().name)
        return false```

Where "owning_player" is declared in the base class's script for Monster.
```@export var owning_player: String = "Natural" # "Natural" if not a summoned monster.```

Error: `Invalid access to property or key 'owning_player' on a base object of type 'Nil'.`

Judging from the 'Nil' in the error message I assume me trying to set `monster_in_range` is failing. I'm brand new to Godot so not sure if I'm doing it wrong. I'm trying to store the reference to the Monster that gets within target range then compare its ownership String value to see if it should target them as an enemy.
#

It's hard to read the code like that so doing a screenshot too

#

Unless I'm reading the stack message incorrectly it says that monster_in_range was correctly referencing the node I was wanting and the String variable I was comparing isn't null either.

#

Doh! I figured it out lol. It wasn't the left side of the comparison operator that was causing the issue. It was the right side, so current_monster that didn't have its reference node set before runtime. I changed that so now it's working! Marking as "Solved".