Function Parameter

I was attempting to create a function that would handle the rollover for a cardgame. Turn the discard pile back into the deck pile.
To do this I created 2 global arrays:

var player_deck: Array = [1,2,3]
var player_discard: Array = [ ]

~code that shuffles cards around and appends them to the discard array~

Then I created a function using parameters:

func deck_rollover(deck, discard):
		deck = discard.duplicate()
		discard.clear()

func deck_check():
	if player_deck.is_empty():
		deck_rollover(player_deck, player_discard)

This did not do what I wanted to: update the global var. It worked “fine” in the function though, it understood that deck = player_deck and so on, so it “worked” inside the function, but did not update the global var.
I am obviously pretty new, so I did some research and found that it would not update the global because the parameter was being passed by value(?) rather than by reference(?), and all the work was being contained within the function.
I have been led to believe that passing an int or a float are primitive data types and would result in this scenario, but there were workarounds that involved encapsulating those primitive variables in arrays or other structures. I thought I was using an array as the parameter, so I am just a little confused as to why this does not work.
Since then I just made specific functions (there’s only 2 players), but I would like to make more flexible functions in the future. Any advice or clarification is much appreciated, thanks!

Are you having problems with discard not clearing globally? Or are you using an autoload?

You are trying to assign a new reference to a parameter, which isn’t going to work. You simply need to

#
# The deck array is empty, so you just need to copy
# the discard contents to the deck array.
#
deck.append_array(discard)
discard.clear()

That should do it.

Before the function

player_deck = []
player_discard = [1,2,3]

In the function

deck = discard.duplicate

so 

player_deck = [1,2,3]
player_discard = [1,2,3]

then

discard.clear()

so

player_deck = [1,2,3]
player_discard = []

Then I ran in a different function or elsewhere in the script

print(player_deck)
print(player_discard)

And it returned

player_deck = []
player_discard = [1,2,3]

So yes, in a way, the global discard and deck vars are not clearing or populating. The operation that I was attempting to perform in the function wasn’t “sticking”, the player_deck and player_discard vars were not being updated properly.
Sorry I don’t know a better way to describe it. For what it’s worth, I am using an autoload to store the deck.
Thank you for the quick response. For what it’s worth, I think this is more of a “theory” question, I solved the issue internally, and soapspangledgames has also provided a wonderful answer. I’m mostly looking for a little insight as to why, if you have the time to break it down for me.
Thanks again!

Thanks for your advice! If you have a minute, can you break down the “new reference” bit of your reply please? The workaround is wonderful, I’m just hoping to better understand how to implement parameters effectively in the future.
Thanks so much!

You are passing deck and discard as parameters to a function. Those parameters are created when deck_rollover() is called, and are destroyed when deck_rollover() returns.

If you reassign deck a new reference [as you did with deck = discard.duplicate()], then whatever new reference deck holds will be destroyed when deck_rollover() finishes.

deck and discard are effectively local variables for purposes of reassignment. You can change the contents of the arrays, but you cannot change the underlying array references since those new references will be destroyed once the function completes.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.