Searching for ray property code?

Hi, I have a ray sensor attached with its property set in the logic brick but How in python do I use ray.propName to test which ray.hitObject contains the correct property? I tried a for ray.propName in ray.hitObject loop but the console gives me error ‘KX_GameObject’ object is not iterable.

If you want to check if hit object has specific property do following:

if 'property' in ray.hitObject:
    # Do something useful

if you want to iterate over all properties that object has do:

for property_name in object.getPropertyNames():
    # do something useful with each or some properties

I never use rays but I think if you give property to ray logic brick it should only give you objects that it hit if these object have specified property. Have not tested that though.

Just did some testing,

ray.hitObject returns first object that ray hits, or first object with property if you specify it (ignoring all objects in its way that do not have that property).

so you cant iterate like this:

for something in ray.hitObject:

because you cant iterate single KX_GameObject

So ray.hitObject should always return object that have specified property and there is no need for additional check up.

EDIT: keep in mind that ray sensors return None if no object with specified property is being hit, or if no object is being hit if property is not specified

So this code:

if prop in ray.hitObject:

would result in error, because None type has no properties.

aditional check would solve this:

if ray.hitObject: # would result in False if ray.hitObject return None
# do something

Zed is right. When you configure the property parameter rhe sensor will sense objects that have a property with this name only. You do not need to validate that again.

I’m still very noob with this game engine stuff and learning with trial and error here lol, thanks. Btw how do I post my code like you guys did? I’m not familiar with HTML stuff.

When you write reply or a post, click “GO ADVANCED”, you will get additional options, one of them will be marked with hash symbol # .

Or you could just write " [ C O D E ] insert your code here [ / C O D E ]" , just without empty spaces in between.

easiest is to go into advanced editing mode
fastest is to write [noparse]

[/noparse] around your code.

It can be useful for other things too if you want to preserve the spacing.

some more useful ray features.
ray.hitNormal — returns the direction in which the face you hit is pointing
normal_Direction = ray.hitNormal

ray.hitPosition — returns where the ray hit in 3d space
hit_Posi = ray.hitPosition

so with this you could do something like
bullet_hole = sceen.objects[“bulletHole”]
bullet_hole.position = ray.hitPosition

very basic scratching the surface of what you can accomplish with rays. The sky is the limit and I’ve found rays and the radar sensor which is similar to be invaluable in making games, you can get really creative with them.