Another "What is wrong with my code" question

Hello!
Today I’m trying to only have my player take damage if he collides with an enemy sword and the sword’s property “votile” is TRUE.
Here is my code so far:

import bgeimport GameLogic


cont = bge.logic.getCurrentController()
own = cont.owner
   
coll = cont.sensors["Collision"]
  
badsword = bge.logic.getCurrentController().sensors["Collision"].hitObject


vot = badsword["votile"]
    
if coll.positive: 
    if vot == TRUE:
        cont.activate("damage")



With this code, I get an error that says “nonetype object is not subscriptable”

Any ideas what I did wrong?

In what line?

I suppose that import bge and import GameLogic are not together like that. Why do you even need to import GameLogic, anyway?

Also, are you sure that con.activate works like that? Shouldn’t you do damage = cont.actuators[“Damage”, cont.activate(damage)
EDIT: Alright, you can do it that way…

you need to consider that there will be no object being hit.

if badsword != none:

Ah, yes, as Daedalus said, you should probably do something like this:

import bge

cont = bge.logic.getCurrentController()
own = cont.owner
   
coll = cont.sensors["Collision"]
 
    
if coll.positive: 

    badsword = bge.logic.getCurrentController().sensors["Collision"].hitObject
    vot = badsword["votile"]
   
    if vot == TRUE:        
        cont.activate("damage")

I don’t know if the indentations are right

There is no “TRUE” in Python unless you define it (you did not).

I guess you meant the boolean true-state which is “True” in Python (regardless what the BGE is showing you elsewhere).

Beside of that you do not even need “if value == True:”. You can use the value as condition “if value:”.

your code can look like that:

import bge

controller = bge.logic.getCurrentController()
   
collisionSensor = controller.sensors["Collision"]
      
if collisionSensor.positive: 
    votile = collisionSensor.hitObject["votile"]

    if votile:
        controller.activate("damage")

Small changes:

  • removed unnecessary import
  • fully named controller, sensor to support readability
  • removed own - never used; if so call it owner (makes more sense)
  • renamed vot to votile (why inventing new names for things that already have a clear name? And why invent a name that nobody else knows about ;)?)
  • removed badsword - just used once= can be used directly; are you sure the sensor only collides with badswords?
  • moved hit object reference after checking the sensor status as it is not necessary when the sensor is not positive.
  • replace boolean value check (==True) with direct evaluation (if votile)

I hope this helps a bit.

 
import bge

controller = bge.logic.getCurrentController()
   
collisionSensor = controller.sensors["Collision"]
      
if collisionSensor.positive: 
    votile = collisionSensor.hitObject["votile"]

    if votile:
        controller.activate("damage")

Typo fix :slight_smile:

thank you … edited post#5

This worked great!
Thanks everyone for helping me fix this!

No problem