Writing a High Score to a external Text file?

Good Morning,
What I would like to do is to have a way to save my high score or fastest time within my game to a seperate HIGH SCORES display screen.
I have a timer in my game & I assume that the way to do this has something to do with capturing the end time and writing that particular time to an external text file which is then read by some kind of command. Then whatever was written to the .txt file is then gotten and displayed on some kind of window graphic etc.

I would very much appreciate any help or direction someone could give me in this matter. Once done I could post up an example for other newbies like me to have as a ref since this seems like a neat feature for games. Thanks in advance…

If I understand you right (past midnight local time), then you have one process which opens/owns the main window), and another separate process which opens/owns the high-score window. The second process runs either on the same computer or on another one. So what you are looking for is a simple and lightweight way of inter-process communication.

One process storing the information on disk and the other reading it is certainly a way to do so, but I think there might be problems with possible simultaneous access to the file by both processes. Nothing that can’t be solved I believe. Another way would be to work into Panda3D’s network code, but this might be an overkill in your case.

Here is what I would suggest: Use Pyro (Python Remote Objects). Pyro is a simple and lightweight middleware for IPC similar to CORBA or RMI. Don’t get afraid of these terms, Pyros is really easy to use. Here is a link to an example which already solves your problem if I got you right. Just replace the joke message with your highscore.

http://pyro.sourceforge.net/example.html

enn0x

Wow ennoX, this a great example for something that I may want to implement later.
I apologize for not really being clear. This is probably more complex than what I need.

I guess what I meant to say was is there a way to run you game from the computer and save your score or timer info? I would prefer it not be a server or need players to create an account just a basic player name maybe. Thanks for your reply though ennoX, great link.

Using dictionary would be great :
{‘player1’:score, ‘player2’:score, ‘player3’:score, …}
and save it to disk using pickle or cPickle class (Python built-in). Search the forum for “serializ*”.

Thanks for the tip…I saw your posts earlier on serializing (forgive me I am new to this area) is this the terminology for “writing to disk”. I tried some google searches but never got a good definition. Thanks again,

Pickle is a pythonic term for serialization. This is the process of taking an Object (with it’s Attributes and Behaviors…i.e Methods, funcs, variables, etc) and converting it into a byte stream that can be saved to disk.

not 100% sure about this but Streams may also be used with Sockets (i.e Over the Internets :slight_smile: )

so, theoretically, you could Pickle from afar.

CPickle is an implementation written in C that is (reportedly) 1000X faster.

docs.python.org/lib/module-pickle.html

Thank you. That is what I needed to know. Since I am not that versed in Python it helps to learn the terminology so I can search definitions better. Thanks again.

Python is so much fun to program in it might be worth your while to disengage from Panda3D development and focus primarily on Learning Python for a while. You can learn an awful lot in a few weeks Things in Panda will make ALOT more sense if you have a good understanding of how Python works.

There are a few free resources. the Free book “Dive into to Python” is awesome. you can d’load it in PDF, XML, HTML, etc.

“Learning Python” from O’reilly is excellent. I recommend them both.

Hey Liquid7800,

I just got done solving a similar problem. Not being a programmer myself, I just invented a way, but it works:

I had the output write a new python file from scratch, created a class and filled the class with the values I needed. That way when the file was loaded again it could load the class as a python module. Worked perfectly. I pulled all the data I needed from the module and could save on top of it.

@bradford6: Thanks for the idea …I really am going to take you up on that, especially if it will make more sense when doing Panda3D stuff. Thanks for the link…I’ll search for that book.

@mavasher: I like your invented version…logically it makes alot of sense! However based on my reply above I am not that versed in many Pythonic structures…although I understand the OOP principles…Would you be able to post a small sample of what you are describing?..If not I appreciate the outline structure you gave.

regards

diveintopython.org/
or if you like to hold a copy in your hands the old fashioned way:
amazon.com/exec/obidos/ASIN/ … intomark20

or, if you want to be more ‘pythonic’ enter this (as a single line) in your python interpreter:

from webbrowser import open_new as fooblitzky ; fooblitzky("www.diveintopyth
on.org")

@bradford6: Thanks! Much appreciated

…LOL

Here’s the test code I wrote before I integrated it into my game.
I basically copied some of the plugin code from chombee’s steer code.
Hope this makes sense:

import os,math,sys,random,imp

from pandac.PandaModules import * 
import direct.directbase.DirectStart
from direct.gui.OnscreenText import OnscreenText 
from direct.gui.DirectGui import *

class OutputSaved:
	def __init__(self):
		self.save()
		self.DirectoryUsers()
		base.setBackgroundColor(0,0,0)



	def save(self):
		name = "Peter"
		saved = open('saved/'+ name + '.py', 'w')
		saved.write('class SavedGame:\n')
		saved.write('	def __init__(self):\n')
		saved.write('		self.name = "'+name+'"\n')
		#write the number of credits the player has.
		saved.write('		self.credits = ')
		value = 10000
		v = str(value)
		saved.write(v+'\n')
				
		#write the number of missions completed
		saved.write('		self.missions = ')
		mission = 1
		m = str(mission)
		saved.write(m+'\n')
		
		
		
	def DirectoryUsers(self):
		self.users = []
		self.user = 0
		for file in sorted(os.listdir('saved')):
			if file.endswith('.py'):
				modulename = file[:-3]
				try:
					file, filename, description = imp.find_module(
						modulename, ['saved'])
					Savedmodule = imp.load_module(modulename,
						file, filename, description)
					if hasattr(Savedmodule, 'SavedGame'):
						#print 'Instantiating plugin from %s' % Savedmodule
						try:
							self.users.append(Savedmodule.SavedGame())
						except Exception, e:
							print 'Error %s' % e
					else:
						print 'No saved game in %s' % Savedmodule
				finally:
					if file:
						file.close()
		
		
		#print out
		counter = 0
		for i in self.users:
		#	print "Name", i.name
			counter = counter + 1
			bk_text = i.name
		
			textObject = OnscreenText(text = bk_text, pos = (-.3,.5-(.07*counter)), 
				scale = 0.07,fg=(0,0.5,1,1),align=TextNode.ALeft,mayChange=1)	
			
		
		Entry = DirectEntry(text = "" ,scale=.1,command=self.compare,initialText="", numLines = 1, rolloverSound = None)
		
	def compare(self, text):
		ThisOne = None
		for user in self.users:
			if user.name == text:
				ThisOne = user
				print ThisOne.name
		return ThisOne

After you run the code look for the .py files. You’ll probably need to create a subdirectory called “saved” for this example.

mavasher Thanks, yes this does make sense. Thats a very creative way to do a high score. One question though, if this were implemented in a “packaged” game would the user need to create a SAVED subdirectory or could one create the folder on the fly in python during an “install” .

Nice example! and thanks for sharing.