Destroy oldest spawned object.

0 favourites
  • 14 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • Had no idea how to describe this in just one box of text (the subject box).

    My problem is that I want to create an object every time the player presses a button. Maximum amount of objects is 3. When the player presses the button to create a new object and there is already 3 spawned I want to spawn a new object but destroy the oldest object (the one that was spawned first). If I had LUA I could do this myself but I have no idea how to do it in Construct 2. :P

  • I can suggest you to read this tutorial and download the sample inside it:

    http://www.scirra.com/tutorials/361/understanding-uid-iid-health-of-enemies-and-rocket-smoke-trail

    Then, just check the wanted ID to be destroyed by: Instance.ID = Maximum ID - 2 = Destroy

    Where Maximum ID can be a Global variable.

  • The oldest sprite will have the lowest UID and a IID of 0.

    Here is one way to do it:

    +on key pressed
    ---+system compare: Sprite.Count>=2
    ---+System: pick instance 0 of sprite
    ------ sprite: destroy
    ---+every tick
    ------ system:create sprite

    Another way is to give the Sprite an instance variable "age" with an initial value of 0.

    +on key pressed
    --- sprite: add 1 to age
    --- system:create sprite
    
    +sprite: age>=3
    --- destroy sprite
  • R0J0hound

    This is not the best practice, if he create or destroy anything in the meantime, he will bug his game.

    Ashley words:

    "IIDs and UIDs in the editor don't change depending on the Z order, but they do change if you delete and insert instances. Because of this you should not rely on knowing UIDs or IIDs in advance at all - the instance with UID 3, for example, could change to a different object if you add or delete some things in the layout view. This could easily break your game or make it work differently "randomly" (it will seem random to many users because they won't realise certain things they did changed the UIDs and IIDs). For this reason I don't think UIDs or IIDs should ever be displayed in the editor, in order to discourage this bad practice."

  • The solutions work fine since the objects are created at runtime. Ashley's quote refers to not being able to assume iid or uid of an object in the editor.

    At runtime once an object is created it gets a unique uid that remains the same for the life of that object. IID's at least in this situation are 0 for the oldest increasing in order to the newest. Changing the ZOrder, layer or deleting a mid aged instance (with some older and some newer) at runtime may be cases that might scramble the iid order, but I would have to test those cases to verify.

  • I did a fast sample file using instance variable, preferring avoid UIDs and IIDs.

    scirra.com/arcade/example/1837/dealing-with-instance-variables

    R0J0hound

    Nice explanation, as always ^^

  • I was looking at using an array for this but perhaps it's a bit too complicated?

  • Preferably I'd like to replace the oldest object with the player sprite.

  • on key pressed

    ---+system compare: Sprite.Count>=2

    ---+System: pick instance 0 of sprite

    ------ sprite: destroy

    ---+every tick

    ------ system:create sprite

    Couldn't get this to work. Seemed to destroy the latest object I created and not the oldest.

    http://s12.postimage.org/sp6zoy1b1/coooooook.png

    In LUA I would add every object to a table and just get rid of the first entry in the table and sort the table again. but yea. No idea how to do it in construct 2.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I'm yet to find a way to do this. :/

  • A possible implementation using array

    In the example, I tried to keep a logic closed to the one you showed in your screenshoot, but in the scope of my example, it makes little sense to check the count AND the global variable to make the stone disappear.

    Also, in your screenshot, it picks the newly created instance, it's kind of by design.

    You might try to put a "System: Pick all" before this condition, that would "reset" the picked instances.

  • Thanks, Kyatric. I'll have a go when I get back home. Stuck at work atm. :/

  • Here is a capx of the methods I wrote about:

    http://dl.dropbox.com/u/5426011/examples14/destroy_oldest.capx

    I personally like the instance variable solution, it's very clean.

  • I ended up using Kyatrics method as I think it will be easier to work if I end up building more on this idea. Thank you all for your help and fast replies. :)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)