0 votes

I'm making a tarot card game and I'm coming across this error constantly:

Invalid get index '0' (on base: 'Array')

Basically heres what I'm doing, I have a deck of cards referenced from a JSON file in the form of a dictionary, I'm starting with a small sample but will be adding in the full deck later. Here's an example of how an entry in the dictionary is set up:

"ID" : "0",
"card" : "res://gfx/Clouds/Clouds01.png",
"title" : "Ace of Clouds",
"keyword" : "Opportunity",
} ]

My plan is to go through the deck, shuffle it then remove cards till I have just one card left which is added to a variable. It's then assigned so the game can show it on screen.

var possible_cards = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

var deck = get_deck("Deck.json")
for drawn_cards in deck:
    var drawn_card1 = deck[possible_cards[0]]
    return drawn_card1

There's a button on screen that when clicked reveals the next shuffle. The game is working fine, it's shuffling the deck, taking out one card and assigning it but after a while it gives this error and crashes. I have three cards being shown on screen (drawn_card1, 2 and 3) they all have the same code different variables, the reason I did this was to avoid getting any duplicates. The clash seems to always happen with the 3rd card and on the 5th shuffle (5th time the button is pressed)

I'm rather new to programming so this is just going over me, any ideas what's happening?

in Engine by (39 points)

seems like. Any ideas what might cause this? Have you found differences between your and my code. Why has your deck a size of 28?

Hey you, I hope you will find a solution soon, but I will be off for the rest of the week so if you need my help it has to wait :)

Hi, that's fine :) thanks for all the help. I ran your code independently and it worked fine with one card, but when I put it through 3 functions one for each card it gave the same result. I'll poke at this some more see if I can get it done with less work arounds and possibly just the one function to handle all the cards so its 3 cards are fed to one array.

How would I call a value in a dictionary from a specific point? Because I'm thinking if I can get it in one array then I could say print title of entry number 2 in dictionary. Or would it be better to do that using multiple arrays?

It's all a learning process for me. I'll update this query with anything I come across :)

I figured a way of simplifying it, not exactly a solution to the original problem and can't say if it's the most elegant of methods as I don't really know what elegant code is rofl, but it is working. I ditched the for loop for something more relatable to me. Instead of fetching from a json file with the array/dictionary setup I'm referencing values from nodes that I've assigned custom values to and saving them to an array that makes them usable in the game. So I have an array with all titles, one with the card images to be shown on screen, another with keywords etc.

func get_cards():
    var cards = $Deck.get_child_count()
    # Assign random index values from deck
    var card1 = randi() % cards
    var card2 = randi() % cards
    var card3 = randi() % cards

    # Prepare Card01 by appending values to respective arrays

    # Prepare Card02
    if card2 != card1:
      card2 = randi() % cards

    # Prepare Card03
    if card3 != card2 and card3 != card1:
      card3 = randi() % cards

I've made a function to check and reset those values which is checked when pressing the button so not letting each array go beyond 3 entries. I guess I could do this reset/check thing to the previous loop. All that for looping's been giving me a headache lol, I'll try again on that later but for now this is the solution I came up with.

Hi namathos, I would always say: "if it works, it works." If you found a solution, then this is great. Well done!

This is not the the best code I've seen (mainly because it repeats many steps again and again --> DRY) but it looks pretty good and most of it makes sense to me. Definitely way better then having three functions that are (nearly) exactly the same :-). You'r making progress! Good luck with your game

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.