2 objects interaction with only one function

Given 2 objects that interact with each other (can be 2 instances of the same class), usually what will happen is that both objects’ overlap function would active.

Is there a way that instead of the 2 functions activating, only one would? Can be only one of the objects’ overlap functions but can also be a 3rd unrelated function.

I just want one single function to manage the interaction instead of it splitting up to 2…

It depends on what you’re trying to do.
If you share the functions and their purpose (preferably with images), I can give a better answer.

There is likely a way to do this, and chances are having multiple functions handling one overlap would not be the right way anyways.

I didn’t make the function yet as I’m still trying to understand how to deal with the interaction,
but the overall idea (I"ll keep it simple) is that if 2 projectiles for example hit each other , I want a function that would take the 2, and according to their values would determine what would happen (for example one of them still going and the other getting destroyed, lets say both have hp and damage).

In theory I might be able to do something as simple as that with 2 separate functions but for the real - a bit more complex idea. I need 1 function that would get as input the 2 objects that just overlapped and do whatever it wants with them - 1 function that runs 1 time instead of the current overlap function that runs once for every object that overlap.

Okay- bit of a long one, but here’s a step-by-step on how I would recommend doing it:



First, we have a base class that every projectile inherits from.
image

It should have no colliders or anything like that. You may want to add projectile movement to it since all your subclasses would probably want it, but it’s up to you.



Second, we have a pure function that child classes always override.

In this function, the base class passes in all things that they want to trigger damage from. This could easily just be a single collider, but this system allows for multiple. Example on how a child would implement it:



Third, in begin play we would iterate through and bind those triggers to an event we’ve created.



Fourth- the collision checker, said event we’ve created:

We check if it’s a projectile by attempting to cast it to ProjectileBase. If it is, it will lead to a HandleProjectileToProjectileCollision function, otherwise it will lead to a HandleProjectileCollision.

If it is actually a projectile, we need to make sure it isn’t already dead. If the other projectile got the overlap event triggered first, it may kill itself, but because both overlap functions are called on the same frame, and the death function is called the following frame when the tick function next triggers, it will still be valid and cast successfully.



To define those two mentioned functions:

The HandleProjectileCollision function is what you want to override with different types of damage in subclasses. Default implementation could look like this:

HandleProjectileToProjectileCollision, on the other hand, should not be overridden.

It uses a pause death bool we’re going to set up in order to make sure both projectiles receive the damage, and not just the one that dies first. It also uses the HandleProjectileCollision function to deal the damage.



We also want to set up basic damage handling for the projectile- maybe like so:


Making sure to not immediately kill the projectile if death is paused.




All of this is in the base class:

3 Likes

Hi Yuval,

Interesting question.

I implemented your example idea. (“one of them still going and the other getting destroyed, lets say both have hp and damage”)

Instead of passing the info that a hit/overlap event has occurred to a third actor, maybe the 2 actors can manage themselves?

5.1 sample project:
AJ_BattleBallz.zip (37.3 KB)

What I’m wondering now is if both of the balls do damage above the hitpoints, should both of them be destroyed? (Right now if one event is called the other gets destroyed without getting a chance to deal damage)

Hopefully the project can help put this in context!!!

edit: Rokens answer looks awesome, will check it out tomorrow.

3 Likes

First of all, thank you very much for taking your time and answering with such a detailed answer!
I really appreciate it.

Second, if I may ask - wouldn’t this system result in the collision technically being called twice (one for each projectile), same for anything that comes after it? If I understood correctly (referring only to the collision part, not the death), this method is about each projectile taking the damage of the other.

If that’s the case (sorry if I understood the system wrong, I tried to reading it multiple times and that’s what I understood) unfortunately I’m not sure it would work due to the complexity I mentioned earlier which is why I wanted only one function to work for the collision…

I would try to think about it a bit more because maybe that system would also fit. I just hoped there’s a simple way, unreal engine provides an “outside collision checker”.

Once again thank you for all of your effort and your time!

I’ve never heard of an “outside collision checker” in Unreal. Based off the name, I’d assume it checks outside of the collision, but that would be no different than making the collision box bigger. That also adds an unnecessary level of complexity by having a disconnect between set collision box and actually collision.

Oh I did actually forget about the case where both live through the hit, but I’m not sure how you’d want to handle that anyways. You may want to add a check at the end of HandleProjectileToProjectileCollision to check whether or not at least one of them died, and if not, then loop back on the function. Two projectiles passing through each-other is probably not what you want.

Assuming the implementation of above changes:

While yes, the ProjectileOverlap event would happen twice, the function that actually handles the projectile-to-projectile collisions, HandleProjectileToProjectileCollision, would only trigger once. Most of the implementation’s complexity is to ensure both projectiles take damage, and that damage is only applied once, while allowing unique-ness between projectiles through inheritance.

If this doesn’t solve your issue, maybe try to explain what that “complexity” is. There’s likely a solution to allow for it.

1 Like

About the outside collision checker sorry for being very unclear with the name. What I meant is not checking for collision outside of the box, but rather a 3rd class that checks for collision and activates a function each time there’s a collision between 2 projectiles.
The outside was more about not being part of the collision sorry if I wasn’t clear.

For the case both live, don’t worry about it, I can deal with that situation, my main problem was more about the collision activating 2 functions than what the function itself does. I just mentioned what it does as to give extra information, sorry for not being clear about that as well.

As for the last part, I would try thinking about it a bit… The complexity I referred to is mostly special traits of certain projectiles. Like a projectile that would copy the other projectile damage or a “ghost” projectile that just passes through other projectiles.
For these 2 examples it’s possible to use that model, I just wasn’t sure if for every single “special trait” it would work considering there are quite a lot.

Once again thank you very much!

First of all thank you for taking your time to not only answer but add an example project as well, I appreciate it a lot.

Secondly, my only fear is that in more complex situations (I have many different and unique projectile like a ghost projectile that goes through other projectiles, a copy projectile that takes the damage or health of the projectile it hits or a literal wall that always block the other projectile) - 2 functions, each focusing on dealing damage to the current projectile might have certain problems.
I’ll try thinking a bit on the subject as maybe this implementation can also work, just hoped there’s a simple solution that instead of activating 2 overlap functions, one for each projectile - trigger just one function in a 3rd class.

Once again thank you for taking your time to help!!!

last thing, for Rokens answer, I would say both of your answers were actually pretty similar in term of the design (each projectile has a function focusing only on itself taking damage). The big major difference was him also implanting death system to make sure that if one projectile is destroyed there any errors if I understood correctly.

1 Like

I hope you like reading.

So I’ve simplified a lot.



This is the new damage handling system:


Notice the absence of DeathQueued and DeathPaused.
Since the death check is in tick, it will only check the next frame, after both projectiles have gone through their functions. Meaning both functions will fire even if one does enough damage to kill.



This is the new HandleProjectileToProjectileCollision:


This is the default implementation, but it is meant to be overridden.



This is the new collision handler:





Examples of how to use (Ghost, Wall, Copy):


The ghost projectile just needs an overridden AnyDamage function:


This would make it immortal except to GhostHurter damage types. Of course, ghosthurter is not nessecary, and you could just never call the parent to make it actually immortal.

If you want the ghost to also not do any damage to projectiles, you can just override the HandleProjectileToProjectileCollision function to not do anything:



The wall projectile you mentioned earlier could be done like so:


I created a custom damage type called InstantDeath and accounted for it in the damage default implementation, but you could just have a really high number that no projectile could ever take- whatever you do, don’t directly destroy them. That would cause problems.
If you wanted it to also take no damage, just override AnyDamage and have it not do anything.



Here’s how you could implement that copy projectile


The best option out of these would probably be the additive health while still take damage. Else that would be the strongest projectile in the game. With additive it has a proper counter- glass cannons. But at this point it’s more down to design.

1 Like

I don’t know what you say…
I really didn’t expect you implement the examples I gave :sweat_smile: It was just for example, noting less noting more - just to make it clear what I meant by complication interactions.

I’m really speechless.

just… WOW.

I do try my best to be as helpful as I can.
Though, if that didn’t help I guess I can’t do much more than wish you luck in finding a solution.

1 Like