Question: .hitObject not working

Hey everyone!

So I have a problem with my game where I’m using a radar sensor to get a target that the player attacks. So it’s like target = hitSens.hitObject, and then i use target.name except I get an error in the console saying “Nonetype” has no attribute “name” so I’m guessing the sensor isn’t working. Any ideas?

Thanks

If the radar sensor is not touching anything, the “hitObject” will return a Nonetype value, try something this:

import bge
cont = bge.logic.getCurrentController()

hitSensor = cont.sensors["Radar"]
hitObject = hitSensor.hitObject
if hitObject:
    *do domething*

Checking the return of hitObject will avoid errors related with a Nonetype value, because the Nonetype equals False, so everthing inside of the loop will be read only if the return of hitObject is not a Nonetype.

Also, if you are trying to do something with the object that the radar detect, do not get the name and use it on scene.objects, use the return of the same hitObject, example:
hitObject = hitSensor.hitObject
hitObject.endObject()
I tell you this just in case you don’t know it, because I made ​​that mistake when I was new…

Ok thanks I’ll try that but I tried to make sure that the criteria of the radar were met. I was right up to the enemy box and such.

print(target)
print(type(target))

please

Are your targets set to Actor? That usually throws me off, haha.
Also (simplistic help alert), make sure your radar is set to the positive Y axis =P

I think that worked thanks MichelleSea but also now for some reason the animation that played when everything wasn’t working is no longer playing. Here’s my code for that section:



