I have an object called unit1, and if it collides with unit1, both of them need to be deleted, and unit2 needs to be created. But how do I know that unit1 is colliding with unit1, and how do I delete them later?
What I usually do goes like this:
For every unit1 object,
Assign a unique id variable, then create a ācolliderā object as well and link them together, and give the collider the same id variable as the unit1 object itās linked to.
Now youāll have to detect collision between unit1 objects and collider objects that donāt have the same id variable.
Thank you for the advice, but Iāve already managed to implement this using JavaScript without any additional collider objects. Later, Iāll explain how I did it.
Ah yes, please do. That would be quite useful. The collider method does seem a bit crude, if I do say so myself.
But this method is only suitable for games of this type:

I used the cursor position and the UID of the objects, which is not suitable in most cases.(but itās a bit glitchy)
But Iāll try to make it possible to detect when an object collides with itself.
You can definitely do the above, but this works OOTB:

I tried it, but it doesnāt work.
I think you could try using Custom Objects, maybe by creating a single mask that works as a sprite proxy. Iām suggesting this because with Custom Objects you could use āOn Destroyā to create the unit2.
As for the collision check, I donāt think you should have problems, as long as you add an ID or something similar when creating them, so you can distinguish between the different instances.
disclaimer: iāve never used any āon destroyedā event yet..so i dunno how they behave.
I see⦠if I understand it correctly, you technically used the cursor as the collider. Thatās a great idea. Youāre right though, that it canāt be used in most cases.
As for same type object collision, would it be possible perhaps to assign a āunit typeā variable and reference them along with their uid? It seems kind of redundant considering theyāre already the same type object, but just throwing it out there.
Hmm. I literally tested this in a new project and it worked fine. Do you have other events that may be picking other instances too?
No, I donāt have any other events related to this object.
If anyone is interested, hereās the code for my extension.
IDofCursorOnObject:
Arguments:
obj: Any object
var obj = eventsFunctionContext.getObjects("obj");
var ids = [];
var curX = gdjs.evtTools.input.getCursorX(runtimeScene);
var curY = gdjs.evtTools.input.getCursorY(runtimeScene);
var objectName = obj.length > 0 ? obj[0].getName() : "";
var allObj = objectName ? runtimeScene.getObjects(objectName) : obj;
function curOnObj(gdobj, cx, cy) {
var L = gdobj.getX();
var R = gdobj.getX() + gdobj.getWidth();
var T = gdobj.getY();
var B = gdobj.getY() + gdobj.getHeight();
return cx >= L && cx <= R && cy >= T && cy <= B;
}
for (var i = 0; i < allObj.length; i++) {
if (curOnObj(allObj[i], curX, curY)) {
ids.push(allObj[i].getUniqueId());
}
if(ids.length > 1){
break;
}
}
eventsFunctionContext.returnValue = JSON.stringify(ids);
DeleteById:
Arguments:
obj: Any object
ID: Number
var obj = eventsFunctionContext.getObjects("obj");
var id = Number(eventsFunctionContext.getArgument("ID"));
var objectName = obj.length > 0 ? obj[0].getName(): "";
var allObj = objectName ? runtimeScene.getObjects(objectName) : obj;
for(var i = 0; i < obj.length; i++){
if(allObj[i].getUniqueId() == id){
allObj[i].deleteFromScene();
}
}
YbyID:
Arguments:
obj: Any object
id: Number
var id = eventsFunctionContext.getArgument("id");
var obj = eventsFunctionContext.getObjects("Object");
var objectName = obj.length > 0 ? obj[0].getName() : "";
var allObj = objectName ? runtimeScene.getObjects(objectName) : obj;
for(var i = 0; i < obj.length; i++){
if(allObj[i].getUniqueId() == id){
eventsFunctionContext.returnValue = allObj[i].y;
}
}
XbyID:
Arguments:
obj: Any object
id: Number
var id = eventsFunctionContext.getArgument("id");
var obj = eventsFunctionContext.getObjects("Object");
var objectName = obj.length > 0 ? obj[0].getName() : "";
var allObj = objectName ? runtimeScene.getObjects(objectName) : obj;
for(var i = 0; i < obj.length; i++){
if(allObj[i].getUniqueId() == id){
eventsFunctionContext.returnValue = allObj[i].x;
}
}
I have reconfirmed in another project that my method works, are you on the newest version?
This is the entire event sheet
I added a variable to one to control it via the mouse, but Iāve also tested with the top down behavior using the same variable and it works too.
Your method seems extremely complicated for the same effect, so I want to try to understand what may be going wrong on your side.
Start of scene:
Moving the box around:
After touching one of the boxes:
I even set up an object creation like you mentioned:
Edit: I also confirmed Trigger once isnāt needed, either.
It uses a bounding rectangle for collisions.
I have the latest version of the engine.
ok ā¦this may appear over complicated but i enjoy creating problems to solve!
I do this sort of thing (i do sometimes refer from another object too).
this looks complicated because thereās a distance check first to isolate and run the checks on the nearest objects. currently this checks if obj with id 1 is in collision with the others using point inside and then only deletes the other object. you could adapt it to work in other situations or view it as ridiculous and ignore it completely.
It saves the ids of the nearest objects and checks if the saved points are inside those objects. It seems to work now for multiple objects but ive probably made a mistake somewhere.
heres the points
it would be great to be able to reference the corners of the collision mask in events (for the benefit of this highly specific (and probably never repeated) situation) just spotted that the delete needs moving below create bomb.
I sensed that many of you were desperate to see it in actionā¦
Interesting solution (though a bit complicated, and I donāt like dealing with points), but Iāll probably release an extension that accesses objects by UID and will probably be able to detect collisions even between three or more objects using an array variable.












