update a string property via Python[solved]..fixed width text in-game-Font available!

Greetings all!

Please forgive my ignorance I just started scripting in Python a few days ago. I am fluent in visual basic but terrible at C++, etc. and I’m finding Python difficult to grasp.

Here’s what I’m doing:
I am making a simple bowling game. It’s my first ever attempt at using the game engine and Python. It’s great experience.

I am storing player scores in a “list” in a global variable (property)
like this:

class Scoresheets:
Player1_Frames = [-1,-1]*12
Player2_Frames = [-1,-1]*12
Scores = Scoresheets()
GameLogic.Scores = Scores

As you see, it stores 24 integers, populated with “-1” until the player bowls a frame, then it’s simply updated with the number of pins the player hits with each ball.

What I want to do is display a scoresheet when the player(s) press a key. I am doing this by using a text plane with a string property called “Text” like this

I want to populate this string with the scores. Obviously I have to coerce integers into string bits and can do so using the “str()” function.

Problem is, I’ve tried multiple ways of writing this and all yield the same result. If I have more than one value I get an error.

Example script as follows:

cont = GameLogic.getCurrentController()
owner = cont.owner
Scores = GameLogic.Scores
Player1_Frames = Scores.Player1_Frames
UpdPl1 = cont.actuators[“UpdPl1Score”]
ScoreEmpty = True
Pl1ScoreStr = ‘’

for item in Player1_Frames:
if item <> -1:
if item <= 9:
Pl1ScoreStr += ’ ’ + str(item)
else:
Pl1ScoreStr += str(item)
ScoreEmpty = False
else:
Pl1ScoreStr += ’ ’

Pl1ScoreStr += ’ ’

if not ScoreEmpty:
UpdPl1.value = str(Pl1ScoreStr)
cont.activate(“UpdPl1Score”)

So, say player rolls first ball, picks up 9 pins

Score reads: 9

player picks up the last pin with second ball

Score should read: 9 1

Instead it read: [ + [9 + [extra characters after expression]]]

I’ve been reading a lot on the Python site and searching the forums and tutorials, I’m just not getting it. Help? Someone?

P.S. Sorry the script examples are difficult to read, the forum removes all spaces and tabs :^(

Sounds like it should work, so I’m guessing a typo?


p1score1 = 9
p1score2 = 1
display.Text = str(p1score1) + " " + str(p1score2) 

Hi, thanks for replying jplur!

actually, I don’t think I have any typos. I get no errors on the console.

I’ve tried this:

String += str(playerscore1)

This:

String = String + str(playerscore1)

And both yield the same result. It’s as if the string property attached to the plane wants a string but somehow interprets it as a function of some sort(?)

Yeah not all typos actually cause errors. I like to keep a python interactive console open so I can test things instantly.

if you try:


score = ""
score += str(9)

you get “9”

Ugh I knew this would happen! I’ve been struggling with this almost two days! I worried and stressed about posting here because I like to solve things by myself and I was also worried I would figure it out right after I posted.

Well, I did. And I feel so stooopid.

If you look at the above code, you will see that I chose to use an “Update Property” actuator attached to my python controller that runs the script.

Ummm, yea. When you update a string property, you have to put the text in quotes “1 2 3” etc. in the value box of the actuator.

Solution?

Pl1ScoreStr = ‘"’ # this is a double quote enclosed by single quotes to start the string.
for item in Player1_Frames:
if item <> -1:
if item <= 9:
Pl1ScoreStr += ’ ’ + str(item)
else:
Pl1ScoreStr += str(item)
ScoreEmpty = False
else:
Pl1ScoreStr += ’ ’

Pl1ScoreStr += ’ ’
if not ScoreEmpty:
UpdPl1.value = str(Pl1ScoreStr) + ‘"’ # This is another double quote enclosed by single quotes to finish the string
cont.activate(“UpdPl1Score”)

Solved.

Again, thank you for talking me through this!

I didn’t read all the comments, but I think you should just use the s.join method.

s.join(scoreVariableList)

Where s is the separator, and scoreVarialbeList is the list of values you want to put together.

Well, shucks, SunJay03, I just tried your suggestion. Thanks! It doesn’t really suit my purpose for this particular problem but I can think of many uses for this join method.

I figured it out though. I have a new problem, however, and am hoping for some resources or suggestions…

Now that I have my scoresheet working somewhat, I am plagued by the problem of non-fixed-width fonts.

I did some research and found this in the archives. It’s a tutorial for creating custom fonts for the game engine. What I’m hoping for is that somebody already has a fixed width font texture available?? I’m trying to learn Python and the GE, I really don’t want to be side-tracked with font creation for such a simple application. I guess I will if I really have to…

Here’s what I mean:

When Player presses “S” the scoresheet is displayed. It looks like a generic 10-pin bowling scoresheet. But depending on the players numbers, the position of the text can get very skewed. I’ve messed with it too long and there’s no solution aside from creating a unique text box for every frame…or having a fixed font…


See how messed up they get? Any suggestions?

I think you should just go with an individual text box for each frame, and name them in a convention so that you can use a loop to go through all of them and change their respective Text properties.

If you use a non-kerned font sheet like monaco it might line up.

Hi all, thanks for all of the useful input, it really is greatly appreciated!

I bit the bullet and downloaded the font creator. It’s much easier to use than I had anticipated.

So, since there are none I can find on google, I figured I would post my fixed-width font. It’s decent looking and just a generic font found everywhere.


IMPORTANT!!! The forum doesn’t allow TGA images to be uploaded, so I had to re-save it as a JPG. Click to view it as a full size image and save it to your hard-disk. You MUST open it in an image editor and resave it as a TGA before it will work in Blender. Don’t resize it or anything, it’s fine as is.

My scoresheet works great now, yay!

P.S. Admins are welcome to move this thread to “Resources” if they wish. Thanks!