Make to give me item.

Hello. I have been following UE4 C++ book with golden egg project. I have simple function, which fires when i overlap .

void ANPC::Prox_Implementation( AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult )
{
    // if the overlapped actor is not the player,
    // you should just simply return from the function
    if( Cast<AU_Avatar>( OtherActor ) == nullptr )
    {
        return;
    }
    UWorld* world = GetWorld();

    APlayerController* PController = world -> GetFirstPlayerController();
    if( PController )
    {
        AMyHUD * hud = Cast<AMyHUD>( PController->GetHUD() );
        hud->addMessage( Message( Face, Name + ": " + NpcMessage, 5.f, FColor::White, FColor::Black ) );
        

    }
}

However I dont know how do i make this to give me something. I have another class called APickupItem which is responsible for any item which can be given to somebody. What are the best approaches of achieving of giving something to Main actor from ?

My thought was - add on same spot invisible mesh with overlapping function. But that wont work if i have to give 2…more items in separate time.

You can do it directly via code if you’d like.

First you’ll need to create a function on the player to receive the item:

YourCharacter.h

void ReceivePickupItem(APickupItem &pickupItem);

YourCharacter.cpp

void YourCharacter::ReceivePickupItem(APickupItem &pickupItem)
{
	// You now have the item
	pickupItem->SomeFunction();
	
	etc...
}

Now, you just need to spawn the item and give it to the player:

void ANPC::Prox_Implementation( AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult )
 {
     // if the overlapped actor is not the player,
     // you should just simply return from the function
     if( Cast<AU_Avatar>( OtherActor ) == nullptr )
     {
         return;
     }
     UWorld* world = GetWorld();
 
     APlayerController* PController = world -> GetFirstPlayerController();
     if( PController )
     {
         AMyHUD * hud = Cast<AMyHUD>( PController->GetHUD() );
         hud->addMessage( Message( Face, Name + ": " + NpcMessage, 5.f, FColor::White, FColor::Black ) );
         
		 // Get reference to Character (very verbose)
		 APawn* pawn = PController->GetPawn();
		 
		 if (pawn != nullptr)
		 {
			AYourCharacter* character = Cast<AYourCharacter>(pawn);
			
			if(character != nullptr)
			{
				// Spawn pickup item
				APickupItem* pickupItem = GetWorld()->SpawnActor<APickupItem>(APickupItem::StaticClass());
				
				// Give to player
				character->ReceivePickupItem(pickupItem);
				
			}
		 }
 
     }
 }

Naturally, there are many ways to do this differently, like passing around a class reference instead of an instance for example, but this should give you the basic idea.

Thanks, i will try this approach. But in code - i see you spawn just a class. But i have some BP derived on APickutItem class, is it possible to spawn blueprinted item instead of parent class?

If this has certain items that it should be giving the player, you could setup a variable in the 's class like so:

TSubclassOf<APickupItem> ItemToGive;

Once this is set up, that variable can be set to anything that derives from APickupItem, including any Blueprints made of that class. You can set these by default or at runtime through Blueprints.