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
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:
(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.
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.
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.
you misunderstand me.
the question is what’s the best approach:
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:
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.