Tell how many of object A overlap object B

Get help using Construct 2

Post » Tue Feb 14, 2012 9:21 pm

That's what I'm trying to do. There's many object A's flying around, and a few stationary object B's. I want to tell how many A's are within a certain distance of each B at any time. It can either be within a certain distance or overlapping an invisible radius, either one will do. This is what I already tried:



That works great... if there's a single object B. Once there's more than one, the counters start flipping out and it doesn't work at all. I can see why it does that, I just don't know how to get around it... Any ideas?
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Tue Feb 14, 2012 9:24 pm

Can you just encapsulate this entire thing in an outer For Each Object B call? You want to check each B against the entire list of Object A.

EDIT: Keep in mind that a single object A could be in range of more than one Object B though. So if you want to account for this, you'll need to have an Object A boolean instance variable HasBeenCounted to make sure that each object A can only get "caught" once.cacotigon2012-02-14 21:27:50
B
30
S
6
G
8
Posts: 232
Reputation: 6,119

Post » Tue Feb 14, 2012 9:27 pm

Oh, duh, forgot to say I also tried that. That screencap is from a barebones capx I was using to test stuff.

The problem is that ObjectA's private var, Overlap, can only tell if it's overlapping ANY ObjectB, not a specific one. So if A is overlapping B1, and not B2, A's Overlap is 1, so for B2 it's constantly setting A's Overlap to 0 (and B1 sets it back to 1) and subtracting from its own Overlap.

I don't know if that explanation made any sense, but that's what appears to be happening.

EDIT: I see what you mean about the boolean, but I'm not sure exactly how to set it up... I'll post the test capx if anyone wants to mess with it.SullyTheStrange2012-02-14 21:29:41
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Tue Feb 14, 2012 9:29 pm

Code: Select all
+System: every tick
  -> ObjectB: set Overlap to 0
+System: foreach ObjectB
  +System: foreach ObjectA
    Local variable else = 1
    +System: distance(ObjectA.X,ObjectA.Y,ObjectB.X,ObjectB.Y <= 300)
      ->ObjectB: Add 1 to Overlap
      ->System: set else to 0
    +System: else = 1
    +ObjectA is overlaping Radius
      ->ObjectB: Add 1 to Overlap



There's only one issue
The radius sprite... how do you associate it to each ObjectB ?
Last edited by Yann on Sun Sep 14, 2014 11:27 am, edited 1 time in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Tue Feb 14, 2012 9:34 pm

I saw your post pre-edit, Yann, and already put it in and it looks like it's working perfectly. What's the problem with the first version?
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Tue Feb 14, 2012 9:36 pm

In your attempt you don't filter specific instances of ObjectB

so the distance check will be true as soon as there's an ObjectB close...
Also System expression doesn't filter the SOL.
So it will change all the overlap variable of all instances of ObjectBYann2012-02-14 21:38:31
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Tue Feb 14, 2012 9:40 pm

oh yeah, I see what you're getting at. Bleh, that *IS* annoying. Maybe something like this, a second boolean for keeping track if A is already in a "overlapping state" with any B this tick.

example:
For(B) --> B.Overlap = 0
For(A) --> A.AlreadyOverlapsThisCycle = false
    if A overlaps B
      B.Overlap++
      A.AlreadyOverlapsThisTick = true
    if A !overlaps B
      if !AlreadyOverlapsThisTick then A.Overlap = false

cacotigon2012-02-14 21:43:40
B
30
S
6
G
8
Posts: 232
Reputation: 6,119

Post » Tue Feb 14, 2012 9:42 pm

Yann can you explain what you mean by:
"Also System expression doesn't filter the SOL."?
Are you referring to the System distance call? Are you saying that it doesn't act on the current SOL list?
B
30
S
6
G
8
Posts: 232
Reputation: 6,119

Post » Tue Feb 14, 2012 9:46 pm

Well, I dunno exactly what you mean Yann, because like I said... the first version you gave me works perfectly. It doesn't have to be absolutely flawless anyway, since it's for a minor feature in a minor game that I have to finish building for Thursday.

So uhh, thanks a bunch!
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Tue Feb 14, 2012 10:03 pm

Nevermind, I think Yann is referring to this from the manual:

"The System object

In Construct 2 the System object represents built-in functionality. It has no instances. This means most system conditions do not pick any instances: they are either true or false. If they are false the event stops running, otherwise the event continues without the picked instances having been changed. There are exceptions, though: if a system condition uses an object, such as Pick random instance, that will affect the picked objects.

System actions do not run on any picked objects: they simply run if all of the event's conditions were met."
B
30
S
6
G
8
Posts: 232
Reputation: 6,119

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 19 guests