another python question v2

ok, i made a high score system with python for my pinball game.
basically it saves the high scores to text files, then opens them again when it needs to.

import GameLogic as G
 
cont = G.getCurrentController()
own = cont.getOwner()
 
gameover = cont.getSensor("gameover")
 
text = open("data\high_score1.txt", "r")
current_score1 = text.read()
current_score1 = int(current_score1)
text.close()
 
text = open("data\high_score2.txt", "r")
current_score2 = text.read()
current_score2 = int(current_score2)
text.close()
text = open("data\high_score3.txt", "r")
current_score3 = text.read()
current_score3 = int(current_score3)
text.close()
 
text = open("data\high_score4.txt", "r")
current_score4 = text.read()
current_score4 = int(current_score4)
text.close()
 
text = open("data\high_score5.txt", "r")
current_score5 = text.read()
current_score5 = int(current_score5)
text.close()
 
scores = [current_score1, current_score2, current_score3, current_score4, current_score5]
scores.sort()
scores.reverse()
 
if gameover.isPositive():
 new_score = own.points
 scores = [current_score1, current_score2, current_score3, current_score4, current_score5, new_score]
 scores.sort()
 scores.reverse()
 print "scores", scores
 
text_file = open("data\high_score1.txt", "w")
new_score1 = str(scores[0])
text_file.write(new_score1)
text_file.close()
 
text_file = open("data\high_score2.txt", "w")
new_score2 = str(scores[1])
text_file.write(new_score2)
text_file.close()
 
text_file = open("data\high_score3.txt", "w")
new_score3 = str(scores[2])
text_file.write(new_score3)
text_file.close()
 
text_file = open("data\high_score4.txt", "w")
new_score4 = str(scores[3])
text_file.write(new_score4)
text_file.close()
 
text_file = open("data\high_score5.txt", "w")
new_score5 = str(scores[4])
text_file.write(new_score5)
text_file.close()
 
 

that all works perfectly, you can see how i open text files for writing and reading.

now i want to add an overlay scene to the ‘game over’ scene ‘if’ the new score is higher than the current high score. so when the game is over (when your number of balls left == -1), i open the text file “new_score” and write the score to that file.

import GameLogic as G
 
cont = G.getCurrentController()
own = cont.getOwner()
 
gameover = cont.getSensor("gameover")
 
