How to use Custom objects/Prefabs for More advnaced Behaviour

If i was to use the “extract as custom object” thing in GDevelop

how would i do something like, have an enemy 2 collission objects as THAT custom object, then have code check if you touched the collision part of the object, or the enemy part of the object

like for an exaple, check if a bullet hit the enemy, or if it hit it’s sheild for enemies that can only be attacked from behind

or like, if i wanted to animate a character in seperate parts

so the legs have their ow animations and the arms have their own animation that arnt connected to eachother (i know that can be done in another way but its the only example that i have rn)

Detecting the collision between an object in a scene against a custom object from the main event sheet is easy. You can just check for a collision between the scene object and the custom object.

Detecting a collision between a scene object and an object or child object of a custom object is more complicated.

You could add a behavior to the bullet and use a manger to track the bullet from the custom object. I’ve never figured out how this works.

See this post
https://forum.gdevelop.io/t/behaviors-and-behavors/61519

An easier method is to add a function in the prefab object that has a parameter for the bullet object. This function would need to be trigger on each frame.

There’s a complication with this approach. The objects inside a custom object are basically on a different layer so even if the objects visually look like they’re colliding. They won’t actually test as colliding using a condition inside the custom object unless the layers were in sync. Meaning the custom object would need to be at 0,0 within the scene that it’s in.

Now, if the custom object moved then you could place the custom object at 0,0 and create an action inside the custom object that moved the custom object’s children objects. That way, they’d be in sync. 0,0 in the scene would be equal to 0,0 inside the custom object.

If you moved the custom object using the custom objects position actions from the event sheet of the scene that it’s in or a behavior on the custom object then you would need to compensate for the child object basically being on another out of sync layer.

Possible methods.

You could use the point is inside the child object from within the custom object if you used it within the custom object using the bullet object as a parameter that is called from the scene on every frame. The problem is that thus would only check a point not the entire object. The point could be on the tip of the projectile but then you might have issues if the bullet came from different angles.

If the custom object was the player then I guess you could create the bullet inside the custom object and check if the 2 child objects were in collision. This would require a function inside the custom object that create a bullet object at the X, Y inside the custom object plus the offset position of the custom object itself.

I guess, you could also just create a test bullet inside the custom object at the point of collision. Im not sure of the method for that and if it would be efficient.

There’s probably a fairly simple formula to calculate the collision manually using the scene object and the custom object children. You could compare the overall bounding boxes or if the projectile is round, there’s probably a formula for checking if a circle is in collision with a square. Pixel perfect collisions would be more complex. IDK if you could use the bultin collision methods using Javascript.

Now, I think I’m making it too complex. I’m just trying to brainstorm. Not all of my ideas are going to be practical.

It’s late. If you’re interested. I can continue tomorrow or maybe someone else has another idea. Either way, it’s unfortunately not as simple as checking for the collision between objects either inside the same scene or inside a custom object.

Depending on your object it might be as easy as checking the direction of the objects and the relative position.

1 Like

This wouldn’t be too different from the current method. You could create functions inside the custom object to change the animations or you could add a few properties or functions that would allow you to set the animations from the scene that they’re in.

Putting as many relative events as possible within the custom object would sort of automate the object and would keep the main event sheet cleaner and easier to manage.