#Am I doing it wrong

1 messages · Page 1 of 1 (latest)

cerulean terrace
#

this should be an easy array append but somehow I cant make it work

so in CardManager, I have this simple append that add card to the array (the card should have values like card name, number etc)

$"../GameManager".card_on_player_board.append(card_being_dragged)

but in GameManager when I tried to use the array, it has the card object but when I check the value in it, it comes out null?

what Im doing wrong here?
let me know if you need more line of code.

#

let me send the .gd here

cerulean terrace
#
func finish_drag():
    card_being_dragged.scale = Vector2(CARD_ENLARGE,CARD_ENLARGE)
    var card_slot_found = raycast_check_for_card_slot()
    if !played_card_this_turn:
        if card_slot_found and not card_slot_found.card_in_slot:
            # Card dropped in empty card slot
            card_being_dragged.scale = Vector2(CARD_REDUCE,CARD_REDUCE)
            player_hand_reference.remove_card_from_hand(card_being_dragged)
            card_being_dragged.card_slot_is_in = card_slot_found
            card_being_dragged.position = card_slot_found.position
            card_being_dragged.get_node("Area2D/CollisionShape2D").disabled = true
            card_slot_found.card_in_slot = true
            $"../GameManager".card_on_player_board.append(card_being_dragged)
            played_card_this_turn = true
            card_being_dragged = null
            return
    player_hand_reference.add_card_to_hand(card_being_dragged, DEFAULT_CARD_MOVE_SPEED)
    card_being_dragged = null
#

so in gamemanager

#
func play_the_best_card():
    if card_on_player_board.size() != 0:
        var opponent_hand = $"../OpponentHand".opponent_hand
        var card_on_the_board = card_on_player_board.duplicate()
        var player_card_with_the_highest_point = card_on_the_board[0] 
        for card in card_on_player_board:
            **if card.value > player_card_with_the_highest_point.value:**
                player_card_with_the_highest_point = card
        if opponent_hand.size() == 0:
            return
        var random_empty_cardslot = empty_cardslot[0]
        empty_cardslot.erase(empty_cardslot)
#
  <GDScript Source>GameManager.gd:69 @ play_the_best_card()
  <Stack Trace> GameManager.gd:69 @ play_the_best_card()
                GameManager.gd:33 @ opponent_turn()
                GameManager.gd:96 @ wait()
rustic breach
#

which one is line 69

cerulean terrace
rustic breach
#

i see

#

go ahead and print out what card is first.

#

and print out what player_card_with_the_highest_point is

#

there's probably something funky going on with the $... syntax

#

(it breaks. so easily.)

cerulean terrace
# rustic breach go ahead and print out what `card` is first.
func play_the_best_card():
    if card_on_player_board.size() != 0:
        var opponent_hand = $"../OpponentHand".opponent_hand
        var card_on_the_board = card_on_player_board.duplicate()
        var player_card_with_the_highest_point = card_on_the_board[0] 
        for card in card_on_player_board:
            print(card)
            if card.value > player_card_with_the_highest_point.value:
                player_card_with_the_highest_point = card
        if opponent_hand.size() == 0:
            return
        var random_empty_cardslot = empty_cardslot[0]
        empty_cardslot.erase(empty_cardslot)
#

so I do this

#

Card:<Node2D#32883344776>

#

got this

rustic breach
#

okay so at least the card exists.

#

what's card.value supposed to be

cerulean terrace
#

Card3:<Node2D#34561066513>

#

this for the other

cerulean terrace
#

just number?

rustic breach
#

im assuming you have some kind of Card class

#

(this is why you wanna use static typing btw, so your types are clear).

#

card is something. i have no clue what, but I'm assuming a Card.

#

so what's value on the Card class.

#

@cerulean terrace ?

cerulean terrace
#

sorry

#

im a bit new, so im trying to make sense what you meant

rustic breach
#

well your card is some type of object. I want to know the type of that object. I'm assuming it's a Card.

cerulean terrace
#
extends Node2D

signal hovered
signal hovered_off

var position_in_hand
var card_slot_is_in
var value


func _ready() -> void:
    #All cards must be a child of CardRanger or this will error.
    get_parent().connect_card_signal(self)
    
func _on_area_2d_mouse_entered() -> void:
    emit_signal("hovered",self)


func _on_area_2d_mouse_exited() -> void:
    emit_signal("hovered_off",self)

ok this is on my card

#

how the card it to the value

rustic breach
#

oh god even the value isn't typed

cerulean terrace
#

like I said, im seeing a tutorial, and use my learn programming to make sense of the code

rustic breach
#

i get that yeah

cerulean terrace
#

sorry if this look so much worse

rustic breach
#

it's not your fault, the tutorial is just writing some shitty ass code.

#

anyway is the value supposed to be a number or something?

cerulean terrace
#

yes

rustic breach
#

okay so we can annotate it as being a number by, say, doing var value: int or var value: float
which lets us (and you!) know what type the value has.
but that's besides the point for now.

cerulean terrace
#

yeah I understand that

#

👍

rustic breach
#

it seems as if your value is never set to an actual value. so Godot thinks it's null.

#

(it also doesn't know to set it to an 0 rather than a null, because it has no idea that value is supposed to be a number).

cerulean terrace
#

wait

#

your right...

#

as soon I put int

#

the code run

#

huh but need more cleaning

#

thanks you

#

let me see if I can make this code work as I intended before writing this as solved

rustic breach
#

yeah go ahead and follow along with the tutorial, but do yourself a favour and use static typing.

#

it won't break anything, but it will make you write better code.

rustic breach