Need some help with enemy management

2 quick questions. I don’t know much python yet, so I’m looking for a bit of scripting help.

1.How do I set up a global variable for the enemy count and have objects check that variable? I have empties adding enemies and I’d like them to stop if there’s a certain number already generated. There’s different classes of enemies, so I’d like to take that into account as well. Then when an enemy dies, it needs to send -1 to the global variable for it’s class.

  1. Is there a good python alternative to the near sensor? It drags down the engine too much and I’m lookiing for a way around it. I thought maybe there was a way to get position of the player and periodically check to see how close it is to the enemy generator (Maybe even having the player position being a global variable as well). Any thoughts?

Also, if anyone has any general tips on creating/managing enemies in the Game Engine, I’d love to hear them. It doesn’t seem to come up much.

Thanks
Chris

  1. Have a script that runs once at the beginning of the game by using a non-pulsed Always sensor (this can be done from an intro scene, or just an object at the start). Put this in it:
GameLogic.enemy1 = 0
GameLogic.enemy2 = 0
#you can name them differently if you want
#continue for number of enemy types

Put this script on the adding empty:

import GameLogic as g #short forming
cont = g.getCurrentController()
addE1 = cont.getActuator("AddEnemy1") #this actuator has to be connected to the Python brick
addE2 = cont.getActuator("AddEnemy2") #and so on...
if g.enemy1 < 4: #or however many the max is
  g.addActiveActuator(addE1,1) #fires the actuator
if g.enemy2 < 3: #or however many the max is
  g.addActiveActuator(addE2,1)
#and so on...

Finally, stick this on the enemies:

import GameLogic as g
cont = g.getCurrentController()
own = cont.getOwner()
if own.health <= 0: #"health" is an object property
  g.enemy1 -= 1 #long form: g.enemy1 = g.enemy1 - 1
  1. You’ll need a global variable that’s an array of size 3 ([0,0,0]) for the objects you want to measure between. If they’re called r and s:
from math import *
distance = sqrt((s[0] - r[0])^2 + (s[1] - r[1])^2 + (s[2] - r[2])^2)

To the first question: it could be done with logic bricks and properties too:D

Also for general tip for enemies (well, even for all npc’s) make sure they all have a basic standing action that is played, even if it is only them breathing. It makes a game much more realistic if you see them constanting moving (even if it’s a slight movement).

Thanks Toomai. That’s really close to what I was looking for. I’m altering it to be more flexible and check the empty for enemy type and limit.

I’m setting up different classes for the enemies. A is small, B is medium, C is large, etc. Each enemy would have a string property named “class” with a value A, B, or C. How would I convert this enemy death script to check the class property and change the corresponding global enemyClass variable?


import GameLogic as g
cont = g.getCurrentController()
own = cont.getOwner()

if own.hp <= 0:
     g.enemyClassA -= 1


The same way you’ve linked to the object’s HP property:

if own.class == "A":
  #etc

My friend just showed me how to set up arrays, which solved most of my problems. I’ve moved all of the variables onto the generators and the enemies and it’s working great. It should be much simpler when I have more enemies made to chose from. Now I just have to tackle the distance part.

thanks