Ren'Py Trying to create a new list of object with copy/deepcopy but it's not working

ChickenChaser

New Member
Game Developer
Mar 22, 2022
12
74
Hello. I'm at my wit's end, so I'd appreciate it if you could enlighten me as to why this isn't working. For a bit of context - I have a list of object Card called drawnCards. The Card object has two fields name and is_selected. This "is_selected" field, depending on it's state, will show different visuals. So I'm trying to copy over cards from drawnCards to another list called usedCards. I'm trying to copy over the objects by using copy/deepcopy or just creating a new instance of Card by using a copy builder method, but it's just not working.


Python:
def cloneCard(self):
    return Card(self.name,  self.selected_card)

def copyCard(self):
    newCard = deepcopy(self)
    return newCard

def  addCardToHistory(card):
    if len(usedCards) < 6:
        usedCards.append(card.cloneCard())
        #usedCards.append(card.clone())
    else:
        usedCards.pop(0)
        usedCards.append(card.cloneCard())
        #usedCards.append(card.clone())
to use deepcopy I've used "from copy import deepcopy" at the begining of the python code init

The behavior is still the same no matter which of the two methods I use. When I change the name/is_selected value it reflects on the objects in both lists,even though supposedely i'm not copying them by reference. I'd appreciate any help/feedback or ideas. Thanks!
 
Jun 15, 2018
414
461
I dont really understand why you are trying to copy anything at all but I see that ur not using the copyCard method at all which should make a copy of the card. Also your clone card isnt really cloning the card. It is returning original card. So ur basically adding the original card to the usedCards list.

If you want to add a copy of the card to the used cards list, you need to call the method copyCard, save it in a variable, then append it to your usedCards list.
 

ChickenChaser

New Member
Game Developer
Mar 22, 2022
12
74
I dont really understand why you are trying to copy anything at all but I see that ur not using the copyCard method at all which should make a copy of the card. Also your clone card isnt really cloning the card. It is returning original card. So ur basically adding the original card to the usedCards list.

If you want to add a copy of the card to the used cards list, you need to call the method copyCard, save it in a variable, then append it to your usedCards list.
Hey, thanks for your answer and sorry, should've double checked what I copied over. Essentially I've already tried both methods that I've shown.

Python:
def cloneCard(self):
    return Card(self.name,  self.selected_card)

def copyCard(self):
    newCard = deepcopy(self)
    return newCard

def  addCardToHistory(card):
    if len(usedCards) < 6:
        usedCards.append(card.cloneCard())
        #usedCards.append(card.copyCard())
    else:
        usedCards.pop(0)
        usedCards.append(card.cloneCard())
        #usedCards.append(card.copyCard())
I've tried both functions and both aren't working, they still copy over the object by refens. It feels like deepcopy just isn't working. Regarding why I do this - I just need a new list with specific objects from the first one where if I change the values of the fields in the first list it won't affect the second one.


Also your clone card isnt really cloning the card. It is returning original card. So ur basically adding the original card to the usedCards list.
Won't the return Card() in copyCard essentially return a newly created instance of the class though?
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,299
15,167
So I'm trying to copy over cards from drawnCards to another list called usedCards.
Why ? What is the interest you think to get by doing this ?


The behavior is still the same no matter which of the two methods I use. When I change the name/is_selected value it reflects on the objects in both lists, [...]
Hmmm, no, it don't...
console.jpg


What mean that the error is somewhere else. My guess goes for either the class __init__ method, or the object creation.