Then I have the following script which references the first. The intent is to click to generate a random card from the hand, then erase that card from the array so it can’t be drawn again. The instructions in the tutorial have the as shown, but my understanding is that because the array is created as a const, I can’t erase from it (I get the error “array is in read only state”). However, if I change it to a var, the script no longer runs and gives the error "Cannot find member “CardList” in base ‘file path’ "
const CardSize = Vector2(125, 178)
const CardBase = preload("res://Assets/Cards/CardBase.tscn") # loads card template
const PlayerHand = preload("res://Assets/Cards/Player_hand.gd") # loads card template
var CardSelected = []
@onready var DeckSize = PlayerHand.CardList.size()
func _ready():
pass # Replace with function body.
func _input(event):
if Input.is_action_just_released("leftclick"):
var new_card = CardBase.instantiate()
CardSelected = randi()% DeckSize
new_card.Cardname = PlayerHand.CardList[CardSelected]
new_card.position = get_global_mouse_position()
new_card.scale *=CardSize/new_card.size
$Cards.add_child(new_card)
PlayerHand.CardList.erase(PlayerHand.CardList[CardSelected])
Can someone help me understand why the initial array needs to be a const in order for my 2nd script to find it, and what would be my best option for modifying it?
Even though GDScript provides some wrapper methods to manipulate arrays, I think you would be better off using a dictionary, which is faster and easier to use in my opinion. The wiki has some good examples of working with dictionaries. I am sorry I don’t have the time at the moment to get you a link.
I dont see the problem there, Since CardSelected is used to generate a card from the CardList array, it needs to result in a number 0-4, adding +1 could result in calling index 5 from the array.