NoneType is not subscriptable

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


    selector = scene.objects['HotBarSelector']
    hand = selector.sensors['Ray']
    handItem = hand.hitObject
    itemDamage = handItem['damage']

Keep getting the error NoneType is not subscriptable whenever i try and read string properties from the hitObject

You have to first make sure the hitObject is not ‘None’ before doing any code that makes use of it (using a ‘if’ statement).

ah that makes sense, cheers

still getting the same error, on the line “itemDamage = handItem[‘damage’]” Ive told it what to do if hand.hitObject == None

So you are executing the code when the object is NoneType (hand.hitObject == None) when you have to do the opposite, use =! instead or just check directly: if hitObject. Also you have to check if the object have the property “damage” if you doesn’t use a property in the sensor.

hitObject = hitSensor.hitObject 

if hitObject:
    if "my property" in hitObject:
        do somenthing
def Tree():

    scene = bge.logic.getCurrentScene()
    cont = bge.logic.getCurrentController()
    own = cont.owner
    selector = scene.objects['HotBarSelector']
    rayItem = selector.sensors['Ray']
    leftMouse = own.sensors['LMouse']
    mouseOver = own.sensors['MouseOver']


    if leftMouse.status == bge.logic.KX_INPUT_ACTIVE and \
    mouseOver.status == bge.logic.KX_INPUT_ACTIVE and \
    "item" in rayItem.hitObject:
        own['health'] -= rayItem.hitObject['damage']
        
    if "item" not in rayItem.hitObject and \
    leftMouse.status == bge.logic.KX_INPUT_ACTIVE and \
    mouseOver.status == bge.logic.KX_INPUT_ACTIVE:
        own['health'] - 1

when i use if “prop” in hitObject: i get the error “item not iterable”

def Tree():

    scene = bge.logic.getCurrentScene()
    cont = bge.logic.getCurrentController()
    own = cont.owner
    selector = scene.objects['HotBarSelector']
    rayItem = selector.sensors['Ray']
    rayType = selector.sensors['Ray.002']
    rayDamage = selector.sensors['Ray.001']
    leftMouse = own.sensors['LMouse']
    mouseOver = own.sensors['MouseOver']


    if rayItem.status != bge.logic.KX_INPUT_ACTIVE and \
    leftMouse.status == bge.logic.KX_INPUT_ACTIVE and \
    mouseOver.status == bge.logic.KX_INPUT_ACTIVE:
        own['health'] - 1
    if leftMouse.status == bge.logic.KX_INPUT_ACTIVE and \
    mouseOver.status == bge.logic.KX_INPUT_ACTIVE and \
    rayItem.status == bge.logic.KX_INPUT_ACTIVE:
        own['health'] - rayDamage.hitObject['damage']

no errors in the console but its still not working

You wouldn’t check if object is in hitObject.

That would only be applicable if hitObject was a list of objects rather than just the first object detected by the collision sensor.

So you would have something like this


myObject = mySensor.hitObject

if myObject == objectBeingLookedFor:
    #do something

def Tree():

    scene = bge.logic.getCurrentScene()
    cont = bge.logic.getCurrentController()
    own = cont.owner
    selector = scene.objects['HotBarSelector']
    rayItem = selector.sensors['Ray']
    rayType = selector.sensors['Ray.002']
    rayDamage = selector.sensors['Ray.001']
    leftMouse = own.sensors['LMouse']
    mouseOver = own.sensors['MouseOver']


    if leftMouse.status == bge.logic.KX_INPUT_ACTIVE and \
    mouseOver.status == bge.logic.KX_INPUT_ACTIVE and \
    rayItem.hitObject == None:
        own['health'] -= 1
        
    if leftMouse.status == bge.logic.KX_INPUT_ACTIVE and \
    mouseOver.status == bge.logic.KX_INPUT_ACTIVE and \
    rayItem.hitObject != None:
        own['health'] -= rayDamage.hitObject['damage']

Okay, my error is on my last line, the error is “NoneType object is not subscriptable”. all the rays are set to search for specific

If ray.hitObject:
    If 'Tag' In ray.hitObject:
        #Do stuff

try, except

would work for that

I’m able to read property no problem now, but i cant subtract hitObject[‘prop’] from own[‘prop’]

If ray.hitObject:
  own['Value']-=ray.hitObject['Value2']

Thats what i have, read my latest code. Its giving me the same error

def Tree():
    scene = bge.logic.getCurrentScene()
    cont = bge.logic.getCurrentController()
    own = cont.owner
    selector = scene.objects['HotBarSelector']
    rayItem = selector.sensors['Ray']
    rayType = selector.sensors['Ray.002']
    rayDamage = selector.sensors['Ray.001']
    leftMouse = own.sensors['LMouse']
    mouseOver = own.sensors['MouseOver']


    if leftMouse.status == bge.logic.KX_INPUT_ACTIVE and \
    mouseOver.status == bge.logic.KX_INPUT_ACTIVE and \
    rayItem.hitObject == None:
        own['health'] -= 1
        
    if leftMouse.status == bge.logic.KX_INPUT_ACTIVE and \
    mouseOver.status == bge.logic.KX_INPUT_ACTIVE and \
    rayItem.hitObject != None:
        own['health'] -= rayDamage.hitObject['damage']

On the last line, it should subtract the value of damage from the owm[‘health’] in this case the owner is a tree and when the player is holding an item with a damge value it should use that value otherwise just damage by 1, every object in my game has a damage value, and the rays are set to check specifically for these items but i keep getting the same error “NoneType is not scriptable” and i cant seem to figure this out

ok, don’t do this this way you have it all backward.

in player -> NO CODE IT TREE :stuck_out_tongue:

this is the player hitting a tree (like a axe?)

or the player hitting the tree (on a bike?)


if ray.positive and mouse.positive:
    ray.hitObject['Health']-=own['Damage']

Explain the situation,
No code at all, what is suposed to happen?

The “Not Subscriptable” error has a meaning. What is means is that something is going wrong when you are using square brackets.

I’ll give some examples, which of these makes sense:


a = {'item':5, ''another item':3}
b = a['item']

a = None
b = a['item']

a = 0
b = a['item']

You can see that the same line of code (b = a[‘item’]) can sometimes make sense and sometimes not. When it doesn’t, the error is thrown.

So to solve the issue you can:

  1. Make sure the object is always what you think it is (in practice this is hard)
  2. Check to see if it is (if ‘Item’ in a)
  3. Try/Except

I’d advise printing some information so you can see what is going on.

Thank you for all the feedback. What im trying to achieve is quite simple, when the player (in a flat 2 world) mouses over and clicks a tree it should affect the trees damage based on whats in the items hand. The rays stem from a piece of GUI which hovers over the active item (item in hand)

all of my ray sensors are checking for the properties specifically, and each item has these props. so the only things being hit by the rays are items with these props, or nothing at all. I can get it to a point with no errors in the console but it still doesnt work, my brain hurts.

is the item in hand in a overlay scene, or in the current scene?

Do you pick the item in hand from a set of icons?

or keypresses?

lets try and separate functions,

Choosing a item, sets a value in the player,

clicking on a ‘tree’ then checks to see if you are close enough, and if so do damage based on what is chosen?