Card Deck

Hey guys ! I’m new to the forum and the game and I need your help xD

How can i get a random object from the array and show it ? XD

In case of a card deck what I would do is have an object with all the card images attached as individual animations then simply pick a random animation:

Let say we have 20 cards in the deck which means our deck object got 20 animations, so we can pick a random animation for the object to display a random card like so:

Do = Random(19) to Animation of Deck

However, in case you do prefer to have each card as an individual object, add each card object to a group name the objects Card1, Card2…etc then you can pick and create a random card object from the group by using the event Create object from it name and for name enter the expression:

"Card" + ToString(Random(19))

In case you are using structure variable in the back end for the logic, you can do the same, have a variable called Deck and name each structure (card) simply as numbers 1,2,3,4,5 or Card1, Card2…etc then you can pick a random structure like so:

Deck["Card" + ToString(Random(19))]

You may notice I entered 19 everywhere though we have 20 cards, it is because the Random() expression return a value in range 0-19 which is 20 values including 0. But if you want a value in range 1-20 you can use the expression RandomInRange(1,20) instead of Random() but keep that in mind in case you are using sprite animations, the first animation of a sprite is always 0

You will need extra logic to not repeat cards, for example check for child existence and delete used cards from the structure (probably there will be a while loop around).

There is a condition to check what is the current animation of an object and then you can use the Delete object action to delete the object or the Change animation action if you want to display a different animation. Can also use the Remove child action to delete a child of a structure variable.

How do you add objects to a group?
I don’t see that option.

The group objects button is to the right of the regular objects button. The icon has three cubes instead of one.

Click on that to open the panel, then click on the add button, name the group, and from there you should be able to choose objects to put in it.

Thank you. :slight_smile:

You’re welcome :slight_smile:

That really helped, but how do i make it not to repeat the cards when it creates it?

you have to keep track or shown cards. For example if you delete each card after showing it, you should change random(19) by random(amount_cards)
First time amount_cards=19 then you decrease it by 1 each time to take out a card

Hi ddabrahim,

I have followed these steps because I am in the same situation but the part Deck[“Card” + ToString (Random (19))] does not work for me.
I’ve tried everything I think. Could you give me some advice?

Sorry I have no time to look in to this. I can only think of the obvious from the top of my head which is you don’t have 20 cards / animations and with Random(19) you are trying to pick a card / animation which you don’t have. If you have only 10 cards, try Random(9)…

If it’s not the problem then I have no idea. If you can share a screenshot of some of your events and explain in more detail what do you expect to happen and what is happening or not happening exactly, then maybe someone else can offer you some advice and a solution.

1 Like

In case you are using structure variable in the back end for the logic, you can do the same, have a variable called Deck and name each structure (card) simply as numbers 1,2,3,4,5 or Card1, Card2…etc then you can pick a random structure like so: Deck[“Card” + ToString(Random(19))]

I don’t know if I can explain myself well since English is not my language. But what I try to achieve is that the cards are not repeated when creating them. I have 10 cards as individual objects (Card1, Card2 … Card10). I have a structure variable for the logic as you say in the post. My question now is how to avoid that the cards are repeated when dealing (creating). I have tried using the expression Deck[“Card” + ToString(Random(9))] but it has not worked for me.

I have also followed the advice from Lizard-13 just below your post to use a conditional while to remove existing cards and they cannot be repeated but I can’t get it to work.

I hope I have explained myself well and thank you for your time.

One way I can think of to do it is to use a structure variable and add as many Childs to the structure as many cards you have for reference and use the value of each child to flag if a card is taken from the deck or not. Then you can use a While loop to pick a random card until you find one that is not taken from the deck yet.
Probably there is a more elegant way to do it but it works. I’m not sure how to explain the actual implementation in a simple way so instead I have created an example with 5 cards. You should be able to just add more cards and change the value of a single variable and it should work with any number of cards. Each card object belongs to a group called Deck.

You can download it from here:

I have commented every single events but the expressions I was using may going to be confusing to you if you are a complete beginner.

Hope it helps :+1:

I am not sure if I completely understand the problem, but in my game (XenoFeud, coming soon :slight_smile: :smiley:) I am using the inventory system to check if, in your case, a card is still available.
Actually I got inspired to use this system also from a post from ddabrahim in 2016.
I will just quote his post here: