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~
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!
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.