How can I change a Game Property name?

Hi,

I would like change the name of a Game Property when the game is running.
For example I have a Cube with a property called “Cube”. How can I change this name
for example when I press “1” on my keyboard to the name “Sphere”?
I hope that anybody can help with with this question.

Why don’t just ask for a string instead for a property name? just wondering.

object 1 - property string “Cube”

If pressed 1key then property string=“Sphere”
if pressed 2key then property string=“Cube”
property.blend (511.9 KB)

Or why do yo need to change a property game while running the game?

I would like to exclude an object from being detected by a ray sensor or a near sensor.
Moreover I use a small script which tracks an object to the nearst property.

what mataii said, use a string property and change it value to what you like.

there is no real way of changing a properties name, but you can copy it’s data remove the old property and create a new one:

obj = cont.owner

if 'cube' in obj:
    obj['new_property'] = obj['cube']
    del obj['cube']

Ok, thanks for your answers. But it will be enough just to add a property and delete a property.
I look for a solution all day. Do you have an example file where a property is added and removed? I am not that good in Python and I think thats a part
of the problem.

I won’t give an example for that because what you are doing is learning it the wrong way.

but i can explain what it does

def change_property(cont):
    #own short for owner, the object the script is run from
    own = cont.owner
    #looks at own if a property called cube is present
    if 'cube' in own:
        #create a property called new_property and add the value of the old cube property into it
        own['new_property'] = own['cube']
        #removes the cube property from own
        del own['cube']

so you use a single property to track, so you alter the tracking script to get it to work with the right method.tracking to a property is good, if you delete that property, the script will give errors and stop working, because the script can’t run anymore due to the property it tracks to is removed.

And that’s why you should keep the property convert to string, and change it’s value to the object you want to track to. This also means that you need to adjust the tracking script a tiny bit.

Thank your for your explanation, I did a few test with the script in it works now.
Thank you for this.

1 Like