something confusing about functions

Here is something that has been happening in a couple of my python modules. Its not really a problem, but I would like to understand why things are happening this way.


##########EXAMPLE.PY########
def property_one(cont):
    own = cont.owner
    do stuff
    do stuff
    if own.getDistanceto(something) > maxdistance:
       do this max distance suff
    do stuff 
    return

def property_two(cont):
    do stuff
    do stuff
    do stuff
    do stuff
    do stuff
    return

LOGIC:
PROPERTYONE SENSOR--------------------PYTHON MODULE MODE
TRUE TRIGGERING ----------------------EXAMPLE.property_one


PROPERTYTWO SENSOR--------------------PYTHON MODULE MODE
TRUE TRIGGERING ----------------------EXAMPLE.property_two

Ok what happening is even if my objects property_one is no longer True, and only property_two is True. It is still doing the stuff under own.getDistanceto(something) > maxdistance if the distance gets greater. Even if property_one is not active, and Im doing property_two stuff. I was just curious as to why. .Sorry if my question is not clear. I can stop it from happening by doing this.



##########EXAMPLE.PY########
def property_one(cont):
own = cont.owner
if own[“property_one”] == True:
do stuff
do stuff
if own.getDistanceto(something) > maxdistance:
do this max distance suff
do stuff
return
else:pass

def property_two(cont):
if own[“property_two”] == True:
do stuff
do stuff
do stuff
do stuff
do stuff
return
else:pass





I was just wondering why I would need to do this. Why the game would be doing things from functions Im not even using. Thanks for any help.

Well, it shouldn’t and most certainly isn’t doing that. For any code to fail at boolean if statement is pretty unlikely, user error is far more likely. You need to debug which path the script running really takes. Since there is no breakpoint debugging a good method is to add quick print() commands in the beginning of sections. Also what I do about misbehaving if statements is I actually print the variables right before I make the comparison:


...
print("Comparing a:" + str(a) + " and b: " + str(b))
if a > b:
    print("a was bigger")
    ...
else:
    print("b was bigger or equal")
    ...

BTW you don’t need else: pass in the if statements. If you need nothing to happen in other case you can leave out the else part altogether.

And you can just write:


if own["property"]:
    ...
else:
    ...

if you are using booleans. Try it :slight_smile:

Also note that you could make all the property checks inside one python script instead of having sensor SCA combined with individual functions. This could be easier in the long run as your project grows more complex and finite state machines need to be in one place expressed as clearly as possible.

Your code does not check the status of the connected sensors. Please have a look at the guide-URL in my signature. There you can find an entry called a word on sensors. It tells you the relatinship of triggering and sensor status. Sorry I cant post the URL from here.

Your code does not check the status of the connected sensors.
You are correct. In my psuedocode I should have wrote, if cont.sensors[“property_sensor_name”].positive. Which is how it is in my functions. My example code was not a good example =(

[For any code to fail at boolean if statement is pretty unlikely, user error is far more likely.
Again I guess I used a bad example. As in my code I do not use booleans but string properties that must equal certain strings.

I guess the jest of what Im wondering is this. If I have an entry point that points to a particular Def function. Is there anyway possible it will execute anything other than the function at the entry point? unless its another function call. Like does having a bunch of functions in 1 py file matter. Will anything else cause it to execute anything other than what is in the called function within the same file, If I use many entry points for different functions in the same file? what is good practice?

When you directly execute a particular function from a module, none of the other functions get called.

However, when a Python controller recieves ANY pulse from a sensor, whether positive or negative, it will execute. That’s why you need to explicitly check if the sensor is positive. Sensors can actually output one of three signals to a controller: positive pulse, negative pulse, or no pulse at all (or at least that’s what I remember. I’m fuzzy on the details).

However, when a Python controller recieves ANY pulse from a sensor, whether positive or negative, it will execute. That’s why you need to explicitly check if the sensor is positive

Well seing how its a property sensor with true pulse, it should only execute if the property matches correct? and if I check within the script, I usually just check the property because if the property isnt matched the script wouldnt be running anyway right?

True pulse does not restrict the sensor output it. It increases the sensor output. A sensor alway triggers when it evaluates a change. It is the concern of the controller to perform the right decision. (Please look at the above mentioned thread about this.)

You can use the sensor as a notificator (what it is anyway). You have some options to react:
A) check the result of the sensor evaluation with .positive as it is already done
B) perform your own evaluation and check that. This usually happens if you need to verify complex combinations.
C) any combination of the A) and B)
D) do not check at all. The result the sensor(s) might evaluate False