Hi, I am looking for an alternative to casting to every possible actor. For example, in my scenario I have a character that can move freely. I have cover actor when character collides with, gets in to cover, I have collectibles when collided can be obtained automatically. And the list goes on. So here is my question. When collision happens, is casting to each possible act is a good way to maintain this or is there a better design for this?
Interface. Set a Blueprint Interface with 1 function, like I_Collided or whatever. Set all your actors to implement that interface, and use the I_Collided event to drive your functions. In the Character BP, send an I_Collided message to the actor the Characted has collided with.
Make generic Parent class that “vehicle”, “collectible” and “cover_exmple” are derived from it,
in the Generic_Parent class have the main generic functionality that is common between all.
Have a function in the parent class that returns a class type, which says what is the actual type of the parent class instance. then the followings will be your steps
Cast other actor to the generic_parent
Do everything that needs to be done for all
call the function in the generic parent which detect and return the actual type of the child class
You’ll have to determine which type of the object that is. I’d use tags for that. E.g. On Event Hit you get the Other Actor’s Tags, get Element 0, and Switch on Name. And depending on that tag you do the respective action.
Yes, ultimately if you have several different actors you’ll have to set some branches sooner or later. Even if you use interfaces, you still have to set a function in each actor, so it’s basically the same, instead you do that in different BPs instead of one.
That was something I thought before but couldn’t decide if it’s the right design choice. Now I’ll go and try it and see if it does do the job on different circumstances.
What if I need to make different acts on the character itself depending on the collided actor? Like if it’s a cover, take cover to that, if it’s a vehicle, get in, if it’s a collectible, collect it?
So in the end, I have to make something like this no matter which design I pick
if cover, take cover, else if collectible, collect, else if vehicle, drive