simple python code help

Just getting into working with python a bit more, the main problem i’m trying to work around is how set a property from the object with the module/script to True or False.

right now I have an always sensor (true pulse) connected to my module/script and a boolean on my cube called ‘asdf’
Here’s the code i’m using:

import bge

def propertychange():
    cont = bge.logic.getCurrentController
    scene = bge.logic.getCurrentScene
    own = cont.owner

    Cube = scene.objects['Cube']
    switch = cont.sensors['Always']
    if switch.positive:
        Cube['asdf'] = True

Neither module (with scriptname.module) or script mode of python seem to work.

What am I doing wrong?
Is there a better way to do this?


If you run it as script nothing happens because you’re only defining a function. If you run it as module you need to run it with “modulename.propertychange” where modulename is the name of the script file without the .py extension.

Edit: also the always sensor checking is redundant. Always is always positive and any one sensor you connect to your python controller is always positive because the script isn’t run otherwise. So something like

import bge

def propertychange():
    Cube = scene.objects['Cube']
    Cube['asdf'] = True

would work for this purpose.

Thanks, however using this doesn’t seem to work either (even with the bge.logic.getCurrentScene etc. moved across).
I was originally using a keyboard sensor but since it wasn’t working i replaced it with an always sensor, which as you said probably isn’t necessary.

Here’s the current .blend property changing.blend (471 KB)

You’re missing () from the getCurrentScene().

It’s a function (all the stuff that start with ‘get’ usually indicate this) that returns what you want rather than a property :slight_smile:

Awesome :smiley: it works now

The same applies to get current controller. Any time you see get in an API name it means that It is a getter function which has to be called.