Why does it keep giving me the invalid assignment error

Godot Version

4.4

Question

I’m trying to make a card game and have the card snap back to a specific position in the players hand if it doesn’t get used. But I keep getting this error and I’m unsure why.

This is basically what I have in the code, it’s just a little bit of it but hopefully the only relevant bit. The variable pos_in_hand is in a separate ‘card’ scene that gets instantiated into the main scene through a manager, and the function is in a node within the main scene.

var pos_in_hand

func update_hand_positions():
for i in range(player_hand.size()):
var new_position = Vector2(calculate_card_position(i), HAND_Y_POSITION)
var card = player_hand[i]
animate_card_to_pos(card, new_position)
card.pos_in_hand = new_position

The error that I keep getting is "Invalid assignment of property or key ‘pos_in_hand’ with value ‘Vector2’ on base object of type ‘Nil’

I’m very new to this and very confused. Thanks in advance!

The message means that at some point, card is zero but you’re still trying to set one of its properties (pos_in_hand).

Are there any “gaps” in player_hand, i.e. null elements?

You might just want to put this after var card = player_hand[i]:

if not card: # check if card is null or invalid
    continue # skip

But if there aren’t supposed to be any null cards in player_hand that doesn’t solve the root problem

1 Like

…Erased inaccurate part of my post…

When you post code please use the </> menu item in the message box toolbar.
This will post formatted code.

That wouldn’t nullify the reference though. It’d be a different error in that case.

Are they not passed by reference?

The value of the reference is passed by value :smiley:

If someone in another function deletes the object pointed to by card, the reference held in card becomes invalid (as the object is not existing any more) but it doesn’t automatically become null. And OP’s error says the reference is null.

Yes, caught in the strange world of kinda by reference.
You can change the properties of the passed object but you cannot change the object itself.
My mistake.