Hi there,

As my first project in Godot, I have chosen to make a 2D memory game.
I would like to propose multiple card sets, and for each of them, let the user choose the number of different cards to play with.

For now, I'm just trying to display the cards on the board and make them flip when clicked on.
My problem is related to the card scene I've created. I have put a button in this scene to detect the pressed signal, when the player clicks on a card to flip it.
When I instantiate this scene multiple times (one instance for each card), whatever card I click on, only one instance is flipped (now it's the first; at some point it was the last; I don't know why).
I've tried different things, such as:

  • Setting up the signal with code
  • Setting up the signal in the main script instead of the card's one
  • Setting up a custom signal with a parameter to pass the instance reference connect("pressed", flip_card.bind(added_card), but it's always the same reference that is passed

I don't understand why it doesn't work and how I should write this differently.

The source code is available here
There are 3 scripts:

  • main.gd is attached to the main scene
  • card.gd is attached to the card scene
  • card_set.gd is a class that I use to generate a list of cards by looking into the card_sets folder, but I don't think the problem is here

I'm a beginner, I took a quick look, and I think the error is in the set / get used with a bool, which I think returns a value always the same and therefore never varies.

var is_face_up: bool:
    get:
	return self.texture == front_texture
var visible_texture: Texture:
    get:
	return back_texture if is_face_up else front_texture

Actually, I forgot to mention you can see the card faces by removing the parameter of set_card_texture like this:
card.gd

func _ready() -> void:
	set_card_texture()

This way, every card will spawn face up, and you can see they all have their respective front_texture.

I think it's a general problem with dynamic values in the instantiated scene, but I don't know why.
I also tried to tick the Local to Scene option for the card script, but it didn't change anything.

Every card instance is clickable, but it only affects the last added instance. I also tried to put the flip() function in the main script.

    qbm Oh yea i do that during my quick test, i see that actually all cards are set.
    But when i place some random breakpoints i see something about members image_value = null.
    I have check the local instances up, and yes 12 instances was located.
    But i have the feel that "null" mean an error on retrieving (set/get) the next image out of the first.
    I am a newbie, sure i can be wrong, maybe a solution can be a simplification of the project.

    I see what you mean, I think it's because the breakpoint hit just after the instantiate is called but before the values are set. If I put a breakpoint before the add_child in the main script, before all the card instances are added to the main scene, I can see these values correctly set.
    visible_texture was a bad name for the variable by the way, I changed it to opposite_side_texture.

    I tried anyway to get rid of these 2 variables by changing the set_card_texture just to be sure, but it doesn't seem to have any effect:

    func set_card_texture(texture: Texture = null) -> void:
    	if texture == null:
    		self.texture = back_texture if self.texture == front_texture else front_texture
    	else:
    		self.texture = texture

    I have tried again with much frustration, name convention isnt for sure the best.
    Managed to activate a couple of cards at same time by copyset_card_texture() under func flip().
    This is the only one that activate on press: /root/Main/FlowContainer/@TextureRect@12/Button

    func flip() -> void:
    	$AnimationPlayer.play("flip")
    	set_card_texture(front_texture)

    Can be that the trigged button signal is always fixed only on the same?
    Something wrong with instantiate for sure, only first and last texture flips and only one button record to be press, hope this can help with your investigation.

    I started the card scene all over again and I managed to make it work.
    I changed multiple things and I am now using the button $Button.toggle_mode = true and $Button.button_pressed to know the state of the button.
    I also changed mouse.filter parameter on some nodes.

    I'm not sure what was causing the issue in the first place, but it was somehow related to the card scene.

    Thanks for taking the time to help.

      qbm It was an interesting exercise and actually i still wish to know the answer.
      Probably for the best was a good way start over!