Results 1 to 14 of 14
  1. #1
    Member J05HU4.blend's Avatar
    Join Date
    Mar 2015
    Location
    don't look behind you
    Posts
    151

    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?



  2. #2
    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.

    Code:
    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



  3. #3
    Originally Posted by Mobious View Post
    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.

    Code:
    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:
    Code:
    lastObj = scene.addObject("Name", Where, Time)
    https://docs.blender.org/api/2.78b/b...cene.addObject
    System "IVAN" (rev 1.3b) - Win7 64bit - Blender 2.74:
    CPU- Intel i3-3220 3.30 Ghz | GPU- EVGA GTX 970 | RAM- GSkill Ares 16GB 1600 Mhz | MB- ASUS P8Z77-V LK



  4. #4
    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.
    Last edited by Mobious; 19-May-17 at 20:49.



  5. #5
    Originally Posted by Mobious View Post
    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.
    System "IVAN" (rev 1.3b) - Win7 64bit - Blender 2.74:
    CPU- Intel i3-3220 3.30 Ghz | GPU- EVGA GTX 970 | RAM- GSkill Ares 16GB 1600 Mhz | MB- ASUS P8Z77-V LK



  6. #6
    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.



  7. #7
    Moderator Monster's Avatar
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    13,676
    @J05HU4.blend how do you create the object? Via actuator or via python?



  8. #8
    Member Nemescraft's Avatar
    Join Date
    Mar 2016
    Location
    Somewhere on Earth
    Posts
    209
    With the actuator:
    Code:
    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:
    Code:
    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
    Last edited by Nemescraft; 20-May-17 at 02:45.



  9. #9
    Member
    Join Date
    Nov 2013
    Location
    Denmark
    Posts
    142
    scene = bge.logic.getCurrentScene()
    obj = scene.objects[-1] # last added object is at the end of the list



  10. #10
    Originally Posted by edderkop View Post
    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?
    System "IVAN" (rev 1.3b) - Win7 64bit - Blender 2.74:
    CPU- Intel i3-3220 3.30 Ghz | GPU- EVGA GTX 970 | RAM- GSkill Ares 16GB 1600 Mhz | MB- ASUS P8Z77-V LK



  11. #11
    Member
    Join Date
    Nov 2013
    Location
    Denmark
    Posts
    142
    Originally Posted by Daedalus_MDW View Post
    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'>



  12. #12
    Member Nemescraft's Avatar
    Join Date
    Mar 2016
    Location
    Somewhere on Earth
    Posts
    209
    And myList[-1] gets the last item in the list? Interesting to know...



  13. #13
    Moderator Monster's Avatar
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    13,676
    Originally Posted by Nemescraft View Post
    And myList[-1] gets the last item in the list? Interesting to know...
    Be aware this is not guarantied. This can change without notification.



  14. #14
    Member J05HU4.blend's Avatar
    Join Date
    Mar 2015
    Location
    don't look behind you
    Posts
    151
    It works, thanks guys!



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •