Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411486 Posts in 69371 Topics- by 58427 Members - Latest Member: shelton786

April 24, 2024, 10:45:22 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Detecting Number of Collisions With An Object (GM:S)
Pages: [1]
Print
Author Topic: Detecting Number of Collisions With An Object (GM:S)  (Read 676 times)
thickspit
Level 0
**

devil square


View Profile WWW
« on: August 24, 2015, 07:03:59 PM »

This is my first time going to a forum to ask for code help. Picture me gently weeping. Cry
Now, for the problem:

It's pretty simple:
If three or more blocks (i use obj_block_parent as a parent for all of the different colors) of the same color are adjacent, they get destroyed. The problem is, I can't seem to find a way of detecting the number of instances currently colliding with the calling instance. (And get their id's, because I'd need to do that to if I wanted to destroy them from the calling instance.)

Can someone wiser than I come up with a method to get the job done? I hope I described it halfway well.

~Much Appreciated.

Logged
mokesmoe
Level 10
*****



View Profile WWW
« Reply #1 on: August 25, 2015, 05:19:10 AM »

Code:
var ids, amount = 0;
with(object){
    if place_meeting(x,y,other){
        ids[amount] = id
        amount += 1
    }
}
Logged
thickspit
Level 0
**

devil square


View Profile WWW
« Reply #2 on: August 25, 2015, 12:04:34 PM »

Sorry to bother, but could you elaborate a little? I'm not entirely sure how to use this.
Logged
@Alex@
Level 0
***


View Profile
« Reply #3 on: August 25, 2015, 02:18:55 PM »

Place the code in the step event [Or you can use a collision event, so that you know there is at least 1 collision] of an object you want to know how many objects can in collision with. Replace the object variable with the resource name of the objects your want to know how many colliding with. E.g if this was obj_wall and you wanted to know how many obj_ball were touching it you'd have with(obj_ball).

If you want to know ALL objects, GameMaker's all keyword will work in place of object.

Within the with loop the other keyword refers to the instance 1 level higher, in this case the "obj_wall" object.
It creates an arrays "ids" which contains the instance ids of the colliding objects and increase amount so that you know how many objects are in collision.

After this code is run you have the variable amounts to see the how many instances are colliding and you have an array "ids" telling you which instances.
Logged
thickspit
Level 0
**

devil square


View Profile WWW
« Reply #4 on: August 25, 2015, 05:11:12 PM »

Thanks guys, this was what I needed  Hand Thumbs Up Left Smiley
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic