Enemy unit scores

What would be the best way to get the score from a spawned projectile of an enemy unit hitting you (or other units) passed back from the projectile to the enemy that spawned it. There are more than 1 enemy and they would all share the same name being spawned units themselves.

Im thinking i might need to rename each enemy on being spawned and then when spawning the projectile pass that unique name to it so that it can pass back the information to that particular unit or is there another simpler way?

Store a reference to the unit within the projectile at the time of spawning it.

#own = unit
new_projectile = own.scene.addObject("ProjectileName", spawnPoint)
new_projectile["master"] = own

Then you can use this reference on impact to grab or overwrite anything. Say,

#own = projectile
if collision.positive:
    some_value = own["master"]["propertyName"]
    own["master"]["propertyName"] = some_other_value

Or maybe I’m not understanding you correctly. What do you mean by “score”?

1 Like

Thanks, you understood me correctly! One of the BGE users on facebook told me I could store a reference to an object and I implemented it how you suggested :slight_smile:

All this time I’ve been searching the scene for object names I need or using message! Facepalms

Relying on object names is not efficient. It means you want to start a search (searching is always slower than knowing). So keeping the reference (= knowladge) is a good and efficient way.

This way the wish to rename objects is suddenly irrelevant :slight_smile: .