Easy python question.

How do I say “What’s the name of the object running this script?”
So, I could have a cube that prints “cube”, or whatever.

Any help is appreciated.

#here is a blend

import bge

cont = bge.logic.getCurrentController()
own = cont.owner
print(own)

Attachments

PoliteCube.blend (462 KB)

print(myObject.name)

This will print the name of the object running the script into the console, you can also do myObject[‘Text’] = myObject.name if you want the text to appear in-game.

I think it is a mixture of BluePrintRandom and AceDragons’s solution:

basically it is:


bge.logic.getCurrentController().owner.name

see it in action:


def setNameOfOwnerToText(currentController):
   owner = currentController.owner
   nameOfOwner = owner.name

   owner["Text"] = nameOfOwner

What is the difference to BluePrintRandoms’s solution:
owner

  • is NOT the name
  • it is the reference to the game object itself

It is very important to keep this fact in mind.

The name of a the game object is owner.name which is a string as you expect.

Monster

Thanks! I’ll try this as soon as I have access to a computer that can run blender. :slight_smile:

why does mine work then?


@@BluePrintRandom
Yeah I know it work you can just use

print(owner)

but you have to consider it not String name of object so if you don’t just want to print but you want to check name with some string or keep object name in list you can’t do like this


if 'name' in owner:

because like monster said
owner

  • is NOT the name
  • it is the reference to the game object itself
    so it not string but it is KX_OBJECT class

As Monster said, you’re dealing with a reference to the object, not the name of the object. Try comparing it to the string “Hi my name is cube”—they are not equal. If you try any other string operations on the object, you’ll get a type error, because it is a KX_GameObject, not a string.
The reason why printing works is that when you use print, Python looks for a method in each object called str(), which then returns a string representation of the object, which in this case is the name of the KX_GameObject.

Thank you!

so own.name = “String”
where as print(own) = KX_GameObjec * Converter = “String”

so it’s like adding an extra step as well

Look at the Documentation for print( )

All non-keyword arguments are converted to strings like str() does…

KX_GameObject has a modified built-in method called str, which is called by Python when you try to explicitly convert the class to a string ( with print() or str() ).

It works like this:


class Test:
    name = "My Test Class" # Like how KX_GameObject has "name"
    
    def __str__(self):
        return self.name
    
myTest = Test()
print(myTest)

This will print “My Test Class”.
If you remove the custom str method from it, then it will call the default str method and print something like “<main.Test object at 0x00000000>”.


from bge import logic

cont = logic.getCurrentController()
own = cont.owner

print("Object name: " + own)

Printing own like this tries to implicitly convert it to a string, which returns some error messages in Python. Printing the class object by itself will work, but trying to concatenate it onto a string will break the code. This is one reason you wouldn’t want to rely on Python to convert it to a string in most cases. Using the “name” attribute is the reliable and direct solution if you want to print the object name.