# Instancing interface to TScriptInterface property

**URL:** <https://forums.unrealengine.com/t/instancing-interface-to-tscriptinterface-property/359013>\
**Category:** UI\
**Tags:** interface, tsubclassof, unreal-engine, UE4, CPP, question\
**Created:** [May 30, 2016, 8:26am UTC](https://forums.unrealengine.com/t/instancing-interface-to-tscriptinterface-property/359013 "2016-05-30T08:26:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![GPSProlapse](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GPSProlapse](https://forums.unrealengine.com/u/GPSProlapse)\
**Post date:** [May 30, 2016, 8:26am UTC](https://forums.unrealengine.com/t/instancing-interface-to-tscriptinterface-property/359013/1 "2016-05-30T08:26:55Z")

</div>

I have two properties:

```
UPROPERTY()
	TScriptInterface<ICoordinateSystem> CoordinateSystem;

UPROPERTY(EditAnywhere)
	TSubclassOf<UCoordinateSystem> CoordinateSystemType;

```

How can I instantiate CoordinateSystemType to CoordinateSystem?  
I have tried different ways like

```
CoordinateSystem = NewObject<ICoordinateSystem>((UObject *) GetTransientPackage(), *CoordinateSystemType);

CoordinateSystem = NewObject<UCoordinateSystem>((UObject *) GetTransientPackage(), *CoordinateSystemType);

CoordinateSystem = Cast<ICoordinateSystem>(NewObject<UCoordinateSystem>((UObject *) GetTransientPackage(), *CoordinateSystemType));

```

but none of them compiles saying something like it can’t convert UObject\* to ICoordinateSystem\* or ICoordinateSystem\* to UObject\* in either NewObject or TScriptInterface = operator.

---

<div class="post-metadata">

**Author:** ![SolarVelocity](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SolarVelocity](https://forums.unrealengine.com/u/SolarVelocity)\
**Post date:** [June 18, 2016, 10:55pm UTC](https://forums.unrealengine.com/t/instancing-interface-to-tscriptinterface-property/359013/2 "2016-06-18T22:55:40Z")

</div>

Not sure if you are still looking for an answer to this, but since Ive just struggled through it might as well have an answer here for you or anyone else

I assume you you have some existing pointer to an object type that implements ICoordinateSystem

Try this:

```
MyTypeThatImplements* myPtr = blah;

if (myPtr->GetClass()->ImplementsInterface(UCoordinateSystem::StaticClass()))
{
    CoordinateSystem.SetInterface(Cast<ICoordinatSystem>(myPtr));
    CoordinateSystem.SetObject(myPtr);
}

```

This will correctly instantiate your TScriptInterface property

You can then call ICoordinateSystem interface methods as

```
if (CoordinateSystem != nullptr)
{
    CoordinateSystem->Execute_MyInterfaceMethod(myPtr);
}

```

HTH

---

<div class="post-metadata">

**Author:** ![GPSProlapse](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GPSProlapse](https://forums.unrealengine.com/u/GPSProlapse)\
**Post date:** [June 19, 2016, 2:40pm UTC](https://forums.unrealengine.com/t/instancing-interface-to-tscriptinterface-property/359013/3 "2016-06-19T14:40:18Z")

</div>

Actually no, I want to create an instance of type in CoordinateSystemType and store it to CoordinateSystem.

---

<div class="post-metadata">

**Author:** ![SolarVelocity](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SolarVelocity](https://forums.unrealengine.com/u/SolarVelocity)\
**Post date:** [June 19, 2016, 4:06pm UTC](https://forums.unrealengine.com/t/instancing-interface-to-tscriptinterface-property/359013/4 "2016-06-19T16:06:42Z")

</div>

First up, UCoordinateSystem is a wrapper class for the ICoordinateSystem … I do not think you should be using it as a reference type (if you followed the [interface implementation for UE4](https://wiki.unrealengine.com/Interfaces_in_C%2B%2B) approach)

So you have a class or set of classes that implements ICoordinateSystem?

All of those classes extend UObject.

Your CoordinateSystemType class should restrict to the lowest base class that will include the set of all coordinate system types (e.g.)

```
TSubclassOf<UObject> CoordinateSystemType;

```

You can then create an instance of your coordinate system type

```
CoordinateSystemType = NewObject<UObject>(this, MyTypeThatImplements::StaticClass());

```

And then assign it to the CoordinateSystem interface pointer as detailed above.

You cant just create an instance of an interface you have to create an instance of a class that implements that interface then you can cast that object to your interface type.

---

<div class="post-metadata">

**Author:** ![GPSProlapse](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GPSProlapse](https://forums.unrealengine.com/u/GPSProlapse)\
**Post date:** [June 19, 2016, 6:31pm UTC](https://forums.unrealengine.com/t/instancing-interface-to-tscriptinterface-property/359013/5 "2016-06-19T18:31:31Z")

</div>

> You cant just create an instance of an interface you have to create an instance of a class that implements that interface then you can cast that object to your interface type.

That is obvious 😕 Your solution is same as simply making type property type UClass. With such approach you get none of possible benefits like limitations for editor UI. So you will be able to set its value to some class which doesn’t implement interface at all.  
Also TSubclassOf is type to store classes, not objects. So you can’t store UObject objects there, but you can store any type extending UObject so it’s same as simple UClass variable.  
Anyway I am not trying to instantiate interface, I am trying to instantiate a type, passed from editor, limited to types implementing interface.

---

<div class="post-metadata">

**Author:** ![SolarVelocity](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SolarVelocity](https://forums.unrealengine.com/u/SolarVelocity)\
**Post date:** [June 19, 2016, 8:12pm UTC](https://forums.unrealengine.com/t/instancing-interface-to-tscriptinterface-property/359013/6 "2016-06-19T20:12:03Z")

</div>

The problem you have is that I dont think there is an easy way to get a list of all types that implement an interface directly or inherit the interface from a base class. I do not think you can use the UCoordinateSystem wrapper type in this manner to filter your type list, but im no expert, and its not really clear what youre trying to do from your question.

The TSubclassOf member variable you have must use a type that is lowest common denominator of classes that implement ICoordinateSystem, I just gave UObject as an example, if there is no common base class then you will be stuck with UObject.

If you are able to obtain the type that is appropriate then you need to create an instance of it using NewObject and then do as I have indicated to populate the TScriptInterface.

You cant assign an object instance cast to your interface type straight to the TScriptInterface as it has an Interface component and an object component. You have to set both otherwise you get the UObject\* error you mention.

---

<div class="post-metadata">

**Author:** ![GwigreBox](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GwigreBox](https://forums.unrealengine.com/u/GwigreBox)\
**Post date:** [August 26, 2016, 1:01pm UTC](https://forums.unrealengine.com/t/instancing-interface-to-tscriptinterface-property/359013/7 "2016-08-26T13:01:49Z")

</div>

You can have only the class that implement your interface by adding something like this:

```
UPROPERTY(EditDefaultsOnly, meta = (MustImplement = "CoordinateSystem"))
TArray<UClass*> CoordinateSystemType;
```

---

<div class="post-metadata">

**Author:** ![GPSProlapse](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GPSProlapse](https://forums.unrealengine.com/u/GPSProlapse)\
**Post date:** [August 26, 2016, 6:36pm UTC](https://forums.unrealengine.com/t/instancing-interface-to-tscriptinterface-property/359013/8 "2016-08-26T18:36:50Z")

</div>

Satan bless you, good man, first sane comment! Anyway, looks like an answer. ^^

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [March 25, 2018, 8:19pm UTC](https://forums.unrealengine.com/t/instancing-interface-to-tscriptinterface-property/359013/9 "2018-03-25T20:19:18Z")

</div>

IAimable\* aimable = …  
TScriptInterface ayyyy;  
ayyyy.SetInterface(aimable);  
ayyyy.SetObject(reinterpret\_cast\<UObject\*\>(aimable)); //UInterface instances are always UObjects so this is safe  
dude-\>startAim(ayyyy); //you then free to use that TScriptInterface object as you please

---

<div class="post-metadata">

**Author:** ![Vicen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Vicen](https://forums.unrealengine.com/u/Vicen)\
**Post date:** [January 21, 2024, 5:13pm UTC](https://forums.unrealengine.com/t/instancing-interface-to-tscriptinterface-property/359013/10 "2024-01-21T17:13:15Z")

</div>

I have tested it and it works although the value of this variable/attribute is not kept in the instance, every time you reload the level it is reset to none.