animChoose = randint(0,1)
            if animChoose == 0: #There are two different animations
                rightArm.playAction("rightArm", 90, 135, 0, 0, 0, 0, 0.0, 0, 0.75) #rightArm is the armature object
                handR.playAction("playerRightHandAction", 90, 135, 0, 0, 0, 0, 0.0, 0, 0.75) #handR is the empty to which the weapon is parented
                if target:
                    print(type(target)
                    cont.sendMessage("attack", "", target.name, "")
            elif animChoose == 1:
                rightArm.playAction("rightArm", 200, 230, 0, 0, 0, 0, 0.0, 0, 0.75)
                handR.playAction("playerRightHandAction", 200, 230, 0, 0, 0, 0, 0.0, 0, 0.75)
                if target:
                    print(type(target)
                    cont.sendMessage("attack", "", target.name, "")


Also it’s giving me a minor error which it has given me before, just in the .playAction, that the type of action is wrong, it has to be either 0-2 and it isn’t saying what line it’s on or in what script, but it changes it to play mode anyway.

Maybe is the random number that you generate, is it running every line? in this case, it will generate a random number every frame, so every time the generate number is different than the last one the animation will change(and the loop will break), can you post the entire script? also I think you can put the KX_GameObject in the body of the message, instead of the name, it will avoid problems when many objects have the same name.

Here’s the script. It’s messy so I didn’t want to show the whole thing but it’s ok as long as y’all can read it:

import bge
from random import randint



scene = bge.logic.getCurrentScene()
handR = scene.objects['playerRightHand']
rightArm = scene.objects['rightArmSkel']
inv = scene.objects['inv']


cont = bge.logic.getCurrentController()
ironSword = cont.owner


if inv['itemRh'] != "":
    itemRh = scene.objectsInactive[inv['itemRh']]
else:
    itemRh = ""
if inv['itemLh'] != "":
    itemLh = scene.objectsInactive[inv['itemLh']]
else:
    itemLh = ""
    
mouse = bge.logic.mouse
key = bge.logic.keyboard


mmb = bge.logic.KX_INPUT_ACTIVE == mouse.events[bge.events.MIDDLEMOUSE]


lmb = cont.sensors['lmb']
rmb = cont.sensors['rmb']
hitSens = cont.sensors['hitRadar']
hittableCheck = cont.sensors['hittableCheck']
npcCheck = cont.sensors['npcCheck']


if rmb.positive:
    #Check if lh is shield
    #If not, animate, start block timer.
    if "Sword" in itemRh.name:
        if mmb:
            rightArm.playAction("rightArm", 230, 240, 0, 0, 0, bge.logic.KX_ACTION_MODE_PING_PONG, 0.0, 0, 0.75)
            handR.playAction("playerRightHandAction", 230, 240, 0, 0, 0, bge.logic.KX_ACTION_MODE_PING_PONG, 0.0, 0, 0.75)
        else:
            rightArm.playAction("rightArm", 250, 260, 0, 0, 0, bge.logic.KX_ACTION_MODE_PING_PONG, 0.0, 0, 0.75)
            handR.playAction("playerRightHandAction", 250, 260, 0, 0, 0, bge.logic.KX_ACTION_MODE_PING_PONG, 0.0, 0, 0.75)


def onRmbRelease():
    pass 


if lmb.positive:
    #if in right hand, attack
    #if in left hand, block
    #for attacking; check whereabouts attacker is in position to defender
    #Figure out corresping damage values, and account for defender's blocking
    #Also account for dodge perhaps?
    
    
    target = hitSens.hitObject
    
    if "Sword" in itemRh.name:
        if mmb:
            rightArm.playAction("rightArm", 135, 170, 0, 0, 0, 0, 0.0, 0, 0.75)
            handR.playAction("playerRightHandAction", 135, 170, 0, 0, 0, 0, 0.0, 0, 0.75)
            if target:
                    
                    bge.logic.sendMessage("attackAlt", "", target.name, "")
        else:
            animChoose = randint(0,1)
            if animChoose == 0:
                rightArm.playAction("rightArm", 90, 135, 0, 0, 0, 0, 0.0, 0, 0.75)
                handR.playAction("playerRightHandAction", 90, 135, 0, 0, 0, 0, 0.0, 0, 0.75)
                if target:
                    print(type(target)
                    cont.sendMessage("attack", "", target.name, "")
            elif animChoose == 1:
                rightArm.playAction("rightArm", 200, 230, 0, 0, 0, 0, 0.0, 0, 0.75)
                handR.playAction("playerRightHandAction", 200, 230, 0, 0, 0, 0, 0.0, 0, 0.75)
                if target:
                    print(type(target)
                    cont.sendMessage("attack", "", target.name, "")


def getEquippedActor():
    pass


def endSwordObj():
    itemRh.endObject()

Well I see the script, I don’t think the random number is the problem: if you don’t have the lmb sensor set to triggering, or if you don’t press it too many times, and if you are pressing the two mouse buttons at the same time will cause problems(use a “elif” in the “if lmb.positive”). Try just click and see if the action is played(do not press anything else).

Thanks but What do you mean by the elif? Where do I put it and what conditions? Also I’m using Tap for the lmb.

Edit: It seems that the problem is with my message centre thing, when i comment it, as well as when I comment print(type(target)), and type some other print or something there instead it works fine.

Edit: I found it, it was just because I typed print(type(target) and forgot the last end bracket. Thanks for a ll the help everyone :smiley:

“elif” is an abbreviation for “else if”.

You use that to check for more than exclusive two conditions.

e.g.


if a:     if a:
elif b:   else if b:
elif c:   else if c:

this comes in handy in when you want to check multiple ranges:


if a < 0: 
    print("a is below zero")
elif a < 10: 
    print("a is between zero and 10 [exclusive]")
elif a < 100: 
    print("a is between 10 and 100 [exclusive]")

in difference to:
if a < 0: 
    print("a is below zero")
if a >= 0 and a < 10: 
    print("a is between zero and 10 [exclusive]")
if a >= 10 and a < 100: 
    print("a is between 10 and 100 [exclusive]")


PLEASE don’t take this the wrong way:

You should take some time to learn some Python! “elif” is a super basic concept and there are a lot of awesome things Python can do that you might not know about. I taught myself Python by working on lots of failed projects that never went anywhere over a span of… 3? years, and I’m still at a basic level of knowledge about it.

Lol no I totally know what elif means! I meant what do you mean by the elif what do I need in it? Haha sorry.