3
$\begingroup$

Instead of having several ray sensors each set to look for a specific property, I would like to have one ray sensor that looks at everything and have python decide what to do with what is hit based on the property.

So something like this:

sensor = cont.sensors["Ray"]
if sensor.positive:
    trigger = sensor.hitObject
    if trigger.(whatever would determine if it has the property):
        # do something
    if trigger.(another property):
        # do something else
$\endgroup$
2
$\begingroup$
sensor = cont.sensors["Ray"]
if sensor.positive:
    trigger = sensor.hitObject
    if "property" in trigger:
        # do something

You may also want to check if the property exists and get it (all in one lookup), This can be done using get a fallback value to avoid try/except.

sensor = cont.sensors["Ray"]
if sensor.positive:
    trigger = sensor.hitObject
    prop = trigger.get("property", None)
    if prop is not None:
        # do something

This style of access is the same thats used in Python dictionaries. eg:

dct = {"a": 2}
if "a" in dct:
    print("found")

val = dct.get("b", 2)
print(val)  # -> 2 (or the value)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.