item inventory script

Programmer new to Python…
Attempting to create a text based item bag (just strings) for an rpg.

<edit>

import bge
cont = bge.logic.getCurrentController()
own = cont.owner
sens = cont.sensors['sensor']
st = own['Text']
print ("
Reading your text file.")
text_file0 = open("write_it.txt", "r")
s = text_file0.readlines()
if sens.positive:
s.append(own['Text'])
text_file1 = open("write_it.txt", "w")
lines = s
text_file1.writelines(lines)
print (s)

okay, I got my script down to this and it works great for storing and reading. However, I’ve been trying to apply it to a second plane (text prop) that will display the items. any ideas?

what are you trying to acheive?
are you trying to have a new line per item?

here’s the .blend

http://www.sendspace.com/file/7lqb0v

I’ve got a discription of what I’m trying to do in the text window.]

(I’m not sure if I packed the ‘font .tga’ correctly so you may need to load up your default.)

fixed the problem.
add this after own = cont.owner
(at the top of the script)
path = bge.logic.expandPath(’//write_it.txt’)
then change all the bits which have “write_it.txt” (with the quote marks)
and put “path” (without the quote marks.)


import bge
cont = bge.logic.getCurrentController()
own = cont.owner

st = own['Text']

#get relative path
path = bge.logic.expandPath('//write_it.txt')

#define sensor
sens = cont.sensors['sensor']
print ("
Reading your text file.")
text_file0 = open(path, "r")
s = text_file0.readlines()
if sens.positive:
    s.append(own['Text'])
    text_file1 = open(path, "w")
    lines = s
    text_file1.writelines(lines)
    print (s)

Doesn’t seem to work. I may have it plugged in wrong so I’ll take a closer look. And Thanks :slight_smile: