Destroy oldest spawned object.

Get help using Construct 2

Post » Sun Sep 09, 2012 3:24 pm

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
B
4
Posts: 46
Reputation: 617

Post » Sun Sep 09, 2012 3:41 pm

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.TELLES08082012-09-09 15:49:45
ImageImageImageImageImageImage
B
92
S
19
G
12
Posts: 1,192
Reputation: 18,276

Post » Sun Sep 09, 2012 3:57 pm

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

Here is one way to do it:
[code]+on key pressed
---+system compare: Sprite.Count>=2
---+System: pick instance 0 of sprite
------ sprite: destroy
---+every tick
------ system:create sprite[/code]

Another way is to give the Sprite an instance variable "age" with an initial value of 0.
[code]+on key pressed
--- sprite: add 1 to age
--- system:create sprite

+sprite: age>=3
--- destroy sprite[/code]
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713

Post » Sun Sep 09, 2012 4:04 pm

@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."
ImageImageImageImageImageImage
B
92
S
19
G
12
Posts: 1,192
Reputation: 18,276

Post » Sun Sep 09, 2012 4:29 pm

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.

B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713

Post » Sun Sep 09, 2012 6:42 pm

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

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

@R0J0hound
Nice explanation, as always ^^
ImageImageImageImageImageImage
B
92
S
19
G
12
Posts: 1,192
Reputation: 18,276

Post » Sun Sep 09, 2012 10:35 pm

I was looking at using an array for this but perhaps it's a bit too complicated?
B
4
Posts: 46
Reputation: 617

Post » Sun Sep 09, 2012 10:41 pm

Preferably I'd like to replace the oldest object with the player sprite.
B
4
Posts: 46
Reputation: 617

Post » Sun Sep 09, 2012 11:24 pm

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.Vitae2012-09-09 23:29:27
B
4
Posts: 46
Reputation: 617

Post » Mon Sep 10, 2012 10:23 am

I'm yet to find a way to do this. :/
B
4
Posts: 46
Reputation: 617

Next

Return to How do I....?

Who is online

Users browsing this forum: kingpirux and 6 guests