Need help erasing from an array

Godot Version



I need some help figuring out the correct way to create an array and erase from it. I’m following a tutorial and here’s where I’m at.

I have a simple script that just holds an array:

const CardList = ["Footman", "Footman", "Archer", "SquadLeader", "Spearman"]

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/") # 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

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.

The code you’ve provided is not readable this way, my friend.
I don’t know about its indentation levels!

I suggest you using this option for code when pasting it.

So It’ll look like this

var my_var := "Look mom, I am a String!"

func _do_something() -> void:

thank you, I wasnt sure how to get the pretty formatting

1 Like

This variable returns 5. If it had 1 element, it would return 1.

@onready var DeckSize = PlayerHand.CardList.size()

This one returns a number between 0 and 4.

CardSelected = randi()% DeckSize

Better do CardSelected = randi()% DeckSize+1, which would return a value between 1 and 5.

Report back!

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.

You want to use static var CardList = ...

Longer explanation:

From what I can understand, you’re loading an array directly off a class, and not a class instance.

A class is, for example, Dog, while a class instance would be var fido =

While class instances can have properties declared by var, classes have properties declared either with:

  • const
  • or static var

The static here denotes the var is a property of the entire class, and not of its instances.


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