.remove() isn't removing an object from array

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Baseliner88
Newbie
Posts: 8
Joined: Wed Feb 06, 2019 1:31 am
Contact:

.remove() isn't removing an object from array

#1 Post by Baseliner88 »

Hi,

i got a simple random.choise and want to remove the picked one from the array...
But i get sometimes two same objects as result.

What am I doning wrong???

Inside of deck01[] are six objects (diffrent ones of cause).

Code: Select all

    $ deck01 = []
    $ deck01.append(karte1)
    $ deck01.append(karte2)
    $ deck01.append(karte3)
    $ deck01.append(karte4)
    $ deck01.append(karte5)
    $ deck01.append(karte6)
    
    $ savedeck = []
    $ savedeck = deck01			######### This is only for reconstructing the deck01 after the choices are done and the battle is over.
    $ held01 = renpy.random.choice(deck01)
    $ deck01.remove(held01)
    $ held02 = renpy.random.choice(deck01)
    $ deck01.remove(held02)
    $ held03 = renpy.random.choice(deck01)
    $ deck01.remove(held03)
    
For explanation, I got an array of six cards and want to draw randomly three out of there.
So it shouldn't be possible that I can draw two cards of the same kind. :?

Thanks for help in advance.

philat
Eileen-Class Veteran
Posts: 1910
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: .remove() isn't removing an object from array

#2 Post by philat »

Several issues:

1. http://foobarnbaz.com/2012/07/08/unders ... variables/ (adjust your expectations on how your deck declarations are working)

2. viewtopic.php?t=50496#p489027 (shuffling is probably easier to manage than picking and removing)

Baseliner88
Newbie
Posts: 8
Joined: Wed Feb 06, 2019 1:31 am
Contact:

Re: .remove() isn't removing an object from array

#3 Post by Baseliner88 »

Thank you for your fast reply.
It was an completely diffrent mistake by myself... I was testing and was filling, every round, the list with these 6 objects...
So .remove() is as well doing it's job. But shuffle is really more comfortable to use for.

Sorry for the stupid question here.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1461
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Discord: xavimat
Contact:

Re: .remove() isn't removing an object from array

#4 Post by xavimat »

I use shuffle and pop in a copy of the list to do this:

Code: Select all

define deck = [1,2,3,4,5,6]  # deck is a constant, so I use "define" here.
label start:
    $ deck2 = deck[:]  # deck2 is a copy of the original deck
    $ renpy.random.shuffle(deck2)  # shuffling as in real life card games.
    while len(deck2) > 3:
        $ card = deck2.pop()  # pop takes the last card, as in real life card games.
        "[card]"
    jump start
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

Baseliner88
Newbie
Posts: 8
Joined: Wed Feb 06, 2019 1:31 am
Contact:

Re: .remove() isn't removing an object from array

#5 Post by Baseliner88 »

Ok cool, thx for your answer.
Pop sounds interesting. I will give it a try just to test it out :)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Sugar_and_rice