Running function from a different class

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
	     }
     }
 }

FoodItem CPP:

 void UFoodItem::UseItem(class UInventoryComponent* Item) 
 {
     if(Item)
     {
         Item->AddItem(this);
     }
 }

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!

You seem like confused. Please check

In function decleration, you are expecting UInventoryComponent but your refthingy is a type of UFoodItem.

And you are trying to add Item into FoodItem ?

Are you want to use or are you want to add inventory?

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.

Use means Use Item, that means eat, consume or something. Your design is wrong. How you should proceed is (Lets keep simple for now) :

UInventoryComponent should have function (maybe name AddItemToInventory(UFoodItem)) to gets Item and adds to inventory.

Use function can stay in FoodItem. What that function should include is ?

Lets imagine it will increase health and amount of mana :

void UFoodItem::UseItem(class AFPSCharacter* character)    
{
       if(character)
       {
           character->AddHealth(50);
           character->AddMana(20);
       }   
}

If this design not clear for you I could continue more to describe.

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 :slight_smile:

Use means Use Item, that means eat, consume or something. Your design is wrong. How you should proceed is (Lets keep simple for now) :

UInventoryComponent should have function (maybe name AddItemToInventory(UFoodItem)) to gets Item and adds to inventory.

Use function can stay in FoodItem. What that function should include is ?

Lets imagine it will increase health and amount of mana :

void UFoodItem::UseItem(class AFPSCharacter* character)    
 {
        if(character)
        {
            character->AddHealth(50);
            character->AddMana(20);
        }   
 }

If this design not clear for you I could continue more to describe.

Could you please explain how I could do that? Since the UFoodItem class would have to go through the UItem class as well? Thank you!

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!