Python question - getting int property value

Im sure its possible, but i just dont know how to get a property value in python and then use it accordingly.

Ive got a ‘int’ property (we’ll call it the defult - prop) which constantly changes (random numbers) and I’m trying to set up a script that does the following example function -


if prop.is(1):
    do action1

if prop.is(2):
    do action2

etc etc

Any help would be good.


if prop1 == 1:
     prop2 = 2
if prop2 == 2:
     GameLogic.addActiveActiveActuator(act2, 1)

??? Anyway, I don’t really know what you’re asking. Perhaps if you’re more specific I could help.

Ok, ill try to explain it a bit better.

Ive got a cube, which has one property (called prop) (which is a number from 1-12). The number randomly changes every 30 second.

So when prop = 1 I’d like the script to do ‘function1’

Then when prop = 5 (for example) I’d like the script to do ‘function5’

And so on.

I know how to get the script to do the function, i just don’t know how to ‘read’ the property value in python lanuage.
Ive looked at the Blender Game Functions list and what im looking for doesnt seem to be there.

It seems like you’d do better by reading a tutorial for plain Python… the one I used was this one:
http://www.swaroopch.com/byteofpython/

But in an attempt to answer your question…
I’m pretty sure that you’d use this piece of code:

GameLogic.getCurrentController().getSensor("propbrick").getValue()

Assuming, of course, that the property brick that you have attached to the controller in question is named “propbrick”.

But you don’t want to type that line out every time, or even copy-paste it. So I’d strongly recommend using variables instead, as follows:
cont = GameLogic.getCurrentController()
propBrick1 = cont.getSensor(“propbrick”)
value1 = propBrick1.getValue()

Then you’d only need to use value1 to get access to this value each time.

If you don’t understand what I’m doing with the variables here, I’d recommend looking over this tutorial by Social:
http://socialstorage.googlepages.com/beginningbgepy
It doesn’t detail how to use or access properties, but it does demonstrate how to use variables to save you typing.

I’m pretty sure that you already understand this part, but here’s how I’d structure the actual code:

if value1 == 1:
    #the script performs the first function
    #function goes here
if value1 == 2:
    #the script performs the second function
    #function 2 goes here

…and so on.
Hope that this helps.
So far, I’ve found that the GE Python documentation is lacking in terms of explaining how to get to the methods. You may have to use dir() to manually look for the function you want - luckily, it’s usually in a logical place.

Try looking at this .blend.

http://www.4shared.com/file/34697996/5ad43da3/properties.html

AD-Edge
If the cube is the object calling the script, you do it like this:

# find the python controller that called the script
cont = GameLogic.getCurrentController()
# find the object that owns the controller that called the script
own = cont.getOwner()

if own.prop == 1:
    # do what you need to
elif own.prop == 2:
    # do that other thing

If another object is calling the script on behalf of the cube, you would do it like this:

# find the cube
cube = GameLogic.getCurrentScene().getObjectList()["OBCube"]

if cube.prop == 1:
    # do what you need to
elif cube.prop == 2:
    # do that other thing

Look at the place where I wrote “OBCube”. The object is named Cube, but you must put the OB in front of it.

Also, be sure that you are using the double equal sign “==” (which is the logical “is equal to” sign) to test values, and that you are not using the single equal sign “=” which will simply set the value of prop to whatever number is on the other side of the equal sign.

It seems like you’d do better by reading a tutorial for plain Python… the one I used was this one:
http://www.swaroopch.com/byteofpython/

Thanks, ill check out the tutorial.

If you don’t understand what I’m doing with the variables here, I’d recommend looking over this tutorial by Social:
http://socialstorage.googlepages.com/beginningbgepy

I was doing that tutorial the other day, but haven’t finished it yet.

Thanks for the .blend S.Cross, ive checked it out and it does exactly what i was talking about. :slight_smile:

And thanks Blendenzo! Both those scripts will come in handy, i did just need it for the object calling the script on itself, but the other will be very helpful.

Im impressed by everyones responses, and its fixed this problem. Ive been trying to learn python for around a year now to help my blender games but ive found it difficult.
But lately its been making a lot more sense, so im well on my way now, I was writing entire scripts yesterday very happily and i only got stopped because of this problem. :rolleyes: