Count objects returned by an event, cleaning up duplicates

Discussion and feedback on Construct 2

Post » Mon Nov 07, 2016 5:32 pm

When you narrow down objects from an event like say

x => x is overlapping y

Is there an easy way to get the count of x that meets that criteria? I want to know HOW MANY Xs are overlapping Y.

Also I have a level maker that stacks floor pieces on top of each other sometimes. This is a natural byproduct of the level generation. I'd like to be able to clean it up though.

Something like

for each floorsprite
if floortsprite is overlapping another floorsprite (in floorsprite family), then destroy only the top floorsprite.

This is kind of difficult, because there's no way of knowing during the same loop whether a thing has been destroyed or not. It ends up destroying BOTH floorsprites.

Then, assuming I can only pick one how to get JUST the one on top?
Follow my CZero development on twitter https://twitter.com/CaptainZeroGame
B
62
S
17
G
5
Posts: 171
Reputation: 8,329

Post » Mon Nov 07, 2016 5:57 pm

For the first you can use x.pickedcount.

For the second if it's tile based i'd use and 2d array for the grid and use a "for each sprite" to add each sprite to the array and if a spot is already occupied just delete the lower one.

Or you could finagle picking to do it. A condition like "sprite is overlapping sprite" will pick all the sprites overlapping but that isn't what you want, and putting that after a "for each sprite" won't work cause then it would only check for collision with itself because only one instance would be picked.

The simplest way would be to make two families of the same object types (or only one family if only one type is needed). Then do an event like this:

for each family1
family1 is overlapping family2
pick top family2
--- family1.zorder > family2.zorder
------ destroy familly1
--- else
------ destroy family2
B
92
S
32
G
107
Posts: 5,274
Reputation: 69,959

Post » Mon Nov 07, 2016 11:02 pm

x.pickedcount! OMG. Thanks! I knew there was something like that but I couldn't seem to find it...


I tried something like that with Object vs Family (of same object) and it still ended up destroying both but I haven't tried two families yet...

Friggn pickedcount. #$%^ haha...
Follow my CZero development on twitter https://twitter.com/CaptainZeroGame
B
62
S
17
G
5
Posts: 171
Reputation: 8,329

Post » Tue Nov 08, 2016 11:05 pm

OK Pick top does the trick! Man I've been wrestling with this for a while.

https://drive.google.com/open?id=0BwvuQqGQSOpMMDZYVmRxZlRTVWM

The circles and squares are different frames of the same object.

I made a grid of circles and sprinkled some frames on top of them...

Left click anywhere to "clean up" the boxes from on top of the circles. It works without cleaning up the circles too.
Follow my CZero development on twitter https://twitter.com/CaptainZeroGame
B
62
S
17
G
5
Posts: 171
Reputation: 8,329

Post » Tue Nov 15, 2016 11:30 pm

OK this is a bit frustrating. There seems to be no reliable way to put a "wait" command in a loop and have it behave like a procedural language would behave.

For example, in the above capx, what if instead of having it call the clean function when you click the mouse, you wanted it to call the clean function, wait 0.1, call it again, wait 0.1 again... say, 4 or 5 times?

You can't go

repeat 5 times
call "clean"
wait 0.1

It only goes once.
Follow my CZero development on twitter https://twitter.com/CaptainZeroGame
B
62
S
17
G
5
Posts: 171
Reputation: 8,329

Post » Tue Nov 15, 2016 11:40 pm

Wait does not block. It puts any events underneath it to the side and runs them after the wait period. You could do:
Clean
wait
clean
wait
clean
wait
clean
wait
clean

Or use a Timer with a counter.
ImageImageImage
B
71
S
21
G
231
Posts: 3,688
Reputation: 129,352

Post » Wed Nov 16, 2016 4:41 pm

That first method is what I end up using, it just seems unruly code wise. That's why I think we need a Sync command (or something) that just updates all the objects right then.

Timer with a counter... that sounds interesting.
Follow my CZero development on twitter https://twitter.com/CaptainZeroGame
B
62
S
17
G
5
Posts: 171
Reputation: 8,329


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 9 guests