getting the propert of an object im hitting with the mouse

I was wondering how to get a property off an object i was hitting with the mouse assuming I have all sensors in place and a property name prop on what im hitting.

mouse = controller.sensors["over"]
objhit = mouse.hitObject

I try

prop = objhit[“prop”]

dont work

prop = [“prop”] in objhit

doesnt lock up the script. But doesnt work

prop = objhit.prop

also isnt working.

Any help is appreciated

Edit: I also tried

prop = controller.sensors[“click”].positive and controller.sensors[“over”].positive and objhit[“prop”]

doesnt work also

Your code is correct.
However, you have to understand that the hitObject attribute will be None if the mouse isn’t hovering over an object, meaning that your code will break.
Therefore, it is best to use:

import bge

controller = bge.logic.getCurrentController()
owner = controller.owner #you don't need this yet, but you might so i'll leave it in.

mouse = controller.sensors['over']
objhit = mouse.hitObject

if objhit:
    prop = objhit['prop']

hmm didnt work…could other parts of my script interrupt it / mess it up?

They could. It would be best to post a blend file, because it sounds like the sensor isn’t being triggered at all.

here ya go. I commented what I added from your recomendation I works fine till I add those

foragoose.blend (306 KB)

the cubes have ‘prop’ propert on them
the script is attached to the pointer

Is that my file?
It looks familiar!? ;D

Oh, there’s an easy solution.
You try and request the key “prop” from the hitobject, but some of the gameobjects don’t have that property in the first place, hence throwing an error. Use this code instead:

if objhit:           
	if 'prop' in objhit:              #####HERE AGOOSE
		prop = objhit["prop"]

[INDENT]Is that my file?
It looks familiar!? ;D

lol yeah. Your basic RTS flag script. Basically learned all my python from that scripts. But that fixed my problem. TY for the easy fix. :smiley:

if objhit:           	if 'prop' in objhit:              #####HERE AGOOSE		prop = objhit["prop"]		print(prop)

is the one that worked…could I also use it to change the property?

if this happens:
-----prop = prop + 1

didnt work right off the bat but ill play with it

much simpler:


or with a default


used to change:

value = obj.get("prop",0)
value += 1
obj["prop"] = value

only if exist:

value = obj.get("prop")
if value is not None:
  value += 1
  obj["prop"] = value


if "prop" in obj:
  obj["prop"] += 1


  obj["prop"] += 1
except KeyError: