loading external .txt files into BGE?

hello guyz c:
I want to know how to make a game property equal to the content of an external .txt file, is that possible?

Yes it is possible, and actually rather easy. You would use the built-in “open” function. I don’t have much time to write this, so rather than explain, I will just post the code:

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

# Set the path variable, the double slash makes the directory local to the current .blend
# this example looks in a folder called "data" that is located in the same directory as the .blend
path = logic.expandPath("//data/")

file = 'gameinfo.txt'

# Call the "open" function, using the path that we constructed, and open in read mode
read = open(path+file, 'r')

#Read the text file, replace any returns with a blank space, then split into a list using the comma
line = read.readline().replace('
','').split(',')

# Close the file to keep it nice and tidy
read.close()

# Assign the property "prop" to be equal to an integer value of the first item in the list
own['prop'] = int(line[0])

YESSSS thank you so much!!
btw, I like your LPs c:

@ kendrick1397:

<i>#Read the text file, replace any returns with a blank space, then split into a list using the comma</i>
line = read.readline().replace('
','').split(',')

Doesn’t this read only the first line of the text file? To remove returns I would think you have to read all the lines and then iterate through them to append to a list:

lines = read.readlines()
read.close()
list = []
for l in lines:
    list.append(l.replace('
',''))

@ ehabcharek:

If you want to convert a string to a list the eval() function will come in handy. Casting every string will remove the return (’
') automatically.

example:

Let’s say your ‘file.txt’ contains the following data:

  • True
  • 1.0
  • 999
  • String
  • [0.0, 1.0, 0.0]
from bge import logic

def loadProp(cont):

    file = logic.expandPath('//file.txt')

    load = open(file, 'r')
    lines = load.readlines()
    load.close()

    own = cont.owner
    own['Boolean'] = bool(lines[0])
    own['Float'] = float(lines[1])
    own['Integer'] = int(lines[2])
    own['String'] = lines[3].replace('
', '')
    own['List'] = eval(lines[4])

    # test:

    for propName in own.getPropertyNames():
        print(own[propName])

For every object your could have a properties list:

  • ‘Monkey’, True, 1.0, 999, ‘String’, [0.0, 1.0, 0.0]
  • ‘Player’, False, 2.0, 100, ‘Ding Dong’, [0.707, 0.707, 0.0]
from bge import logic

def loadProp(cont):

    file = logic.expandPath('//file2.txt')

    load = open(file, 'r')
    lines = load.readlines()
    load.close()
    
    sce = logic.getCurrentScene()
    obList = sce.objects
    propNames = ['Prop1', 'Prop2', 'Prop3', 'Prop4', 'Prop5']

    for l in lines:
        list = eval(l)
        obName = list[0]
        for i in range(len(propNames)):
            obList[obName][propNames[i]] = list[i+1]

In this case you only have to evaluate once, so no individual casting is needed, which is very convenient.
Maybe all this info wasn’t necessary for you, then I hope it will be useful for someone else.

Here is a much cleaner/simple example!
My save script makes the external file to get encrypted too.

Press ‘S’ to save properties.
Press ‘L’ to load.

Attachments

untitled.blend (447 KB)

That’s great!

EDIT: If you dump data with the pickle module, will this clear the pickle module also? Do I understand correctly that the data will be stored only during the execution of the script (so one logic tick)?

EDIT2: Okay, dumb question. I read now that the pickle module is a conversion tool, so logically it is no dictionary. http://docs.python.org/dev/library/pickle.html