bge script for loading multiple file options

im relatively new to scripting ive basically been modifying other peoples scripts to suit my needs haha and its worked well i might add. ive spliced together a script that saves 2 txt files in a directory: one saying what the most recent name saved was and the other is that name that was saved. as of right now its not saving any data because i havent got that ffar. I want to have a solid save and load system first. so my question is can someone give me a quick way of making a script that says the names of the files in the save folder and i can usually come up with a way to take that data and send it to a text planes “Text” property so the player has all the save files to choose from?

this is my save script ive been using:

This creates a file named by the object “writer”'s

property “Text” and makes a recentsave file

cont = GameLogic.getCurrentController()
own = cont.owner
objects = GameLogic.getCurrentScene().objects
entry = objects[“OBwriter”]
name = entry[‘Text’]
toggle = own[‘save’]
if name == “”:
print “Error: Try writing something next time!”
else:
print “init_save: " + name
prefFile = open(”./saves/" + name + “.sav”, “w”)
prefFile.write(“file OK” +"
")
prefFile.write(’!!this is test data!!’)
prefFile.close()

print “init_recentsave”
prefFile = open("./saves/" + “[recentsave]” + “.sav”, “w”)
prefFile.write(name)
prefFile.close()

hehe, interesting, I made these scripts two days ago !

Oh yes, I know that we have Blender Registry and globalDict to save but I got in troubles with them, so I made on my own.

I used bge with 3 keys:

  • “S” key will trigger the second script to save preferences on disk
  • “L” key will trigger the third script to load preferences from disk

load this script into a Text Window and rename from Text.001 to preferences.py, that way we can import this module from others scripts


# Script to load and save your preferences to disk
#
# by Lepes 2010
# License: do what you want with it

import GameLogic as gl
from types import StringType

def savePref(filename, main):
	""" save a dictionary to disk, you can neste dictionaries and python lists
	
	filename -> Filename to save your data, if absolute path is not supplied, it is saved on the same folder than .blend file
	main     -> dictionary to save, it can contain simple types (int, float, string), lists and dictionaries
	"""
	saveFile = open(filename, "w")
	if saveFile:
		for mkey in main.keys():
			if isinstance(main[mkey], StringType):
				prefijo = '"'
			else:
				prefijo = ''

			saveFile.write("%s->%s
" % (mkey, prefijo + str(main[mkey]) + prefijo))
			
		### Cerramos el archvo
		saveFile.close()
	else:
		print "ERROR: File: '" + filename + "'  couldn't be opened with write rights"		


def loadPref(filename):
	""" load from a file your preferences returning a dictionary
	
	filename -> filename to load preferences from. If absolute path is not provide, it will use the same path where .blend file is placed.

	return: dictionary (posible empty)
	"""
	loadFile = open(filename, "r")
	armas = {} 
	if loadFile:
		lineas = loadFile.readlines()
		loadFile.close()

		for linea in lineas:
				claveYvalor = linea.strip().split('->')
				if len(claveYvalor) == 2:
					armas[claveYvalor[0]] = eval(claveYvalor[1])
	return armas

Now the way to save your preferences:


import GameLogic as gl
from preferences import savePref, loadPref

# basic weapons to save on disk
weapons = {
'1': {'trigger_key': 'c', 'name': 'mortal ray'},
'3': {'trigger_key': 'v', 'name': 'simple gun'},
'ids': [1, 3]

}

sce =  gl.getCurrentScene()
cube = gl.getCurrentScene().objects["OBCube"]


# create a property on the cube to hold data while 
# game is running

if not 'weapons' in dir(cube):
	cube['weapons'] = weapons

cube['weapons']['cubepos'] = cube.position

# Example to show how to save new things on the cube and then, to files
cube['weapons']['players'] = 3
cube['weapons']['player_name'] = 'pepe grillo'
cube['weapons']['inventary'] = ['pale', 'gun', 'book']


savePref('weapons.txt', cube['weapons'])

print "properties saved"

… and the way to load the file:


import GameLogic as gl
from preferences import loadPref

sce =  gl.getCurrentScene()
cube = gl.getCurrentScene().objects["OBCube"]


cube['weapons'] = loadPref('weapons.txt')

# if position of the cube is loaded from file, update cube scene position
if 'cubepos' in cube['weapons']:
	cube.position = cube['weapons']['cubepos']
	print "
cube position changed to: ", cube['weapons']['cubepos']	

print "properties loaded!"

I translated from spanish to English on the fly, I hope I didn’t mispelled something :confused: