#Need help understanding how to detect which object the player interacted with in which order.

1 messages · Page 1 of 1 (latest)

limpid fossil
#

Hello, I'm currently working on a game jam project which works like a deck builder. The player is supposed to be able to select 3 out of 4 cards from their deck and when they choose the third one the three chosen cards are supposed to move towards three different locations.

I am having a hard time coding a component that can track which card was selected in which order. I've tried if statements for 1st 2nd 3rd cards, I've tried emiting signals but due to my lack of experience all of these have failed. How would I go about this to be able to track these everchanging variables.

I will post the codes for my Card Select Component scripts with the codes I've tried to solve my problem deleted in the comments as doing so here reaches the character limit.

#
extends Area2D

@export var interaction_area: CollisionShape2D
@export var actor_card: Sprite2D

var selected = false
var on_animation = false

func _ready():
    input_event.connect(_on_input_event)

func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int):
    if event is InputEventMouseButton && event.button_index == MOUSE_BUTTON_LEFT && event.is_pressed() && on_animation == false:
        if selected == false && Globals.card_limit < 3:
            self.on_pick()
        elif selected == true:
            self.on_deselect()

func on_pick():
    on_animation = true
    var select_tween = get_tree().create_tween().set_parallel(true)
    select_tween.tween_property(actor_card, "scale", Vector2(1.25, 1.25), 0.25).set_trans(Tween.TRANS_SINE)
    select_tween.tween_property(actor_card, "position:y",actor_card.global_position.y -48.0 , 0.25).set_trans(Tween.TRANS_SINE)
    select_tween.connect("finished", on_tween_finished)
    Globals.card_limit += 1
    selected = true

func on_deselect():
    on_animation = true
    var unselect_tween = get_tree().create_tween().set_parallel(true)
    unselect_tween.tween_property(actor_card, "scale", Vector2(1.0, 1.0), 0.25).set_trans(Tween.TRANS_SINE)
    unselect_tween.tween_property(actor_card, "position:y",actor_card.global_position.y +48.0 , 0.25).set_trans(Tween.TRANS_SINE)
    unselect_tween.connect("finished", on_tween_finished)
    if Globals.card_limit > 0:
        Globals.card_limit -= 1
    selected = false

func on_tween_finished():
    on_animation = false
rancid crater
#

Making it difficult for yourself by adding all the animation and logic together.

Could be easier to start with some simple buttons that represent the cards. Get the logic in place, and only then, move onto the animation.

The cards from the stack, and the chosen cards can be stored in an array or dictionary.

Break the code down using multiple methods/functions.

Nesting multiple conditional statements is best avoided, if possible. It starts to get messy very quickly.

Basically, get your logic working first. Ensure that you output the results of your members (arrays, etc), so that you can see what's happening.

limpid fossil
# rancid crater Making it difficult for yourself by adding all the animation and logic together....

Hello, sorry for the late response. Been busy with school and sleep.

So my theory from what I've understood from your tips is that if I can collect the information off all cards available and chosen cards in two separate dictionary. Then I can return the results from these dictionaries later on and forward it to another function / script to make the cards move to corresponding spots. And to be able to forward them to other scripts I would need these dictionaries on a Global script.

I have a few more questions, would you be able to recommend me documents or videos that I can learn how dictionaries work. I've read through the official document page but atp It's still a bit too complex.

By Nesting multiple conditional statements is best avoided, if possible. It starts to get messy very quickly., do you mean statements like

        if selected == false && Globals.card_limit < 3:
``` If so, how would you go about making lines like this better / less messy .

Thank you for your response.
rancid crater
rancid crater
limpid fossil
#

Gosh thanks man, I'm waiting for it!

rancid crater
# limpid fossil Gosh thanks man, I'm waiting for it!

Still got a bit of tidying up to do. This was a far bigger task than I originally thought. Currently, it has;

Card class
CardSelectionManager class
CardConfig resource

The Card class is an Area2D scene that is instantiated in the main scene for each card that can be chosen. It simply handles the animation, checks for input, animation tween state, signals, etc.

The CardSelectionManager class handles all the logic, registers the cards, controls the initial card layout and final selected cards layout (that will be moved though).

The config file has options for number of allowed selected cards, positioning values, allow deselect bool, etc

Hopefully, I'll have it done tonight for you.