0
\$\begingroup\$

To add images, as far as I know you create a sprite in godot, but I don't want to do that, I'd like to do it pro grammatically using arrays, the reason is I'm creating a card game and I don't want to add 52 scenes and then attach 52 sprites.

Isn't there a smarter way to do it? I haven't found anything. What I'm doing now is I have a card scene and then I inherit from it the other cards scenes and attach sprites to them and add them to a 2d array but I'd like another way of doing that

\$\endgroup\$
  • \$\begingroup\$ Why do you need to create a new Scene for each new Sprite? Also assuming you've checked Godot's API, have you tried creating the Sprite object yourself, inside the code? \$\endgroup\$ – TomTsagk Feb 18 at 10:03
  • \$\begingroup\$ @TomTsagk I'm not sure how to create sprite object by coding it, all tutorials i followed create scenes using the editor and inherit it, I'd like to do it using Gdscript, not sure how though \$\endgroup\$ – Lynob Feb 18 at 23:43
2
\$\begingroup\$

Put your cards in a folder and set them names which follow a logic, like: card_0, card_1, card_2, and so on. As you can see, you can use a loop from 0 to 52 and using the method

load(your-path + number)

you can retrieve all cards.

I think you mean by a scene that you create a scene which handle the logic for a card. If this is the case, you need to instantiate the scene and call a method you defined, in order to pass the path of your card and load it.

Good luck!

\$\endgroup\$
1
\$\begingroup\$

I used the sprite sheet found at https://www.emanueleferonato.com/2017/03/04/html5-deck-of-cards-management/ .

You can create a single Card scene with a single sprite. The sprite is set up as animation (4 Vframes by 13 Hframes), even though the animation won't actually play continuously.

Instantiate 52 instances of the Card scene, setting each instance's frame in a loop:

var deck = []
var c = load("res://Card.tscn")
for i in range(52):
    var n = c.instance()
    n.position.x = i * 30
    n.position.y = 200
    n.get_node("Sprite").frame = i  # <==== this is the trick
    deck.append(n)
    add_child(n)

Result: https://www.wookilar.com/img/cardswave.gif

\$\endgroup\$
  • \$\begingroup\$ what if you dont want animation? \$\endgroup\$ – Lynob May 1 at 22:07
  • \$\begingroup\$ Any individual card is not animated. I.e. the King of Hearts card remains the King of Hearts forever; it doesn't change its face to some other suit/rank. The wavy pattern in my result screenshot was just a way for me to test out the func _process() of Godot. I was just changing the position.y of each card every frame. \$\endgroup\$ – amonroejj May 2 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.