[SOLVED] GDJS Collision Test Question

Hello all! I’m one of those guys coming in hot from the Humble Bundle. I’m not very interested in the No-Code approach, instead I like me some JavaScript. There’s not a heck of a lotta documentation so I’m struggling. At one point I kinda thought I could always use the No Code to do what I want and look at the JavaScript in the debugger and steal that code if I’m in trouble, but that code is usually kinda mangled and beyond my comprehension!
Um, so I have an Enemy1 and a CoconutDrop object that I wanna check collision for - and it works good in the No-Code. This is generated JavaScript

gdjs.copyArray(runtimeScene.getObjects(“CoconutDrop”), gdjs.Level_321Code.GDCoconutDropObjects1);

gdjs.copyArray(runtimeScene.getObjects(“Enemy1”), gdjs.Level_321Code.GDEnemy1Objects1);

gdjs.copyArray

gdjs.Level_321Code.condition0IsTrue_0.val = false;

{

gdjs.Level_321Code.condition0IsTrue_0.val = gdjs.evtTools.object.hitBoxesCollisionTest(gdjs.Level_321Code.mapOfGDgdjs_46Level_95321Code_46GDCoconutDropObjects1Objects, gdjs.Level_321Code.mapOfGDgdjs_46Level_95321Code_46GDEnemy1Objects1Objects, false, runtimeScene, false);

}if (gdjs.Level_321Code.condition0IsTrue_0.val) {

{gdjs.evtTools.sound.playSound(runtimeScene, “Assets\SFX\SoundFX\BigShot.wav”, false, 100, 1);

}} */

I’m OFC just playing a sound on impact. If I paste this code I get errors and I assume its because of the mangled/generated variable names, so I’ve taken my limited JavaScript knowledge and tried to simplify it to the following:

var Hit = gdjs.evtTools.object.hitBoxesCollisionTest( runtimeScene.getObjects(“CoconutDrop”), runtimeScene.getObjects(“Enemy1”), false, runtimeScene, false );

if( Hit ) console.log(“Hit”);

But I never get a hit. I think I’m not understanding the array that I’m (not?) passing into the collision tester().
Anyone have any ideas?

Thanks in advance!
Lawrence

I can usually figuire it out with trial and error. I’m not at my PC. The best source for me is checking existing user extensions. A lot of them use JavaScript and you can click the extension to see their code.

Have seen these links?

https://wiki.gdevelop.io/gdevelop5/events/js-code

Hey, thanks for getting back. Yes I’ve seen those and other links, but I’m just not enlightened enough to understand what’s in front of me - I guess.
I’ll definitely check the extensions - I shoulda thought of that - Thank-you!

1 Like

I didn’t look at all of your code. I’m still learning. This will work with individual instances.
When I copied and pasted your snippet, I noticed the quotes turned red for me. (invalid character) They were the wrong type of quotes. IDK if it was your code or the way the site displayed them. Gdevelop will accept single or double but not the curly type or whatever they were. IDK if there’s a difference or preference. This uses 2 objects named Batman and Robin

Edit: it must be the site changing the quotes. Let’s try preformatted. (toolstrip, under gear)

const obj1 = runtimeScene.getObjects('Batman');
const obj2 =  runtimeScene.getObjects("Robin");
var Hit = gdjs.RuntimeObject.collisionTest( obj1[0],obj2[0], false );
if( Hit ) console.log("Hit");

Sorry for the multiple edits of the last post. You live and learn.

Note: It helps to add a type line like the following. The editor will give more autocomplete options.

/** @type {gdjs.SpriteRuntimeObject} */

1 Like

Humm, thanks!
I tried what you suggested, and it was back to something I also achieved myself earlier - an error from within:
Cannot read properties of undefined (reading ‘getCenterX’)
at Function.collisionTest (runtimeobject.ts:2267:30)

I gotta believe that it’s not my hitboxes or anything like that, because it works great in no code. I know most people would suggest to just go ahead and use the no code, but I gotta bust this ghost for my own sanity!
I’ll keep at it, and I will for sure look at the extensions source code and see what wisdom I can distil from them - thanks for that awesome tip!

I found this post and modified it. My version compares the first instance of Batman [0] to all of Robin. It prints Hit but also rotates the object.

[SOLVED] JavaScript detect collisions with a group of objects - How do I…? - GDevelop Forum

/** @type {gdjs.SpriteRuntimeObject} */

const Obj1 = runtimeScene.getObjects('Batman');
const Obj2 =  runtimeScene.getObjects("Robin");

var in_collision = false;

for ( const Obj of Obj2){
 
 in_collision = gdjs.RuntimeObject.collisionTest(Obj1[0], Obj, false);

if( in_collision ) {
    console.log('Hit');
    Obj.setAngle(Obj.getAngle()+10);
  }
}

BIngo! Thanks a million @Keith_1357 , that seems to be working.
I’m off to heaven on a golden chariot now!

Take care and Merry Christmas!

1 Like

You’re welcome. Merry Christmas.