I was not sure how to title this. But I have a basic card game with a dictionary that houses the cards info. I have a random number generated that will correspond with the dictionary. Once the random number is generated it will either grab the correct card and add that number to an array OR if the number generated is in the array it will generate a new number until it grabs one, not in the array. What I've done is not working. I was hoping someone could look at my code and tell me what I did wrong. I also would ultimately like the array to be cleared once it reaches 252. I hope I explained that well enough. Below is my code. Thanks in advance for any advice!!

onready var index = OnStartData.random_numbers.find(OnStartData.random_numbers)
var rand = RandomNumberGenerator.new()

func _ready():
	OnStartData.score = 3
	rand.randomize()
	OnStartData.random_number = rand.randi_range(1,252)
	if index == -1:
		OnStartData.random_numbers.append(OnStartData.random_number)
		print("not found")
		print (OnStartData.random_numbers)
	else:
		while index.has(OnStartData.random_number):
			rand.randomize()
			OnStartData.random_number = rand.randi_range(1,252)
		print(index)
  • xyz replied to this.
  • charliemacdmv
    There's a better way of doing it. Similar to what you'd do with real cards.
    You don't really need a dictionary if you use integer indices. So put the cards into a plain array. Shuffle the array using Array::shuffle() and then pop cards one by one from the start or end of the array.

    var deck = Array()
    
    # populate deck with 100 dummy cards wehre each card is just a number corresponding to its initial index
    for i in range(100):
    	deck.push_back(i)
    
    #shuffle
    deck.shuffle()
    
    # draw cards
    while not deck.empty():
    	print(deck.pop_back())

    charliemacdmv
    There's a better way of doing it. Similar to what you'd do with real cards.
    You don't really need a dictionary if you use integer indices. So put the cards into a plain array. Shuffle the array using Array::shuffle() and then pop cards one by one from the start or end of the array.

    var deck = Array()
    
    # populate deck with 100 dummy cards wehre each card is just a number corresponding to its initial index
    for i in range(100):
    	deck.push_back(i)
    
    #shuffle
    deck.shuffle()
    
    # draw cards
    while not deck.empty():
    	print(deck.pop_back())

      xyz Wow that is way smarter than my way. I am very new to godot. So i wasn't aware of the shuffle action. Thank you very much!! I will give this a shot.

      xyz I should have mentioned the reason i'm using a dictionary is because i'm bringing all the cards in from Google sheets using JSON. WOuld that change anything in your solution?

      • xyz replied to this.

        charliemacdmv It doesn't really matter. If your dictionary keys are integers then you can just copy the values into an array. Otherwise you can copy the keys into an array, and index the dictionary consecutively with shuffled keys.

        You have to use the array for shuffling because it's an ordered data structure, in contrast to dictionary which doesn't guarantee any specific ordering of items and therefore doesn't have the shuffle() method.

          xyz I really appreciate your help. I used your method to see what it does. And it does exactly what i need. Again, I'm new to Godot. How would i take those numbers and match them to my dictionary? I tried the code below but it is not working.

          $HBoxContainer/Genre.text = Starter.starter_data[str(deck.pop_front())]["Card"]

          • xyz replied to this.

            charliemacdmv

            # example dictionary
            var dict = {
            	"key_one" : "value_one",
            	"key_two" : "value_two",
            	"key_thre" : "value_three"
            }
            
            # get an array of dictionary keys
            var keys = dict.keys()
            
            # shuffle the keys
            keys.shuffle()
            
            # index the dictionary with shuffled keys
            while not keys.empty():
            	print(dict[keys.pop_back()])