I need a script to save score and all priority in .text and load it
With Python!
You can open a file with:
file = open(bge.logic.expandpath('//relative/path'), 'w')
Note that the w tells python to open it for writing
We can then write to it with:
file.write('I want to put this in the file')
And finally close the file:
file.close()
Now we want to read it back:
file = open(bge.logic.expandpath(’//relative/path’))
No w because we are reading it
and we can get what's in it:
contents_of_file = file.readlines()
This is in a list which you have to iterate through if you want to parse it.
Moved from “Game Engine Resources” to “Game Engine Support and Discussion”
Write the data into a dictionary -> Add it to bge.logic.globalDict
then use the GameActuator to save to file/load from file
Indeed this involves Python.
how to write on a specific line? sorry english.
and dont forget expandpath should be expandPath (capital P)
> file.write('I want to put this in the file') add more of these to ad more lines or change the text to you likingsto read from file:
contents_of_file = file.readlines()
needs also this:
your_property = contents_of_file[0] # 0 for first line in file, 1 for second line, etc.
Thank you! ![]()
For the people who whants the script:
Read / load data from file
import bge
cont = bge.logic.getCurrentController()
own = cont.owner
Read text file
file = open(bge.logic.expandPath(‘//test.txt’), ‘r’) # r command to read only
file_data = file.readlines()
close the file
file.close()
and put file data into property
own[‘test’] = file_data[0].replace(’
‘, ‘’) # get line 0 and replace /n with a white space
own[‘test2’] = file_data[1].replace(’
', ‘’)# get line 2 “”
And the Save / store part:
import bge
open path to test.txt (if test.txt does not exist it will be made automatically)
file = open(bge.logic.expandPath(‘//test.txt’), ‘w’) # w command to write
write to text file
file.write('It works omg.
') #
to define a new line
file.write('test 1 2 3 4 5.
')
#closing the file
file.close()
Tested and working, have fun with it.