if gameover.isPositive():
 score = own.points
 score = str(score)
 text_file = open("data
ew_score.txt", "w")
 text_file.write(score)
 text_file.close()
 print "new score recorded"

the problem is that i get an error saying that new_score doesnt exist. oh, but it does. im not making a mistake( not one that i can spot) it worked perfectly for the first part of code. where i opened all five of those.

see, heres a picture for proof

http://img130.imageshack.us/img130/8522/newscore8bi.jpg

im confused. i rewrote the second part of code i dont know how many times. it should work!

all of the files are in the ‘data’ subdirectory so thats why its text_file = open(“data\high_score2.txt”, "w blah blah

Find out what the score is before you write it to file. It may be nothing. Notice it says 0 kb for size, or just open the file with a text editor and see if there is anything in there. You really can’t get the error that the file doesn’t exist because it would be created if it didn’t. It would be best if you copied your error and posted it.

i do find out the score before i write it.

score = own.points
score = str(score)
text_file = open("bla bla… “w”)

with that w, it doesnt matter if there is anything in it or not when i write to it because it will just overwrite it.\

the thing is, its not writing to it because it says its not there…

Can you post the exact error message. It really does no good without posting it. For instance, is it the open line or , the write line, or the close line that causes the error. I meant find out what the score is as in :
print score. It may be your trying to write a file with nothing, or a null. I don’t know. Or give score a known value when you write the file to see if that’s the problem.

ok heres the error

http://img295.imageshack.us/img295/4480/error2zg.png

the first of the two almost identical erros is when i try to open the file to write the score to it. the second is when i open it back up so i can compare it to the previous high score.

i even tried writing a known value to the file, but i still got that error…

You need to put an extra back slash:

Try this line:

 text_file = open("data\
ew_score.txt", "w")

Remember that
is a new line character.

oh man, how can i forget!

thank you both very much :smiley:

I don’t know, really. Your code is identical to the other. It would be best to work with one file at a time if your having a problem like that. They might be getting called so close to the same time that one hasn’t closed, but it doesn’t seem like you’d get two errors that way.

ok that worked. i may as well ask another python question in here instead of making a new topic. im still very new to python in blender and also just python as a language so i run into these problems often :frowning:

ok now that i have my new score stored in a text file, i open it back up in the game over scene to compare it to the current high score. if the current high score < the new score, i want to add an overlay scene( just make an actuator true)

so heres my code for that

import GameLogic as G
 
cont = G.getCurrentController()
 
on = cont.getSensor("on")
add = cont.getActuator("add")
 
if on.isPositive():
 text_file = open("data\high_score1.txt", "r")
 current_score = text_file.read()
 current_score = int(current_score)
 text_file.close()
 
 text_file = open("data\
ew_score.txt", "r")
 new_score = text_file.read()
 new_score = int(new_score)
 text_file.close()
 
 if current_score &lt; new_score:
  cont.add = True
  print "new high score!!!"
 else:
  cont.add = False
  
 G.addActiveActuator(add, 1)

the tabs dont translate properly when i copy it onto the forums but you can still see the indents.

the problem is that it always adds the overly scene. it doesnt matter if the new score is higher or lower… the thing i know the least about is

G.addActiveActuator()

where should it go?
what is it for?

Be careful with using True or False in the GE. It gets ignored sometimes. At least it has for me in the past. Use a 1 and 0 instead, but that still won’t make it work as far as I can see. “cont.add” would be making a variable for the controller object. I don’t think that’s a good idea. If you want to assign a variable, give it to the owner. It’s much safer, or at least, easier to understand. You should be able to just:


 if current_score &lt; new_score:
     GameLogic.addActiveActuator(add,1)

Technically though, it would work if you put:


if cont.add:
   GameLogic.addActiveActuator(add,1)

It just seems wierd to make a controller variable called add for a flag for add.
As far as what is it for, it just adds the actuator to the game loop if you use a 1 or True, else it takes it out of the game loop if you use a 0.

I missed that /n thing. That has gotten me before to, I wish they would have done something that wouldn’t interfere like that for an escape.

Yes do not use “True” or “False”. Use 0/1 or g.KX_TRUE/g.KX_False

Personaly I prefere the 0/1 option. But even if this True/False works for you on this script, I would not make a habit of it, because almost everything I write in python has issues when True/False are not actualy defined properly, but 0/1 will always work the way you want.

Huh? Since python2.4 True and False are properly defined in python. Unless you are using the python2.3 version of blender, you shouldn’t have a problem with it.

Anyway, your problem is the addActiveActuator call is actually saying: “add the other scene.” You only want that line of code to be run when the new score is better. I don’t know where you got the idea for cont.add. As firesaid said replace


 if current_score &lt; new_score:
  cont.add = True
  print "new high score!!!"
 else:
  cont.add = False
  
 G.addActiveActuator(add, 1) 

with


 if current_score &lt; new_score:
     GameLogic.addActiveActuator(add,1) 

if current_score < new_score:
GameLogic.addActiveActuator(add,1)

does not work

STELLA, remember to replace GameLogic with G, since that’s what you imported it as.

Ha Ha, good point, Friday. I have complaints about that type of shortcut anyway. It removes clarity from the code. The whole idea of using objects in the first place is to make code more understandable. Using one letter replacements pretty much defeats the purpose and makes the code unreadable again.

lol i did use G

True, but it can make code easier to read and physically shorter by “compressing” multiple characters into just one.

True, but it can make code easier to read and physically shorter by “compressing” multiple characters into just one.

It’s a temptation that everyone faces. What it amounts to is your own shorthand that you perhaps understand and no one else will. In fact, after a few weeks, you probably won’t either. The GameLogic module is probably the safest to do it with because it’s used almost universally in Blender game engine scripting, but I still personally don’t like that kind of thing. Most of the time I don’t even bother looking at other people’s scripts because their variable naming is so poor I could write my own script before I figured out what they were doing.

i know what the problem is. the new score is written to list of scores before it is to be compared to the previous high score. so if its a new high score, itll be compared to itself therefore it isnt greater than the previous high score. so G.addActiveActuator() will never be true.