#Global collision event

8 messages · Page 1 of 1 (latest)

marble owl
#

Is there a way to handle all collisions happening in the world from a single script?
It's useful for making sound on collision

sand river
#

Depends on the implementation, I suppose. If you're talking like instancing items bouncing off a wall you can put a function inside the instanced object's script to play an audiostream when is_on_wall == true and put it in the process(delta). If you are only dealing with a player making noises you can similarly do the same but do is_on_wall == true or if it's interacting with collision bodies or area2ds it can enter their shape you can connect the object to itself, and it will come out of the box like _on_entered_body(body) and you can call a function on the entity that entered it like body.sound_func(). If you're talking like swinging an axe against a tree, the player can house all the sound code and only execute it when doing that action, and signals can handle different sounds when swining collides with different objects

#

It can be done but most people would probably call it spaghetti if you have different objects that have different sounds played at different times and you put all of it into a single script

marble owl
#

is there a global event when something in the scene has collided?

old night
#

You should be able to use signals for that, for example with a CollisionShape node

deep mirage
#

@marble owl I don't know if you can leverage the physics engine in that way, but I can only assume if you could, you would start in physics server.

sand river
# marble owl You have 100 rigid bodies in the world and you are going to attach 100 individua...

If the object is instantiated into the scene (aka reused)? Yes. Say for example I have a object created above the screen at a random x interval and it falls into water on screen. I would put a script on the object with an audiostreamplayer as a child of the object in its own scene. Create a function like func splash(): $AudioStreamPlayer.play(). Create an area2d with a collision on the water in a new scene, connect a script for on_body_entered(body) to the water and call body.splash() inside the function. That being said there any many different ways to achieve the same result, some are better for one implementation over another but I don't think there's a way to give you an accurate answer without any idea of what you're doing. If you have 100 rigid bodies that arent instatiated then there's likely a better way to handle them, are they all the same object, do they have various physics applied or are they uniform, does the sound need to be a one off or do you need to implement multiple states of the sound handling like if it connects but bounces off vs connecting and sliding down the object