Advanced game/python problem. Help needed!

I’m having trouble with a very common problem in making games and other such things. (allthough maybe not so often specifically like this)

I want to be able to make instances of objects and get Blender or Python to create sepperate properties for each instance when the game begins.

My game is a shoot-em-up where the enemys loose energy if a ray (comming from the bullet) hits the body of the enemy. In other words they dont loose energy if they are hit by a bullet, but rather if the bullet is going to hit them. This is a much beter way of doing things, because if I used the Collision Sensor in the enemy to detect the bullet, there is a big chance that the bullet would just pass through the mesh of the enemy (because of the high speed of the bullet, and because blender only registers collisions every frame).

So this is how it is set up:

The Logic Brics in the bullet look roughly like this:

RAY (property: enemy) >> and >> MESSAGE (loose energy)

The Logic Brics in the enemy look roughly like this:

MESSAGE (loose energy) >> and >> PROPERTY (energy, -1)

This works fine for one enemy.

The problem occurs when I make instances of the enemy. (I want to able to just position emtys where an enemy should appear). When I hit one enemy, they all loose energy! This is because they share the same energy property.

The soluition would be to maybe have a python script that creates sepperate Properties for each instance of the enemy. But there would still be problems regarding the message sensor and actuator, because even if you had a python script that made sepperate properties for each instance and i set up the logic brics in the bullet like this,

[RAY (property: enemy1) >> and >> MESSAGE (enemy1 loose energy)
RAY (property: enemy2) >> and >> MESSAGE (enemy2 loose energy)
and so on…]

there would still be problems in the enemys Logic Brics. How would they now know wich message to recieve?

This is driving me round the bend. I don’t know python, just logic brics.

First of all, I just want to know whether this is at all possible to solve, and secondly, how would one go about it?

Thanks, and regards, Monkeyboi

Hi!
I had this problem, too, but I found another solution.
I gave the enemy simply an integer property and named it “Health”. Then I gave the enemy a string property (“Enemy”).
After that I wrote a little and simple script (one of my first scripts :wink: ) and connected it to a raysensor named “sensor” of my player.
In the property field of the raysensor I wrote “Enemy”.

Now here is the script:

import GameLogic
contr = GameLogic.getCurrentController()
owner = contr.getOwner()
sensor = contr.getSensor("sensor")   #this is the ray sensor of the player
target = sensor.getHitObject()
target.Health = target.Health - 25   #this is the damage value

Not very complicated, but it works. Hope this helps you a little bit! :wink:

Ok, the problem isn’t because all enemies use the same energy property, it’s because they ALL recieve the message. The message doesn’t only get sent to the enemy the ray hit, rather, the message goes to everything that is looking for that message. A much better way to do this, would be to write a script for the BULLET, that takes damage away from whichever enemy the ray hit.


c = GaneLogic.getCUrrentController()
ray = c.getSensor("ray") #The ray sensor must be named ray
delete = c.getActuator("delete")  #An end object actuator named delete
if ray.isPositive():  #Checks to see if the ray saw something
   enemy = ray.getHitObject()   #Gets the enemy that was hit
   enemy.energy = enemy.energy - 1  #Sets the energy property of the enemy to one less
   GameLogic.addActiveActuator(delete,1)  #Delete's the bullet

Try this script. It is pretty self-explanitory, just make sure you connect the ray sensor to a python controller (with the script name in it) and connect the python controller to an end object actuator. Make sure to name the ray sensor “ray” and the end object “delete”.

i dont like/know how to use python
and if you didnt either you could…

make a long rectangular mesh
|=======================|
sorry really bad drawing… anyway… just take a cube and stretch it really long.

make it a ghost with all invisible faces
parent it to the camera/whatever object rotates and give the new mesh the property bullet

so when the AI touches the rectanular mesh(property “bullet”) and receives the message “shot” (or whaever) they lose health

or u could use the python way suggested by the ppl above

Reffering to Saluks answer:

Well thanks but I cant really get it to work how I want it to.

With any number of enemys just positioned on the level i got it to work, but as soon as I moved him to an invisible layer and added some “add enemy” emptys it stopped working.

The enemys do not loose energy. I have named him “enemy” as in your example just to be on the safe side.

What could be wrong? And does the script at all make it possible to add enemys the way I want?

PS
If you are interested you can download my game (with one enemy) in exe form on www.shadeless.dk/3d under Games. The game is called Maharaja. Comments are welcome!

Ug. I think this has to do with rays not seeing objects that have been added. I’d suggest the really long bullet mesh actually, just make the collision mesh of the bullet really long and invisible. It’s the easiest method and works pretty well. What you mentioned above sounds like way too much for the job, and rediculously complicated as well. I dont know, blender still has a lot of weird bugs that need fixing…

Whoa! If you use a long mesh like Ineedanewbike suggested, those bullets will pierce through everything. It seems every solution has a problem