How to change position of a variable stored inside an array or dictionary

Godot 4

Hi! I have few nodes, that are my prefabs. I include them inside the main script and then put them inside an array and dictionary (both methods are nort working) as variables. Then I tried to acces a specific variable’s position to move said node. Does it make sense? Unfortunetly it’s not working :frowning: I am doing it this way because I try to randomly pick a variable, cos it’s supposed to be a memory game. In this example I am trying to move at least a specific card, because rest of the code that randomly picks cards works just fine. The error that I get is “Invalid set index ‘position’ (on base: ‘Nill’) with value of type Vector2”. I will appreciate all the help greatly!

@onready var card1a =$memory_card1a
@onready var card1b =$memory_card1b
@onready var card2a =$memory_card2a
@onready var card2b =$memory_card2b
@onready var card3a =$memory_card3a
@onready var card3b =$memory_card3b
@onready var card4a =$memory_card4a
@onready var card4b =$memory_card4b


var card_index = [card1a,card1b,card2a,card2b,card3a,card3b,card4a,card4b]
var c = ["1a","1b","2a","2b","3a","3b","4a","4b"]
var DicCards = { 1 : card1a, 
				2 : card1b, 
				3 : card2a, 
				4 : card2b,
				5 : card3a, 
				6 : card3b,
				7 : card4a, 
				8 : card4b
				}


func _ready():

	card_index[0].position = Vector2(200,600)

put a @onready in front if your other variable too:

@onready var card_index = ...
var c = ...
@onready var DicCards = ...

WOW It’s working! Thank you! :smiley: but why is it working? Can you expalin please? :smiley:

1 Like

You try to save the values in your list, but these values only get set when the node is ready, while the list gets loaded before that. This leads to an empty list. With @onready you ensure that all the referenced variables have been set because they are also @onready

1 Like

ooo it makes sense now, thank you! :smiley:

1 Like

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