Let’s say I have an enemy with 100 health and every time my bullet hits him he loses 10 health. So I’m testing for collision to see if my bullet hits him… and I’ve got that. The bullet deletes itself. But I want to refer and decrease the health of the enemy (a property of enemy). And than once the health is 0 i will delete the object or switch to death animation. So how do I refer to a property of one object from the function of another object.
Not to sure on how to store and point to values properly in python across objects.
The bullet is a property of the player… i suppose. Well it’s a function in the player.
small fast travelling objects produce problems on collision (they potetntial fly through the wall).
Solution: use a ray to find the point of impact. WHich means you do not even need a bullet. But this is ok, as you wouldn’t do not see it anyway.
So now to your problem:
the ray = ray sensor provides you with the object it hits = raySensor.hitObject
on collision (if you use a bullet) a collision sensor provides you with the object it colides with = collisionSensor.hitObject
when you have the hitObject you can check if it has a property e.g. health
if so you can decrease it:
def processHit(cont):
sensor = cont.sensors[0] # assumed that the first sensor is a ray or collision sensor
if not sensor.positive:
return
hitObject = sensor.hitObject
if not hitObject:
return
health = hitObject.get("health")
if health is None:
return
newHealth = health - 1
if newHealth < 0:
hitObject["dead"] = True
hitObject["health] = newHealth
If I get your first questions right, you can use a property sensor on the enemy object and test the health value if its less than equals 0. Then the sensor will send a pulse when the health is 0 or lower. (In case he has 5 health and loses 10 health. Then he would have -5)
Btw, Monster posted the post over while I was writing xD
I understand the logic used, and thanks alot for the helpful reply.
I’ve run into a problem though with actually getting the sensor.
I’m still using an object for my bullet with a ray sensor turned on as well.
In the script for the bullet, I was using rayCast to detect collision with my ‘enemy - fly’. I’m able for it to print a string “HIT” and than delete itself to confirm the HIT.
I tried to include in that block of code the info you said:
But for some reason it keeps referencing my Always sensor (using this to run the python script… mainly for motion and init ).
I have it placed correctly, I believe. Anyway, what were you saying about implementing shots without objects? What about tracking the bullets and controlling speed.
cont.sensors[0] gives you the first sensor connected to the current controller.
con.sensors[“sensorsName”] gives you the first sensor with the name sensorsName.
with following code you get the first sensor that has hitObject:
def getSensorWithHitObject(cont):
for sensor in cont.sensors:
try:
hitObject = sensor.hitObject
return sensor
except AttributeError:
continue
return None
You can call it with that:
hittingSensor = getSensorWithHitObject(cont)
if sensor is None:
return
hitObject = hittingSensor
So I’ve got the right sensor now… and it’s showing me that it’s hitting. However, hitObject is None. So when I do hitObject.get(“health”) it has no property .get