# How to safely store a pointer to my UINTERFACE?

**URL:** <https://forums.unrealengine.com/t/how-to-safely-store-a-pointer-to-my-uinterface/282930>\
**Category:** UI\
**Tags:** interface, unreal-engine, UInterface, question, pointers\
**Created:** [May 9, 2014, 5:18pm UTC](https://forums.unrealengine.com/t/how-to-safely-store-a-pointer-to-my-uinterface/282930 "2014-05-09T17:18:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![undercover](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@undercover](https://forums.unrealengine.com/u/undercover)\
**Post date:** [May 9, 2014, 5:18pm UTC](https://forums.unrealengine.com/t/how-to-safely-store-a-pointer-to-my-uinterface/282930/1 "2014-05-09T17:18:49Z")

</div>

Hi!

I have created an UINTERFACE called “MyInterface” i.e. UMyInterface and IMyInterface. A class that implements this interface and inheriting AActor. Then in the world I raycast where the mouse pointer is and get an actor back. I use InterfaceCast to cast it to “IMyInterface” which works good (I can e.g. call my methods). Now I want to store a pointer to my Interface in a way such that I can check something like IsValid because there is no guarantee this actor will live/exist during the whole playthrough.

How is the UE4 interface supposed to be used. If I store the “UMyInterface” I have access to a IsValid method, however then I have no longer access to my methods in “IMyInterface”. How are you supposed to go back and forth between the interfaces. Also how do you get the “UMyInterface” in the first place from e.g. an actor. I have hard to believe that you are supposed to cast the interfaces back and forth between each other instead of using a more type safe approach.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![gmpreussner\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gmpreussner\_1](https://forums.unrealengine.com/u/gmpreussner_1)\
**Post date:** [May 9, 2014, 5:26pm UTC](https://forums.unrealengine.com/t/how-to-safely-store-a-pointer-to-my-uinterface/282930/2 "2014-05-09T17:26:59Z")

</div>

You can store it in a _TWeakObjectPtr\<UYourInterface\> MyInterfacePtr;_  
You can then use _MyInterfacePtr.IsValid()_ to check whether the actor still exists.

If you want your class to take lifetime ownership of this interface, i.e. you do not want the garbage collector to destroy the actor while your class holds a pointer to it, then store it in an _UPROPERTY() UMyInterface_ MyInterface;\* in your class.

---

<div class="post-metadata">

**Author:** ![undercover](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@undercover](https://forums.unrealengine.com/u/undercover)\
**Post date:** [May 9, 2014, 7:34pm UTC](https://forums.unrealengine.com/t/how-to-safely-store-a-pointer-to-my-uinterface/282930/3 "2014-05-09T19:34:12Z")

</div>

I can’t see my interface’s methods when I’m switching to using UUnit instead of IUnit?

---

<div class="post-metadata">

**Author:** ![undercover](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@undercover](https://forums.unrealengine.com/u/undercover)\
**Post date:** [June 7, 2014, 3:50pm UTC](https://forums.unrealengine.com/t/how-to-safely-store-a-pointer-to-my-uinterface/282930/4 "2014-06-07T15:50:26Z")

</div>

My conclusion is that this isn’t possible to achieve in a typesafe way in unreal engine. Dynamic casts are required even though the types are known at compile time, or at least as long as UObjects and the garbage collector are involved.

---

<div class="post-metadata">

**Author:** ![Gnurr](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Gnurr](https://forums.unrealengine.com/u/Gnurr)\
**Post date:** [May 1, 2017, 5:24pm UTC](https://forums.unrealengine.com/t/how-to-safely-store-a-pointer-to-my-uinterface/282930/5 "2017-05-01T17:24:59Z")

</div>

so basically interfaces in unreal are pointless? I don’t get it…

---

<div class="post-metadata">

**Author:** ![gmpreussner\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gmpreussner\_1](https://forums.unrealengine.com/u/gmpreussner_1)\
**Post date:** [May 10, 2017, 7:13pm UTC](https://forums.unrealengine.com/t/how-to-safely-store-a-pointer-to-my-uinterface/282930/6 "2017-05-10T19:13:58Z")

</div>

Sadly, UInterfaces are almost useless, yes. There were some discussions on making them more useful, but it’s not a high priority thing at the moment.

---

<div class="post-metadata">

**Author:** ![undercover](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@undercover](https://forums.unrealengine.com/u/undercover)\
**Post date:** [August 2, 2017, 9:42pm UTC](https://forums.unrealengine.com/t/how-to-safely-store-a-pointer-to-my-uinterface/282930/7 "2017-08-02T21:42:56Z")

</div>

I have an old feature request [here](https://forums.unrealengine.com/showthread.php?27012-Feature-request-Typesafety-for-unreal-interfaces!) also 🙂

---

<div class="post-metadata">

**Author:** ![undercover](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@undercover](https://forums.unrealengine.com/u/undercover)\
**Post date:** [August 2, 2017, 9:47pm UTC](https://forums.unrealengine.com/t/how-to-safely-store-a-pointer-to-my-uinterface/282930/8 "2017-08-02T21:47:45Z")

</div>

Interfaces are not useless but no type safety is supported. You will have to use dynamic casts when accessing interfaces in your application. For more detailed complaints I have regarding this you can read my feature request [here](https://forums.unrealengine.com/showthread.php?27012-Feature-request-Typesafety-for-unreal-interfaces!)
