A simple phyton question

Hey there, started learning phyton a little ago.
I was making this little test code. The objective is to get the object that has the property ‘‘value’’ changes to a rigid body state if the property changes to 1. So, could someone help this little noobie? :smiley:
Here’s the code:

from bge import logic

#Get Scene - This gets the current scene.

scene = logic.getCurrentScene()

#Get an object - the ["Cube"] can be changed to any object.
object = scene.objects["Cube"]

#Get property from ["Cube"]
property = object["value"]


Well, since you’re learning Python, it would be best if I taught you how to solve the problem rather than solving it for you. So, let’s walk through this.

You want to see if property is 1, and then change object to a rigid body state, right?

Well, you need to find a function that allows you to enable rigid body physics for a game object. Check the API, particularly here. Look under that heading - that’s for the KX_GameObject class, which is what all game objects in the BGE are. They all have the functions and properties under that heading.

Then, in your code, you need to check for a condition - if property is equal to a value (the number 1). If so, then you just execute the correct function.

Well, that looks Okay so far. The reason it’s only OK is that if your object doesn’t exist, your script crashes. Also, you use ‘object’ as a variable name, because ‘object’ is also used in some internal python commands, and the script will get confused very easily. Instead use ‘playerObject’ or ‘cubeObject.’

So what us guys normally do is use:

cubeObject = [obj for obj in bge.logic.getCurrentScene().objects if obj.name == "Cube"][0]

Looks daunting doesn’t it, so I’ll explain it.
This is really a compressed version of the following:

scene = bge.logic.getCurrentScene() #Gets current scene
for obj in scene.objects: #looks at every object
    if obj.name == "Cube": #Compares every object to the name 'Cube'
        cubeObject = obj

You don’t have to understand how I compressed it into the top code line, but there you go.

As to the second part of your problem, you want to learn about how to find the value of the property.
So you’ve got the property fine:

cubeProperty = cubeObject["property"] #Again using 'property' as a variable is a bad idea

So now we’ve got it we need to compare it:

if cubeProperty == 1: #Two equals means compare instead of assign (var = 1 vs if var == 1:)
    '''do whatever you want to do'''

In this case we want to set the physics type, so we look up the API and find some functions
Now we can use them like:

if cubeProperty == 1:
elif cubeProperty != 1: #Is not equal to 1

Hope that helped a little.

Nice! thanks for the little help! I’m still pretty much a noob as far as i can see. Heh, reminds me when i started using blender back in the 2.46 days… :slight_smile: