Use python to get the key of the item that an item is parented to?

Ok, so you’ve figured out that obj.parent gives you something.
The question is, what?

There are two ways to find out. The first is the documentation (yes it’s easiest, hence everyone suggesting it if you know the command.) Open it up, and open bge.types (that has the game object stuff) and then search for ‘parent.’ You get this:

parent
The object’s parent object. (read-only).
[TABLE=“class: docutils field-list, width: 0”]
[TR=“class: field-odd field”]
[TH=“class: field-name, bgcolor: #EEDDEE, align: left”]Type :[/TH]
KX_GameObject or None
[/TR]
[/TABLE]

Notice the ‘type.’ It says that it will return a game object. Not the name, but the object itself.

The other way is to try.
Printing a game object will always print it’s name, but you can be sneaky and use the print command to find out whether it is a game object by running a command for game objects, and seeing if there is an error.
So if I take a string and try to print it’s name:

gameObject = 'thisisnotanobject'
print (gameObject.name)

You’ll see an error along the lines of ‘string object does not have inbuilt function name’

However if you try the same with a game object:

gameObject = cont.owner
print (gameObject.name)

it will print something like ‘Cube.002’
Then you know it’s a game object.


As for the later question about the collision sensor:

  1. please use sensible names (bill and bob??). It makes code more readable, and readability is everything.

So let’s wire up the following logic:
Collision –> Python
and rig something to hit that object.

In that script put:

import bge
cont = bge.currentController()
obj = cont.owner

collSens = cont.sensors['Collision']

I take it you’ve got that far?

Now you can look at the documentation again, and you’ll see under bge.logic (it’s a logic brick), one called ‘KX_TouchSensor’
This has several useful bits of information:

hitObjectThe last collided object. (read-only).
[TABLE=“class: docutils field-list, width: 0”]
[TR=“class: field-odd field”]
[TH=“class: field-name, bgcolor: #EEDDEE, align: left”]Type :[/TH]
KX_GameObject or None
[/TR]
[/TABLE]
hitObjectListA list of colliding objects. (read-only).
[TABLE=“class: docutils field-list, width: 0”]
[TR=“class: field-odd field”]
[TH=“class: field-name, bgcolor: #EEDDEE, align: left”]Type :[/TH]
CListValue of KX_GameObject
[/TR]
[/TABLE]

So if we append onto the end of that earlier code:

print(collSens.hitObject)

We’ll get a single object. The one that most recently hit the scripts owner.

If instead we want something like damage, where it could be hitting 3 or 4 things at once, we can use the other:

colList = collSens.hitObjectList
for col in colList:
    print(col.name)

We can go through each and every object that collided with the script owner.

I highly suggest you look at python tutorials BluePrint, you don’t really seem like you know what you’re doing.

Goran has made the best set of python lessons I’ve seen on youtube. Check them out if you’re interested in actually learning the language, it’ll help you a lot.

Python Tutorial Playlist

It doesn’t teach you about the blender API but it doesn’t matter. Learning the basics of python is much more important and way more useful. After you fully understand the language you’ll be able to use python with blender very easily.

Got it done :slight_smile: