how do I use the ID of the objects in the game engine?

Regards to all :wink:

how do I use the ID of the objects in the game engine?

I know how to take the ID, but I can not use it as an object,
for example the object “Cube” launches this python:

import bge
c = bge.logic.getCurrentController ()
ob = c.owner

print (ob)
#>Cube

print (ob.name)
#>Cube

Up to now we have … now I try with the ID …

ObID = id (ob)

print (ObID)
#> 171025440

print (obID.name)
#> AttributeError: ‘int’ object as no attribute ‘name’

why?
how do I let him know that 171025440 is an object?

Thanks in advance

Well, as far as I can tell, the id() function just returns the number of the unique object - it’s not a reference to the object itself. If I were you, I’d just use the pointer to the object itself:

logic.me = b # Store the reference to this object
print (logic.me) # logic.me = b, which = Cube
logic.me[‘whoami’] = ‘A Cube’ #logic.me = b, so logic.me[‘whoami’] = b[‘whoami’], which = ‘A Cube’

Sorry about the double post. My internet tripped up.

but “me”, is a function?

I also tried adding “bge.” but does not go

but you are able to recall, for example:
worldPosition
worldOrientation
name
etc. etc. …

From The ID?
I need the ID when there are 10 … 20 cubes, being able to distinguish them one by one

if I make an object “cube”, with 10 balls (through a script … not only the parent), we turn around
Then I start the game engine five Cube …
(5 cubes, 50 balls)
if I can not retrieve the ID (and related variables) of the cube, all the balls will spin around a single cube

If you’re using add object

use this code
import random
addobject = cont.actuators[“Edit Object”] #replace this with the addobejct actuator
ob = addobject.instantAddObject()
ob[“special_id”] = random.random(1,40)

@agoose - That won’t work. It’s entirely possible for two objects to have the same ID number, as it is a random number between 1 and 40.

@Marco - Well, ‘me’ was just a variable that I used to show that you can store object references. As for your original question / problem, you want to assign a unique ID individually to each object to tell them apart, right? There is another way to fix your problem without using individual IDs - just store which cube was created, then create the balls and tell them which cube they belong to. There’s no reason to use unique ID numbers:



sce = logic.getCurrentScene() # Get the current scene
obj = logic.getCurrentController().owner                   # Get this object running this script

for x in range(5): # Do the following 5 times (adds 5 cubes, and 10 balls per cube); the x variable here is created, but isn't used. It's simply used for counting.

     cube = sce.addObject('Cube', obj) # Add a cube (is done 5 times)

     for y in range(10): # Do the following 10 times

          ball = sce.addObject('Ball', obj) # Create a ball (is done 10 times per cube, as you can see above)
          ball['mycube'] = cube               # Assign a variable to the ball telling it which cube it belongs to


That’s it - each ball will have a ‘mycube’ variable that points to its individual cube. Also, each cube will only have 10 balls. You can also parent the balls to the cube, if you want:



sce = logic.getCurrentScene() # Get the current scene
obj = logic.getCurrentController().owner                   # Get this object running this script

for x in range(5): # Do the following 5 times (adds 5 cubes, and 10 balls per cube)

     cube = sce.addObject('Cube', obj) # Add a cube

     for y in range(10): # Do the following 10 times

          ball = sce.addObject('Ball', obj)
          ball.setParent(cube)    # Parent the ball to the new cube


And, you can store in each cube which balls are its own:



sce = logic.getCurrentScene() # Get the current scene
obj = logic.getCurrentController().owner                   # Get this object running this script

for x in range(5): # Do the following 5 times (adds 5 cubes, and 10 balls per cube)

     cube = sce.addObject('Cube', obj) # Add a cube
     cube['balls'] = []    # Keep track of the balls in the cube

     for y in range(10): # Do the following 10 times

          ball = sce.addObject('Ball', obj)
          ball.setParent(cube)
          cube['balls'].append(ball) # Add the new ball (and each of the ten balls) to the cube's list of it's own ball objects


Solarlune, it works, but it assumes that he won’t add more than forty cubes.

Are you sure it works, agoose? It’s just random numbers, so isn’t it possible that there could be a situation where the same number is used twice? For example:


ob = addobject.instantAddObject()
ob['special_id'] = Random.random(1, 40) # special_id = <b>9</b>
ob = addobject.instantAddObject()
ob['special_id'] = Random.random(1, 40) # special_id = 37
ob = addobject.instantAddObject()
ob['special_id'] = Random.random(1, 40) # special_id = 2
ob = addobject.instantAddObject()
ob['special_id'] = Random.random(1, 40) # special_id = 16
ob = addobject.instantAddObject()
ob['special_id'] = Random.random(1, 40) # special_id = <b>9</b>
ob = addobject.instantAddObject()
ob['special_id'] = Random.random(1, 40) # special_id = 4
...

Seems to me that since it’s random, each number could theoretically be used more than once, making the ID non-unique.

well except of using random numbers, just use property wich counts up. This way, no doubles can be generated. Also for saving the objects on disk, this works fine, id(obj) is useless for this issue, because it always creates a new number…

Sorry both of you, long day!
What i meant to say was that you could use a bigger range of random numbers, then check if the number does not exist before assigning it. All because i couldn’t be bothered to write some code that has a simple counter (var+=1)!!!
Sorry again, but yes sevi, indeed.

yes, it works perfectly!:eek::cool:
I’ve spent many hours to test it (when you do not know what you’re doing is all complicated …:rolleyes:)

especially what I needed to do from the objects with the right setup was this:

#

obj = c.owner
addBall = scene.addObject = (“Ball”, obj)
addBall[“prop”] = obj

#

for these 3 lines, it took 3 “gooooogle’s Day”(more…more…:spin:)

thanks a lot!:wink:

for testing…

#################################################
#################################################
#####obj “Player1” launch python

import bge
c=bge.logic.getCurrentController()
ob=c.owner
sce=bge.logic.getCurrentScene()

addCube=sce.addObject(“Cube”,ob)
addCube[“remember”]=ob

print(ob.name),print(ob.position)
#Player1
#Vector(11.0, 0, 15.0)

#################################################
#################################################

#################################################
#################################################
#####obj “Cube” launch python

import bge
c=bge.logic.getCurrentController()
ob=c.owner

print(ob[“remember”].name),print(ob[“remember”].position)
#Player1
#Vector(11.0, 0, 15.0)

##################################################
##################################################

I had thought of doing a “fake ID” with random numbers, but it would be a good solution …
because if you assign to a property,
then to be used must be searched item by item increasing much the calculations for the CPU…

thanks anyway :wink:

in short, problem solved

or rather, the ID is saved with the object (even if somewhat 'hidden) …
so do not need to convert the ID