Help getting specific value from array

Godot Version

4.4

Hello people!
i’ve been making a card game and i have almost no expirience with programming. i’ve followed a tutorial on yt and it has ben going fine, but now i have to diverge from the tutorials and so i’ve been slowly learning how to do stuff..

i’m trying to the highest value card in my opponent hand but i’m kinda going crazy. this is the database i’ve made for the cards:

const CARDS = { #Valore, Value, Type, Ability, Ability Script
“Ace” : [“A”, 14, “Numero”, null, null],
“King” : [“K”, 13, “Magic”, “Guarda la mano avversaria e ruba una carta”,“res://Scripts/Abilità/King.gd”],
“Jack” : [“J”, 11, “Magic”, “Pesca una carta”,“res://Scripts/Abilità/Jack.gd”],
“Queen” : [“Q”, 12, “Magic”, “Può essere giocata prima o dopo il Re”,“res://Scripts/Abilità/Queen.gd”],
}

after that i have a funcion that puts cards in my opponent’s hand:

func add_card_to_hand(card, speed):
if card not in opponent_hand:
opponent_hand.insert(0, card)
update_hand_positions(speed)

that i’m calling at the start of the game to have it draw 3 cards:

where do i find the values of the cards in the hand? as you can see in the commented lines ive tried (and succeded) to get the values making a new array, but i still don’t know how to tell the stupid game that thats the card i want lol.

i’m sorry if this code is super messy but this is my first project.
thanks to anyone that reads this!!

Ok so If I am getting you right you are able to get the value of the card you want (in the lines you commented out) but are unaware of how to actually get the reference to the card with that value.

So first of all your value seems to be a Database Reference. That means it contains the information which type the card is and what properties are associated with it.
Value is not a good name for this since it is very generic. I only know a small part of your code so I cannot certaintly say this but probably a name like card_properties would be better.
This would also remove confusion from the fact, that you have a property in your Database which is also called value.

Depending on the rules of your game there might be multiple Cards with the same value (Database Reference) in your Deck.
In this case there is no single card we can find which is the only card with that value (Database Reference).
But if all the cards are identical if they have the same value (Database Reference) it might be fine to just find any card with this value.

The problem with finding any card is, that in the code your provided there is no Array which contains all cards in a deck.

First you need to get this Array.
Depending on the code you havent provided there are multiple ways to do this:
Im ranking them here by how good of a solution I think they would be. (from best to worst)
1.
If the add_card_to_hand method on the OpponentHand object adds the cards to some kind of Array you could get this Array.
2.
You could create a new variable like opponent_cards which is an array. Afer creating a new card you simply append it to that array.
3.
You could use $“../OpponentHand”$.get_children() to get your array. (Please dont do this. It probably works but it is very ugly for multiple reasons.)

If you now have obtained this array, you can go trough its Elements in a for loop like this:

for card in opponent_cards:
     if card.value == highest_value_
          print(card, " was the card you where looking for.")

Note:
When creating help request please prefer this:

```gdscript
type or paste code here
/```

without the /
over taking screenshots of your code.
This saves people from copying your code manually.

1 Like

thank you so much for the replay i will look into this as soon as possible! just wanted to thank you for the advice about the request cause i didn’t know how to properly post the script lol