Get Last Object Created Without Actuator

How do I get the last object created, without using a edit object actuator, Just python?

Or is it even possible?

The easiest way would be to create your own addObject method that calls the scene’s addObject method and saves the returned object somewhere. Then just always use your method to add objects.

lastObj = None

def myAddObject(scene, *args, **kwargs):
    # using a global variable to save the object in this example
    # (don't recommend doing this in practice)
    global lastObj
    lastObj = scene.addObject(*args, **kwargs)
    return lastObj

that looks needlessly complex.

all you need is:

lastObj = scene.addObject("Name", Where, Time)

https://docs.blender.org/api/2.78b/bge.types.KX_Scene.html#bge.types.KX_Scene.addObject

It’s not needlessly complex. Using your own method prevents you have having to remember to save the object every time you make a call to addObject somewhere in your code.

Having your own method also allows to you easily extend it later if necessary. For example, imagine you later decide you also need to keep track of the time the object was created. Using your example, you would have to add additional duplicate code in every single location you call addObject. If you use your own method, you only have to add the additional code there once and you’re done.

thats what class methods and variables are for. i have a spawn function in my base class that handles this for almost all my game objects.

many times i dont want to save the object, just add it, move it or something, then be done.

or, what if your making a list of items. you would want to save your game objects into a special list to destroy quickly later.

theres too many special cases for a basic function to be of use.

I think we both interpreted OP’s question differently.

If he just wants to get the reference to an object he just created, then yes, he only needs to use the provided addObject method as in your example.

If he wants to be able to access the most recently created object at any time or from any location in his code, he should create a function/method that saves the reference to the object somewhere.

@J05HU4.blend how do you create the object? Via actuator or via python?

With the actuator:

import bge
cont = bge.logic.getCurrentController()
add = cont.actuators["add"] #add object actuator
cont.activate(add)
obj = add.objectLasCreated #last object created by this actuator

With Python:

import bge
scene = bge.logic.getCurrentScene()
obj = scene.addObject("Object", "Empty", 0) 
#object created by this line - "Object" is the object to add and "Empty" is the object used to add it

Note that the object to add must be in an inactive layer in both cases

Also, for the actuator the object must be created before using that line, I think, that’s the cont.activate(add)​ line

scene = bge.logic.getCurrentScene()
obj = scene.objects[-1] # last added object is at the end of the list

isnt scene.objects a dictionary? dictionary ordering cant be guaranteed i thought?

it is not a dict.

scene = bge.logic.getCurrentScene()
print( type( scene.objects ) )

output:
<class ‘CListValue’>

And myList[-1] gets the last item in the list? Interesting to know…

Be aware this is not guarantied. This can change without notification.

It works, thanks guys!