inventory items

A place to discuss things that aren't specific to any one creator or game.
Forum rules
Ren'Py specific questions should be posted in the Ren'Py Questions and Annoucements forum, not here.
Post Reply
Message
Author
bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

inventory items

#1 Post by bozance »

Hello,

Currently I do the following to display inventory items:

1. In INIT section, I have $inv=[]
2. To display inventory near bottom of screen, I entered:
screen statsinv:
hbox:
text("{color=#5f9ea0}Inv: %s{/color}" % inv)
3. When an item is added, I have $inv.append("watch") for example.

The Inventory starts out actually displaying the brackets, and puts a single-quote around each item.

So the game displays the following:
Inv: ['watch', 'ring']

I just want it to display
Inv: watch, ring

Is there a way to do that?

Thanks

Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: inventory items

#2 Post by Jake »

bozance wrote: I just want it to display
Inv: watch, ring

Is there a way to do that?
Since 'inv' is a list, when you interpolate it into a string, it uses Python's default string representation of a list, which includes the square brackets and the quotes.

One thing you can do is use the 'join' method of a string, which takes a sequence (like a list) as a parameter and joins all the items in the list together using that string between each one. So, for example:

Code: Select all

>>> inv = ['watch', 'ring']
>>> print "Inv: " + str(inv)
Inv: ['watch', 'ring']
>>> print "Inv: " + ", ".join(inv)
Inv: watch, ring
The 'join' function is actually a method on a string instance itself - so you need to provide the string you want to join, then a dot, then the function name 'join' and then the parameter list, which is a little different from most function calls. So:

Code: Select all

  ", ".join(inv)
is using the string instance:

Code: Select all

", "
and calling the method join with 'inv' as a parameter
Server error: user 'Jake' not found

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

Re: inventory items

#3 Post by bozance »

Thanks--though I'm not sure exactly where to put that in my script.

If I have the following in the INIT section, where exactly would I put the join stuff? Can you enter it in where it would belong in the example below?

Sorry if my question is too basic. I'm an English major and anything beyond jump and basic if statements and I'm screwed.

Init:
screen statsinv:
hbox:
yalign 0.95
xpos 0.01
xfill True
text("{color=#5f9ea0}Inv: %s{/color}" % inv)

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

Re: inventory items

#4 Post by bozance »

Ah--I just had to enter

text("Inv: "+", ".join(inv))

at the bottom of the hbox definition.

Thanks again for the help... I wish there were an in-person training course for Renpy!

Post Reply

Who is online

Users browsing this forum: No registered users