Save/Load txt

Hey all,

Does anyone know how to save and load data into or from a txt file?

Thanks ^^

search for open ,read, write functions in python. The official manual of python is quite explicative

Thanks. ^^ It looks a lot like php lol.
Only 1 more question, is it possible to in stead of read from map C:/program files/etc/etc, just the map that the blend file or exe file of the game is in.
Because if other people download my blend and save it to another map the blend won’t know what map he’s talking about.

Something interesting that I found is that on Script controllers (not set to module) the script is stored as a single string. If you format your text file inside the triple quotation marks of a Python block comment it will compile just fine and can be accessed through the script controller as a single string using cont.script

This way reduces the need for external text files in BGE. :slight_smile:

Wh… what?? xD
How do I do that? :stuck_out_tongue:
It might be my limit knowledge in English as my knowledge in python but I don’t really understand what you mean. :stuck_out_tongue:

A simple *.blend printing the contents of a text file that is stored internally:

text_from_BGE.blend (447 KB)

Just run the game and it will print whatever is in the text file.

You can iterate through the string and extract the information in there however you want. :slight_smile:

Shit, the problem is that I can’t instal blender 54 on my computer. And I prever working in 49b.

Oh yeah… doesn’t seem to work in 2.49b :confused:

Let me have a look…

Edit:
text_from_BGE_249.blend (129 KB)

Sorted :slight_smile:

Sure ^^ Take your time lol

Cheers, FunkyWyrm, that’s useful to know!

Just an alternative method:

When using open(‘file.txt’) if you don’t specify a file path, then python will assume that the file you’re trying to access is in the current working directory, ie, the directory the .blend is being run from. Also, if the file doesn’t exist it will create it. Used in conjunction with try and except you can check if a file exists and if it doesn’t create it fill it with defaults. This way the external file doesn’t need to be supplied with the .blend/.exe

Off the top of my head…


# check a file exists and open it
try:file = open('text.txt', 'r')
file.readline()
# ... do something with the data, numbers will need converting from strings
# to integers with int()

file.close() # Don't forget to close it!
# If the file doesn't exist, create it and fill it with data
except:file = open('text.txt', 'w')
file.write('fill it with information 
')
# Note, that write only takes strings so data needs to be converted using str()
file.close()

Hope this helps!

Just a bump since I edited my previous post to include a functioning 2.49b *.blend and the edit might get lost. :wink:

Edit:

battery is definately doing it the ‘correct’ way. Thanks for posting that, I’m really poor at loading/parsing/closing files. :o

HAHAHA! Potential script mayhem going on here…

text_from_BGE_2.blend (447 KB)
text_from_BGE_249_2.blend (130 KB)

You can actually edit scripts that are running in the game engine. :spin:

This leads to all sorts of mental possibilities. I considered the possibility that scripts could be edited in this way, but the file above proves it.

Scripts that edit scripts… Self-editing algorithms… evolutionary programming… WOW! :D:eek:

Interesting find. Though it can make a script difficult to read, hence why self modifying code is used in obfuscation, useful for those who want to protect their scripts. I’ve also read that it can be used in optimising - like if a certain state is set remove unnecessary code such as removing if checks that will constantly return false.

I’ve had to deal with this problem before, I have had this script in one of my first GLSL powered games since 2.48 (though was updated at a later date).

This assumes you have a property where each scene is assigned a number and a property to use as the score. (This actually isn’t 100 percent my script, it was based on a script in a very old thread in the depths of this forum section, also note that this assumes the .txt file is opened in the same Blender build that it is created in (which shouldn’t be a problem if the game was in .exe form and was kept in the same location))

Save


ControllerPlane = GameLogic.getCurrentController()
objPlane = ControllerPlane.owner

#variables for the Count property (for the levels)#
PlaneText = objPlane['Count']
stringPlaneText = str(PlaneText)

#variables for the current score (the PlantText variable already unloaded the level number, so it's safe to re-assign)#
PlaneText = GameLogic.Text
stringPlaneScore = str(PlaneText)

#Write to txt file the count variable for storing the level#
out_file_L = open("test2.txt", "w")
out_file_L.write(stringPlaneText)
out_file_L.close()

#write to txt file the current score at the end of the level#
out_file_S = open("test.txt", "w")
out_file_S.write(stringPlaneScore)
out_file_S.close()

#read level no. file#
in_file_L = open("test.txt", "r")
txtText_L = in_file_L.read()
in_file_L.close()

#read score file#
in_file_S = open("test2.txt", "r")
txtText_S = in_file_S.read()
in_file_S.close()

Load


ControllerPlane = GameLogic.getCurrentController()
objPlane = ControllerPlane.owner

#Load Level no. file#
in_file_L = open("test2.txt", "r")
txtText_L = in_file_L.read()
in_file_L.close()

#Load Score file#
in_file_S = open("test.txt", "r")
txtText_S = in_file_S.read()
in_file_S.close()

#Assign Level no. and score to those in the files#
GameLogic.Text = int(txtText_S)
objPlane['Count'] = int(txtText_L)