Removing element from Array, still left with duplicates of items in it

I’ve read through other similar questions here, but I can’t seem to nail down what it is that I’m doing wrong here.


I’m attempting to make a simple card-draw feature in a project.

Inhand is a an array with 5 elements that are empty.
Deck is an array of 10 different cards (dataAssets).
LocalDrawDeck is a local copy variable to the DrawCards function.

When Called, the DrawCards function sets the LocalDrawDeck to be the same as Deck.
Then iterating through the InHand array, I want to pull a random element from the LocalDrawDeck and set the value of it to occupy the space in InHand, before removing it from the LocalDrawDeck, repeating the process until InHand is filled. At the end, I set Deck to be the same as LocalDrawDeck.

The idea being that Deck depletes over time as the player is pulling more cards.
However, I keep getting duplicate cards in the InHand array when displaying drawing them on screen, and I can’t for the life of me figure out what it is that I’m doing wrong.

Below is the SpawnCards function that reads from the InHand array and feeds the cards to my Viewport-Widget.

I appreciate any help that anyone can provide.

1 Like

add extra data/variable to your card ex: ID number, if the ID is repeated re-randomize the array untill you get another unique ID.

2 Likes

Hi there,
Typically, before the “for each loop”, get the reference for the “Inventory Layout” and connect it to a node called “clear children”. That should work.

2 Likes

The cards in Card-array are all unique - so if the remove-item from the LocalDrawDeck actually works that shouldn’t strictly be necessary, should it?

I’m wondering if the issue is tied to the actual removal from the LocalDrawDeck, rather than what actually gets put in it. Rather - why is it even possible to add the same card twice, some times thrice if the element is removed between each add to InHand-array.

Attempted this - I still get duplicates of cards displayed on screen regardless.
I’m thinking the issue seems rooted in the “Remove” command of the LocalDrawDeck-function.

For some reason it seems as duplicate cards get added to InHand even though they should be removed before passing a new card.

If you share the rest of your code, it would be better for understanding where your issue is being generated. Typically, these duplicates in UI happen every time you open the widget without “Clear Children” being called in the beginning of the function. Now, if your widget is always on screen (already added to the viewport once), so I’d suggest investigating your code that updates the arrays elements.

I should’ve mentioned this to begin with, sorry. Yes, the UI only gets generated once - it’s always on screen.

I only have the two functions (DrawCards and SpawnCards) that I’ve posted above that perform any sort of logic. The Card UI that gets added to the viewport only triggers a simple animation when being constructed.

I have however stumbled across a bunch of issue directly related to manipulating an array with a ForEachLoop during runtime. Issue seems related to arrays dynamically rescaling when removing elements, although this still doesn’t really solve my problem.

I did however stumble across the ability to just shuffle an entire array with the “shuffle”-node.
As I’m only trying to get 5 random cards at the first draw (when the Viewport is initially instantiated) I can just grab the first 5 elements of the shuffled array - when completed, I compare the InHand-array with the LocalDrawDeck-array, and remove the matches before setting that as Deck-array.

Thank you for the provided input!

Thank you for sharing this explanation. That clarifies that the issue might be related to your array logic. Have you ever tried to use set array element to reorganize and adjust the size of the array (Size to Fit)? Also, using a Random Array Element can be used to draw a single card from the array, rather than using shuffle then get array element at index, for instance.

1 Like

Perhaps the issue is because at a given moment, you have arrays with different sizes (length). You are executing a for each loop with Local Draw Deck that changes simultaneously with the set array element. Perhaps you need another array element as auxiliary variable.

1 Like

Will definitely keep this in mind going forward. The project is less of a fixed design and more of a playground for me to attempt solving different issues around card-draw and -play logic.

Thank you for the tip! :smile:

I think you might be right. I should be double-checking my LocalDrawDeck with an auxiliary variable to determine if a change has happened in the size of the local variable before re-entering the loop.

So, from the Random Integer and after the set array element, add to an array of integers. And disconnect remove index. From your For Each Loop “Completed”, add another for each loop, take the array of integers, and connect remove index from local draw deck.