I am trying to have an objects distance on the x axis be tracked then displayed through a text image property in real time. Then if the distance is greater than the saved high score it will need to save the high score when my player dies and also I need a difforent text image to display the current high score.
I don’t speak parseltongue so I really could use some help. I hope this makes sense.
should work, with ‘trackingobjectname’ being the name of the object that you want to track (under Blender 2.5). As for the high score stuff, I’m sure Monster would say to look into his SaveLoad module - I think it can help with that (I don’t usually save info to or from files, so I don’t know a lot about that).
Well I guess I can give up on that game. Your saveloader is useless to me, I just see thousands of line of code that I will never understand and I wouldn’t even begin to guess what parts I would need to use.
I should just find some one in town that can code and team up using Unity or some other engine.
Keep at it! Just because you don’t understand something now doesn’t mean that you wont later. :yes:
Anyway, assuming that you’ve set everything thing up right, here’s a script that should work in Blender 2.49, I’ll comment it heavily so it’s clear what’s going on. This script should be attached to the object that you’re tracking along the X axis. This script also assumes that the only thing being saved in that file is the high score and that the high score is the only data in the file.
#Get the usual stuff
cont = GameLogic.getCurrentController()
own = cont.owner
scene = GameLogic.getCurrentScene()
#Get the text plane object
#<i>textObj</i> is the name of the plane object which displays the text,
#edit/change names accordingly
textObj = scene.objects['OB<i>textObj</i>']
#The high score only needs to be loaded once
#so we check for a variable that wont exist on the first run, returning false.
if own.has_key('highLoaded') == False:
try:
[INDENT]#Check if there is a current high score saved
saveData = open('highScores.txt', 'r')
#Load the save file into a variable
highScore = saveData.read()
#Since .read() returns a string, convert it to an floating point for comparison later
own['highScore'] = float(highScore)
#Close the file after opening it - releases system resources used by the file
saveData.close()
[/INDENT]except:
[INDENT]#If something goes wrong reading the file, or there is no saved file
#set the high score to a default value. In this case zero.
own['highScore'] = 0
#Create the variable checked at the start so this part only runs once.
own['highLoaded'] = 1
[/INDENT]#Get the objects position on the X axis
xPos = own.worldPosition[0]
#Convert X axis position to absolute value
xPosAbs = abs(xPos)
#Display the objects X axis on the text plane
textObj['Text'] = xPosAbs
#Compare the X axis value to the high score, if it's greater save it.
if xPosAbs > own['highScore']:
try:
[INDENT]#This will create a text file and save the high score if no file exists.
saveData = open('highScores.txt', 'w')
#To write a value it needs to be a string, so convert xPosAbs to a string.
newScore = str(xPosAbs)
#Save the new high score, over writing the old one.
saveData.write(newScore)
saveData.close()
except:
#In case something goes wrong, tell it to the console.
print 'Failed to save score.'
[/INDENT]
I like to use try/except when dealing with reading/writing files so the game doesn’t break if something goes wrong. It’s also handy for checking data’s saved and creating defaults if there is none.
I/O in python can be slow, so running this script every frame could slow the game down (you could use cPickel which would be a lot quicker, but not as light weight). Also, it might be worth adding an extra argument to the second if statement, so it’s not constantly writing the X axis position to a file on every loop. Or separating the script into two so one script updates the text plane and another handles the saving running less often or triggered on an event -like the game ending.
As usual, I’m at work, so I can’t test this, if anyone spots any errors or it doesn’t work let us know or fix it and post it up. If I get chance when I get home I’ll post up a .blend.
Hope this helps!
EDIT: fixed typo in script
EDIT: converted xPos to absolute values to handle negative values
haha, that’s so bad it’s funny! (well, made me chuckle anyway)
I got into the bad habit of over-commenting code as I often take long breaks from programming and forget things, sometimes it can be helpful, most of the time it’s just extra clutter and annoys people who know what they’re doing. Not sure if it all works though!
except:
[INDENT]#If something goes wrong reading the file, or there is no saved file
#set the high score to a default value. In this case zero.
own['highScore'] = 0
[/INDENT]
I’ve updated the script above, there’s probably more, let me know as you find them, and I’ll keep checking back and do my best to fix them.
Ok I think I got it working only problem is for what ever reason I build my game with me moving in the -X so when I move that way it prints a - and if I go backwards it gives me single digit numbers 1-9 and repeats them as I move back.
Hmm, just a thought but it could be related to the planes Text property expecting a string and being given a float, sometimes this can cause errors. Try changing this line:
textObj['Text'] = xPos
to:
#Converts the objects X position floating point to a string and displays it on text plane
textObj['Text'] = str(xPos)
I’m also assuming that the textObj is in the same scene as the object the scripts attached to? Also, it might be related to the logic brick set up - i.e. how often the script is triggered to run. If you’ve got it set up to an always sensor change it to pulse mode and increase it’s frequency (f) to something like 15-20, use higher values if you can get away with it.
We’ll get there, just gotta stick with these things! Hopefully all the comments is helping you understand what’s going on and the python a little better. Anything that you don’t understand just ask.
That didn’t seam to effect anything, does the same.
Yeah it all makes sense, I just have the memory retention of a… well I don’t know, I just can’t remember this stuff. I even have to look up some html tags some times and I have been making websites for years.
Edit: Didn’t see your second post till after I posted, it now does the 1-9 thing forward(YEY!). Also it is odd that it kinda is random placement of the numbers.
You mean the way the text plane displays them? If that’s the case I’d check the UV mapping on the plane, plus have a look at any scaling or moving of the verts on the plane object it’s self. I really wish Blender handled in-game text better! I’ve always got the best display results when the UV map is square and the plane is square, then just scale the plan uniformly to the desired size.
Oh, and I updated the script in my first post to use the absolute value.
No its that the number is not placed right on the uv, I uploaded a video so you can see what is going on. I did split the scripts so it will save it only when you die.