# \[Blueprint&C++\] Unreal Interface

**URL:** <https://forums.unrealengine.com/t/blueprint-c-unreal-interface/2239322>\
**Category:** Blueprint\
**Tags:** unreal-engine, CPP, Blueprint, question\
**Created:** [December 23, 2024, 10:11pm UTC](https://forums.unrealengine.com/t/blueprint-c-unreal-interface/2239322 "2024-12-23T22:11:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [December 23, 2024, 10:11pm UTC](https://forums.unrealengine.com/t/blueprint-c-unreal-interface/2239322/1 "2024-12-23T22:11:02Z")

</div>

Hello Everyone!!  
I have a character base class(MainCharacter). In Blueprints I have a character that inherits from this class and there I add InventoryComponent(inherits from InventoryComponentBase written in C++).  
In other components I use functions from InventoryComponentBase such as delete, search, add items.  
But I don’t want to make a function in MainCharacter to get the inventory component

```C++
UInventoryComponentBase* AMainCharacter::GetPlayerInventory() const
{
   //as we add this component in Blueprints and not in a C++ class.
   return FindComponentByClass<UInventoryComponentBase>();
}

```

Then constantly cast to AMainCharacter and call that function to get the inventory component.  
Instead, I was thinking of using an Unreal Interface for a simpler interaction between blueprint and C++.  
BUT!!! I don’t know how to make it better.  
Right now my interface looks like this:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/b/9/cb929146ed3cea76831db489d4ce31dea6fa6976.png)  
And in Blueprint:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/b/5/6b5bff7c31c0d8c722626206369730a17156cf26.png)  
and each function calls an inventory component function:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/c/3/ec315cee48ac0dbcdf4947f0c64af5a1b195bb28.png)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/e/e/9eebc2def1685059b5c7dd034b6113c0b7c1d8f1.png)  
etc.  
and I use it in C++ like this:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/8/b/f8b345e50abcedd4e741e9f33b675bdd517a23da.png)  
But then I thought I’d make one function to retrieve the inventory component:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/5/0/650de981b283164a176d5e7b660ab3789c3eab1f.png)  
and use it in C++ like this:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/8/5/d85cfae64e4c8b764d45cd7a629c55aa59c842d6.png)  
_(of course, take the component acquisition out of the loop)_  
And then if in the Blueprint version of the inventory component I add some function that is not in InventoryComponentBase, then I can make a function for it in the interface.

**What’s the best way to do it? Leave as I have or leave one function to get the component from Blueprint and call the same functions from there**

I really hope I’ve made myself clear, but if you need clarification, please do not hesi  
tate to contact me. 😀 😃 😄 😁 🙃

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [December 23, 2024, 10:23pm UTC](https://forums.unrealengine.com/t/blueprint-c-unreal-interface/2239322/2 "2024-12-23T22:23:44Z")

</div>

Do you _have_ to add the component in blueprint only?  
Typically, you’ll declare the base component reference in the C++ actor class, and set up the default for that component in actor construction.  
When you do that, you can set up a subclass of that component as the component instance in the editor in your blueprint actor subclass, and the right object will be instantiated.  
That way, you can just say `AMyCharacter->InventoryComponent->...` and the right thing happens.

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [December 23, 2024, 10:40pm UTC](https://forums.unrealengine.com/t/blueprint-c-unreal-interface/2239322/3 "2024-12-23T22:40:05Z")

</div>

This component is only in Blueprint + I don’t want to make any unnecessary connections

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [December 24, 2024, 12:04am UTC](https://forums.unrealengine.com/t/blueprint-c-unreal-interface/2239322/4 "2024-12-24T00:04:40Z")

</div>

You can have an inventory component derive from actor component, and make the default component be actor component at the C++ level. (It won’t let you easily call methods on that component of course.)

Because you don’t want to do dynamic discovery and casting all the time, it sounds like the connection is necessary, not unnecessary.

---

<div class="post-metadata">

**Author:** ![blAddddDe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@blAddddDe](https://forums.unrealengine.com/u/blAddddDe)\
**Post date:** [December 24, 2024, 3:46pm UTC](https://forums.unrealengine.com/t/blueprint-c-unreal-interface/2239322/5 "2024-12-24T15:46:48Z")

</div>

you misunderstand me.  
the question is what’s the best approach:

1. Leave it as I have it, i.e. for each function I use outside of InventoryComponentBase make a function in Interface and in the blueprint of the character override it like this:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/e/e/9eebc2def1685059b5c7dd034b6113c0b7c1d8f1.png)
2. Leave only this function in interface:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/5/0/650de981b283164a176d5e7b660ab3789c3eab1f.png)  
and use it like this:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/8/5/d85cfae64e4c8b764d45cd7a629c55aa59c842d6.png)  
and delete all other functions in the interface

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [December 25, 2024, 6:59pm UTC](https://forums.unrealengine.com/t/blueprint-c-unreal-interface/2239322/6 "2024-12-25T18:59:55Z")

</div>

What is your definition of “best?”  
Both methods will work, with slightly different ergonomics.  
Unless you somehow call these functions hundreds of times per frame, it’s unlikely to have a meaningful performance difference.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [January 24, 2025, 7:00pm UTC](https://forums.unrealengine.com/t/blueprint-c-unreal-interface/2239322/7 "2025-01-24T19:00:20Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
