what is the opposite of "append"?

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
bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

what is the opposite of "append"?

#1 Post by bozance » Thu Mar 24, 2011 9:34 pm

Hello,

Does anyone know what the opposite of "append" is?

In my inventory here is how I add a gun with bullets:

$mp.inv.append("gun (%d/6)" % ammo)

Not sure how to remove it. Please help!

User avatar
Megaman Z
Miko-Class Veteran
Posts: 829
Joined: Sun Feb 20, 2005 8:45 pm
Projects: NaNoRenO 2016, Ren'Py tutorial series
Location: USA
Contact:

Re: what is the opposite of "append"?

#2 Post by Megaman Z » Thu Mar 24, 2011 10:28 pm

based off of official documentation, the exact opposite of append is pop() (which, given no parameters, removes the last entry from the array, but if given a parameter, removes the specified item in the array. remember arrays count from 0 upwards - "1" is the SECOND item in an array)

Do note that you're going to have to keep track of what item is where in the array, and that's going to take a bit of trial and error in some cases.
~Kitsune Zeta

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: what is the opposite of "append"?

#3 Post by Aleema » Thu Mar 24, 2011 10:45 pm


bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

Re: what is the opposite of "append"?

#4 Post by bozance » Thu Mar 24, 2011 10:49 pm

Thanks! I am a bit clueless with this stuff, so I'm not sure how to approach this.

Can you provide an example of how I might implement this? Doesn't have to work, exactly... I just want to know what it generally would look like.

$mp.inv.pop("gun (%d/6)" % ammo)?

I read the documentation but I suck at comprehending it.

bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

Re: what is the opposite of "append"?

#5 Post by bozance » Thu Mar 24, 2011 10:53 pm

Actually based on your response I tried $mp.inv.pop(0), which worked!

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: what is the opposite of "append"?

#6 Post by Aleema » Thu Mar 24, 2011 10:57 pm

Aw, c'mon. My answer was totally right. Give me some points. :P

bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

Re: what is the opposite of "append"?

#7 Post by bozance » Thu Mar 24, 2011 11:01 pm

Does remove really work? I couldn't get that going.

User avatar
Megaman Z
Miko-Class Veteran
Posts: 829
Joined: Sun Feb 20, 2005 8:45 pm
Projects: NaNoRenO 2016, Ren'Py tutorial series
Location: USA
Contact:

Re: what is the opposite of "append"?

#8 Post by Megaman Z » Thu Mar 24, 2011 11:02 pm

bozance wrote:Thanks! I am a bit clueless with this stuff, so I'm not sure how to approach this.

Can you provide an example of how I might implement this? Doesn't have to work, exactly... I just want to know what it generally would look like.

$mp.inv.pop("gun (%d/6)" % ammo)?

I read the documentation but I suck at comprehending it.
yeah, it takes a bit of work to figure some things out.
bozance wrote:Actually based on your response I tried $mp.inv.pop(0), which worked!
...be careful on that one - pop(0) removes the first item in the array, regardless of what it is.

remove(x) removes the first occurrence of x in the array. if you have an array [5, 3, "beer", 5, 2, "beer", "women"] and use remove("beer") and remove(5), the resulting array (after both are done) will be [3, 5, 2, "beer", "women"]
Aleema wrote:Aw, c'mon. My answer was totally right. Give me some points. :P
Yeah, I missed remove when I was glancing through the page. my bad on that one.
~Kitsune Zeta

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: what is the opposite of "append"?

#9 Post by Aleema » Thu Mar 24, 2011 11:04 pm

It totally works, I use it myself. It might be fickle with your variable in there, though. Pop() is going to rely on the placement of your item in the list, and if you don't know the place, then Remove() works because it simply finds it for you.

bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

Re: what is the opposite of "append"?

#10 Post by bozance » Thu Mar 24, 2011 11:14 pm

Excellent. I'm going to put the gun and ammo counter in a separate frame, apart from inventory. Remove works fine with just regular strings, so I'll stick with those for inventory items.

Thanks for all your help.

Post Reply

Who is online

Users browsing this forum: deltadidirac