AddObject and set property question

In my game i got 10 different objects(players and AI) that can spawn missiles.
Player 1 should not be able to get hit by his own missile, and the same for other players.

Instead of having 10 missiles on layer 2 to spawn in, one for each player with property ‘player1’, ‘player2’ etc to test on,
I thought if is was possible to set a property on an object at spawn time, so each player can spawn in the same missile object and attach a unique identifier on it, something like

spawnMissile = scene.addObject("missile_ai",aiCarEmptyBack,6000)missile_id = "player_missile" owner[missile_id] = 3

So im just looking for the correct syntax for the above code that obviously doesnt work.


own = cont.owner
spawnObj = scene.addObject("What", "where")
spawnObj["ID"] = "owner:" + own["ID"]

Sweet ! Thank you :slight_smile:

ID = owner.get("ID")
            spawnMissile = scene.addObject("missile_ai",aiCarEmptyBack,6000)
            spawnMissile[ID] = "owner:" + owner["ID"]

The above creates a property on the missile equal to the value in owner property ID, eg. player3
Then i just test for a collision with property player3, and it works.

What if i wanted a value on it as well, maybe to have a damage value to it.

spawnMissile["DamageValue"] = 42


Just insert a string in your object[STRING] and it will create that property if it isn’t already there.

and to further simplify, if you aren’t tied to the idea of object id’s, you can just do something like:

bullet['shooter'] = own

That way if you ever need to reference the shooter object from somewhere else, you do not have to worry about lookup logic to convert the ID to an object. Now the collision property could be something like ‘health’, and then to check whether you are shooting yourself, you just do something like:

shooter = bullet['shooter']
target   = cont.sensors['Collision'].hitObject

if not shooter == target:
    target['health'] -= bullet['damage']

Thank you /bow

In the futura, if i need to do something else with addObject, or something else for that matter. Where would i go about looking?
I looked at
to begin with, but it’s not there.

Or, is it “just” having a basic understanding of how python works that does it? I mean, even someone that knows python well, wouldnt know the code you wrote will add a property in blender.

            ID = owner.get("player") #the value is 3
            spawnMissile = scene.addObject("missile_ai",aiCarEmptyBack,6000)
            spawnMissile["missile"] = ID

On my collision sensor

    cont = bge.logic.getCurrentController()
    owner = cont.owner
    target = cont.sensors['Collision'].hitObject
    test = target.get("missile")

I wanted 3 to be printed.

Edit: nvm, it works now. Just had to put in the if cont.sensors[“Collision”].positive:

In the futura, if i need to do something else with addObject, or something else for that matter. Where would i go about looking?

Unfortunately, you just sorta have to feel your way through it (or atleast that’s my general method). There is no official place to go find useful conceptual stuff like that. Some of it is eluded to in the api reference, some is squirreled away in obscure parts of the wiki, some in posts on this and other forums, but none of these is comprehensive.

As a matter of fact, the collision sensor is considered a touch sensor by BGE, and this is not documented in the API Reference. I just sorta guessed that it would have a .hitObject property because touch and ray sensors do.

Learning the correspondence between what you see in the blender UI, and the python representation at BGE runtime feels (to me) like being the main character in a Dan Brown novel.

Best of luck :P.

Okay, thanks for the answer Hobomatic.

I kind of miss the possibilities that would show up when using c++, or was it in lua back when i made some add ons for WoW.
Anyway, that was awsome - when i wrote the code, a list of possible properties would show up, along with a short description.
I miss that in python.

Thanks for the help guys. Appreciate it.

You simply looked at the wrong place. Check this one:

I agree there is no real clear documentation that tells you that this object is a container of properties. The access is dictionary-like with the property name as key. But you can see it in the provided examples.

This is my logic based missile system :slight_smile:


Missile.blend (739 KB)