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’
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
…
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
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:
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.
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…