5
$\begingroup$

In my version of Blender (2.71), i can add "Text Game Properties" to text. But if i have two objects that are text, both individual properties are called the same thing "Text". I want to refer to both of them individually in a script in python, but i don't know how to do it. Please help.

@sambler This is the code i used, but it still won't work. Please help.

    from bge import logic

    def reload():
cont = bge.logic.getCurrentController()
own = cont.owner
txtVal = own['Text']
sce = bge.logic.getCurrentScene()
sce.objects['Refill']['Text'] = sce.objects['Refill']['Text']-(80-own['Text'])
$\endgroup$

1 Answer 1

2
$\begingroup$

A text object gets it's own button to Add Text Game Property. This button produces the same effect as the normal Add Game Property with the type set to string and name set to 'Text'. This 'Text' property of the text object gives the ability to get and set the contents of the text object during game play.

The 'Text' property can be accessed the same as other game properties, by treating the object as a dictionary and the property name as the key.

In a python script used as a controller that would normally be with -

cont = bge.logic.getCurrentController()
own = cont.owner
txtVal = own['Text']
own['Text'] = 'NewString'
own['prop'] = 25
own['prop2'] = 'a big long string'

Other objects in the game can also be accessed -

sce = bge.logic.getCurrentScene()
sce.objects['otherObj']['Text'] = 'something'
sce.objects['missile']['prop3'] = 356

Properties changed during game play only exist for the duration of the game. Values set in the Game Logic Editor will be in effect at the start of a game, with the 'Text' property being set to the contents of the text object set in edit mode.

EDIT: response to your code example.

While your debugging your scripts you will want to watch the console output. It can contain hints as to why your scripts fails.

In python scripts formatting is important, the indentation defines separate parts of code. Not sure if any formatting got lost copying your code across but it appears you have a function and the following code might be meant to be part of that, if so that code isn't going to run unless you add a function call at the end of the script. For this small script you don't need any functions.

The all important last line, you are mixing strings and integers which you need to convert to work as you want. int(strValue) will give you a number you can calculate with and str(number) will then give you a string to use for the text value.

import bge

cont = bge.logic.getCurrentController()
sce = bge.logic.getCurrentScene()

own = cont.owner
refillObj = sce.objects['Refill']

refillObj['Text'] = str( int(refillObj['Text']) - (80- int(own['Text']) ) )

It would appear that you may be trying to jump ahead of yourself. I'd recommend you spend an hour or so going over a python introduction tutorial to get more familiar with basic python concepts first. The official python tutorial can be a good start, even if you only go over parts 3 to 6.

$\endgroup$
2
  • $\begingroup$ Um, i've tried to do what you said but it still didn't work. This code is for an ammunition algorithm. I posted my code in the question above. What did i do wrong? $\endgroup$
    – christai
    Commented Jul 19, 2014 at 20:45
  • $\begingroup$ Okay, Thanks. :D My only problem was trying to call the object properties, because i could get exactly what i wanted with normal properties, just the text properties were being a pain. Thanks! $\endgroup$
    – christai
    Commented Jul 20, 2014 at 20:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .