Efficient Casting

Hello there fellas

I’m trying to implement a way for me to have three types of objects (Interactive, Inventory and Pickups). Normally I code but I haven’t learned how to C++ yet so I’m going with Blueprint.
What I’d do is I’d have these three be abstract classes, perhaps implementing interfaces with functions in them so that I can call this easily.

Then my idea is that first I use a line trace to check if there is one of the three types of objects. If there is I take the necessary action that you can do to that item if any.
This is where my problem arise. I don’t know how to do this without it being very tedious to implement. Here is a scenario I’d like to have:

  1. Do a Line Trace
  2. Figure out if any of the three types of objects are hit by the line trace.
  3. In this example lets say it’s an interactive item.
  4. I cast the actor reference to an InteractableObject.
  5. ???
  6. Call the RunInteraction() function that is specific to that type of interactable item (like a lever will be different from a door)
  7. The interaction plays out.

How would I achieve this workflow with blueprints? I can’t really figure out how to do this without casting the item I find to a door but then I’d have to find out first if I have the right door and that can only be achieved by doing excessive cast tries and that can be quite infeasible to do if you have a lot of different intractable items.

You can use an interface for that:

But how do I run the overridden function in this scenario?
Or would I simply make this test and then run the interface function on that object?

Hey there,

to add some more information to Mhousse answer, here is a link to the Wiki page that explains how to implement Interfaces in Blueprints:

If you trace different classes that don’t share a base class, you will always be fine with Interfaces. Then you don’t need to cast (:

1 Like

Oh, neat!
Thanks, that helps out.

1 Like

Just to show what I ended up with for doors as a “thank you” I guess :slight_smile:

Glad it helped!