Checking if an interface object is valid

I am implementing an “Interaction Interface” for all the actors in my game that you can interact with that has 3 functions: Start Interaction, Interact, and End Interaction.

I have been able to get it to work successfully, but I’m not sure how to deal with when there’s nothing to interact with.

The character blueprint has a member variable called “Interaction Actor,” which is of the type BPI_Interaction; this has no value by default. If the player enters an interaction collision box of an actor, that actor will set the player’s interaction actor to self. If the player leaves the interaction collision, the actor will set the player’s interaction actor to nil.

To interact, the player presses an interaction button or key, which calls “Start Interaction” on the interaction actor. This is currently working, but if the interaction actor is nil, I get an “Accessed None” error in the message log. It hasn’t crashed anything yet, but I’m wary of leaving this in a final product. Is there a built-in way to check if an interface variable has a real actor value?


I thought of a hack-way solution, where I would have an empty actor class that implements the interface and have that in the world, and rather than setting the value to nil, I would set it to that actor, but it sure seems like there should be a better way.

Change your InteractionActor variable to type Actor, that way you have an Object reference to check if its Null or not (You cant check Null on an Interface), you can still call all your Interface functions on Actor, this is the intended purpose of Interfaces in the first place. Providing interaction between unrelated Objects. You wont need to cast at all.

2 Likes

Thank you for your answer, DevilsD