Problem with property in Python

How can I access the value of a property in a Python script?


cont = GameLogic.getCurrentController()
own = cont.getOwner()

#Set your property to a value of 2 (assuming that it's an int)
own.propertyname = 2


#Set a control statement to execute action according to prop value
if own.propertyname == 2:
    #Do something

Be sure to check the tutorial/demos sticky for usefull python tutorials.

1 Like

if the property come from another object that this one which call the script, use this :


import GameLogic as g

cont = g.getCurrentController()
own = cont.getOwner()
scene = g.getCurrentScene()
objects = scene.getObjectList()

Camera = objects["OBCamera"]

print Camera.Property1

Thanks!!! I’m kind of new to programming. You saved my day :stuck_out_tongue:

It’s funny how the answer seams so logic after we see it :slight_smile:

Thanks for the quick answer too, and sorry the bother.

so… using this method is a way to snthesize global variables I guess

you could have one object named as Variables and then set all properties to it. Then just have everything refer to that Variables object, but I wonder if that would slow it down having to do that all the time?

If you want “global” variables you can attach them dynamically to the GameLogic module:

import GameLogic

GameLogic.myProperty = "hello!"

“myProperty” is then visible to anything that imports GameLogic. Although, if you need to manipulate those properties with logic bricks that’s obviously not possible.
You could also attach an object with all your properties to GameLogic, rather than having to grab it from the scenes object list each time.

cheers
Piran

thanks, so
I guess it would go something like


import GameLogic as g

gobject = g.getObjectList()
myobject = gobject["OBThisObject"]
print myobject.Property1