Download

Deck of Cards - Somebody help I seriously forked up T~T

Ok, so I’m working on a little VR project, and right now I’m trying to make a deck of cards that’ll give you a different card each time you draw from it. However, I’m having a very strange issue that I can’t really explain in one or two sentences. So first I’m going to explain what I’m trying to do with my blueprints, and then explain what’s actually happening. I’ll also post alternate links to these images just in case. (Buckle up kids, this could take a minute.)

(alternate link → Imgur: The magic of the Internet )

Ok, so here is the beginning of it all. Basically, I just have it so that the deck spawns a card in the player’s hand, instead of picking up the deck. This part works without fail.

(alternate link → Imgur: The magic of the Internet )

Then I generate a random integer. I set the integer so that it won’t go higher than 51, since there are 52 cards in a regular deck of playing cards. Then I see if the number generated is in an array “removed cards.” If it is, then I go back and generate another number, if it isn’t then I move on to the next step. I also have a text render component above the deck to see what number is being generated (since I can’t really use the debugging tools in VR, I kinda have to make my own.)

(alternate link → Imgur: The magic of the Internet )

Ok, this next part I’m sure someone is going to yell at me for. This is what the next bit looks like zoomed out all the way; just to give you an idea of how much **** went into the next part.

(alternate link → Imgur: The magic of the Internet)

(This is what the previous picture looks like up close.) So first off, there’s a couple of nodes here that I forgot to put in the screenshot. I have a name array of 52 elements (each one being a different card) Just before this mess of wires, I have a “get cards” node, and a “get index” node. I plugged the “get cards” node into both the “get index” node, and all those == nodes you see there. I also plugged the aforementioned random integer node into the “get index” node. So what it’s doing (or at least what I think it’s doing) is pulling a random element (or card type) from the “cards” array based on what random integer comes up. After that, I’m performing 52 checks to see which card name got pulled from that array. When it finds a match, it sets 2 integer variables into the card I spawned earlier: A “suit” variable, and a “#”(number) variable.

(alternate link → Imgur: The magic of the Internet )

Ok, so after it does all that stuff, I have it perform a custom function from the card_bp that it spawned earlier which sets the picture on the face of the card (I’ll go over that in a second.) Then it takes the previously generated integer, and adds it to the “Removed Cards” array, and removes the corresponding index from the “Cards” array. Then if there’s nothing left in the “Cards” array it just destroys the whole deck altogether.

OK, so that’s what’s in my deck_bp thus far.

(alternate link → Imgur: The magic of the Internet )

This is what’s in the “Set Card Picture” function that I was talking about. It takes those “Suit” and “#” variables that I was talking about earlier, and creates a dynamic material out of a material that uses those variables to map out the texture below.

(alternate link → Imgur: The magic of the Internet )

(alternate link → Imgur: The magic of the Internet )

I also have another thing set up in this function. I have a text render component on my card_bp as well, it’s set up so that it says the card’s number and suit based on the “Suit” and “#” variables. But again, this is just for debugging purposes.

Ok, so that’s what I have set up and my thought process behind it. Now here’s what it actually does.

Every time you draw a card, one of 2 things will happen. You will get one of two kinds of cards:

(alternate link → Imgur: The magic of the Internet ) (also I’m sorry about the weird angles, it’s hard to screenshot in vr T~T )

The number indicating the random integer always changes with this, but this card repeats a bunch. It always says “Text” over the top like that, and it’s always ace of hearts. But if you don’t draw that, you’ll get this one:

(alternate link → Imgur: The magic of the Internet )

(this isn’t to say that this card is always the 3 of clubs, the face and text on this card actually function perfectly) Now at a first glance, there’s really nothing wrong with this. The image on the card says 3 of clubs, and the text above it does too. However, there is one really weird inconsistency. The floating number above the deck is supposed to be indicative of the random integer that we generated earlier, which also was supposed to determine which card we picked from the “Cards” array. But look here:

(alternate link → Imgur: The magic of the Internet )

Element 18 in the “Cards” array is not the 3 of clubs, but in fact the 7 of clubs. So why the toot did I get the 3 of clubs?

So to recap on everything, I’m trying to make a working deck of cards, but I forked up somewhere super hard and I have no idea where or how or who or where. There’s a lot of **** here, if you need me to re-explain something please ask, cause I really want to figure this out. Any and all input is greatly appreciated.

UPDATE: I figured it out, I just had to put an “is valid index” check after that first branch.

To reduce some of the blueprint wires, could you use an enum?

I could use an enum, but I would still need to specify what to do with all 52 possibilities. I also tried to use a function, but I still couldn’t find a way to not have to use it 52 times. If I just wanted it to draw a random card, then I wouldn’t need all this. But I also want it to remember which cards I’ve drawn so that it doesn’t draw the same card twice. The easiest way I can think to do that is to use that name array.

Here’s a suggestion that should help simplify your code and make it less error prone.

It looks like you already have an array, “Cards”, that contains all of the cards in your deck.

  1. Make a copy of the “Cards” array and call the copied array “ShuffledDeck”, for example.

  2. Array type has a method on it called “Shuffle”. Execute “Shuffle” on “ShuffledDeck”. You have now randomized the ordering of the cards in “ShuffledDeck”.

  3. Now, when a card is drawn and “ShuffledDeck” still has cards in it, execute “Pop” on “ShuffledDeck”. This will get you a card value and remove it from “ShuffledDeck”.

When your shuffled deck is empty, repeat steps 1 and 2 to get a new deck of shuffled cards.

There may be specific reasons you are keeping an array of what’s been played. But if that’s not information you need for your game play, you can eliminate the need for an array that keeps track of already drawn cards. You also wouldn’t need to reroll your random index if a card had already been drawn. It’s not very efficient. In theory, your reroll could cause an infinite loop if the random index chosen was always a card that had already been drawn. Using the shuffled deck I described above avoids that.