Damage Script

So I’ve made this script to make my job of adding weapons easier…but I’ve encountered a problem. What the script is suppose to do is this…it collides with a bullet and it registers it damage(which would have a assigned amount). But the there’s a dumb error is that it registers the other caliber’s damage instead of the one it collided with.

Here’s the blend file script…

I’m just asking for someone to fix the script…

http://pasteall.org/blend/index.php?id=46297

Your code is a little bit strange. Basically this is what it does:

  • if the sensor (collision sensor) detects any object, you look for all objects with property “5.56mm” and decrease their health.
  • when the sensor triggers (regardless if positive or not) you decrease the heath of all objects with property “9mm” and “12mm”.

basically when anything hits that object (and when stopping to hit) you decrease the health of all bullets (I assume these should be bullets).

First you need to decide what registers the impact. That can be the projectile or the hit target. As you let the target detect the impact we will continue from there.

Assumptions:

  • more than one projectile can collide at the same time
  • projectiles have a property that tells the damage e.g. “physical damage”:10
  • projectiles are recognized by a property e.g. “projectile”

This makes projectile detection much easier, as you just need to filter for “projectile”.

This means when the sensor detects anything you check all detected projectiles:



projectileSensor = controller.sensors["detect projectiles"]

for projectile in projectileSensor.hitObjectList:
    # Get the damage of the bullet. (it can depend on the speed).
    receivedPhysicalDamage = projectile["physical damage"]
    # Get other properties of the bullet/sender that modify the damage
    armoryPenetration =  projectile["armory penetration"]

    # Let the projectile know it transferred the damage
    # The projectile should deal with the consequences within the next frame. 
    # E.g. it can end itself or it continue and damges more objects
    # We do not care here are this code deals with target damage only
    projectile["hit"] = True 

   # Now deal with the damage. Damage is NOT health decrease. 
   # damage will increased or reduced by further attributes e.g. armory, armor penetration, resistances ...
   finalPhysicalDamage =receivedPhysicalDamage / 1 - 0.0, owner.get("armory", 0.0) # armoryPenetration somewhere

   # now we know the damage so we can adjust the health:
   health = owner.get("health", 0.0)
   health = max(0.0, health - finalPhysicalDamage)

   owner["health"] = health

   if health<= 0.0:
      .... die()


You calculate the damage of each single projectile.

The same belongs to non-projectiles like swords, impacts, explosions, gas.
You can have different types of damages like physical, magical, chemical, fire, ice, radiation.
You can have different duration with different damage rates: immediately, one frame, damage over time, area damage, health percentage damage, impact dependent damage, multi-hit damage, amplified damage, reduced damage, heal …

You see there are an awful lot of options.

So how do I utilize this script? I’m just trying to calculate one bullet caliber…is there a way to make it so if it collides with a certain bullet it just subtracts a certain amount from the property “health”?

This can be done with logic. Collision(“bullet”) -> and -> property(add(health(-1)))

I know, that’s what I currently use. Imagine this…you add a new weapon…that means you have to add the bullet damage to everysingle AI in your game as opposed to just adding a couple of new lines of code in a script.

No, I don’t think you get how this works. It can be a lot easier than how you have it. You are just overthinking it. If you want to do it with python, let me show you how:


from bge import logic

cont = logic.getCurrentController()
own = cont.owner

coll = cont.sensors["Collision"]
bulletList = coll.hitObjectList

for bullet in bulletList:
    if "bullet" in bullet:
        own["health"] -= bullet["bullet"]

connect this to a collision sensor. on your bullets, have a float (or integer) property called “bullet” for the value of the property, set it to a custom number specific to the bullet. This number will be subtracted from the health of the AI.

Thanks RedFrost, nice to know you’re on BlenderArtists…

I’ll see if it works…

That is the reason I would add the damage dealer at the projectile rather than the target.

The target can get a damage handler that processes the received damage.

The reason is simple: Each single projectile knows the specific damage it deals. The target knows how damage influences health.

Oh, well I was only looking for something simple to implement with.

Well, thanks for your help.

Alright thanks for the help and for those who tried to help. Now here’s the dumb question…How do you set this as solved?

edit first post->advanced->prefix->solved

Alright, I got it. Thanks…