Hello, basically I’m trying to run a function from a class different to the functions class. Here’s my code:
Header for SpawnGuy:
UPROPERTY(EditAnywhere)
class UFoodItem * refthingy;
SpawnGuy CPP:
void ASpawnGuy::UseItem()
{
for (TObjectIterator<UFoodItem> ctr; ctr; ++ctr)
{
if (ctr->IsA(UFoodItem::StaticClass()))
{
UFoodItem* actorFood = *ctr;
actorFood->UseItem(refthingy); //reference to the class below
}
}
}
Long story short, the code inside the if(Item) doesn’t run and I don’t really know why, I also tried making a reference to the UInventoryComponent class instead of UFoodItem but to no avail. If anyone can help me clear this up I’d appreciate it. Thank you!
I already tried using a type UInventoryComponent and it still didn’t work. Basically I’m trying to add the FoodItem type into the inventory. Hope that makes sense.
I got the concept of what you explained and it does make a little more sense, I do have another question. UFoodItem is a child of UItem, which is the base class and has stuff like weight, mesh and so on on it. Could I do the same thing you explained and have my UInventoryComponent take UItem instead of UFoodItem but still have a reference to UFoodItem in it so the inventory knows I want to add the UFoodItem not the base class UItem, I hope that makes sense and thank you once again!
yes @stareping, you can do like that. If my answer helped you could you please like the comments which are helped you with. I will also carry that solution as answer
First you should have UItem base class. FoodItem, ConsumableItem will be their child classes. UItem could include Use function and in FoodItem and ConsumableItem it could be overridable
Second you should have an UInventoryComponent. Inside of inventory component, you will have container TArray<class UItem*> Items which, you will add item inside of inventory component. And so AddItem function should be inside of your Inventory component.
Long story short, I’m trying to draw a design on your mind for you to understand how will you do that.
Hello! Thank you for the help, I managed to get the item to appear in my inventory! Now, as for the spawn part, how could I create a spawnable actor that is connected to my UObject of type FoodItem? So when I’m in the proximity of the SpawnableActor and interact with it the FoodItem appears in the inventory? Hope that makes sense, thank you!