I’m trying to create raycast pickup on items. So basicly here is my idea:
I create a class Item which holds few variables like Name, Price, Type, etc. Plus I have created SphereComponent for collision and pointer for the StaticMesh
Inside the editor I create a blueprint which extends from Item class (I set variables there and set the mesh)
Spawn it inside the level and hit play and try to pickup the item but it doesn’t work… here’s the code for the pickup function:
void ATTSCharacter::UseE()
{
FHitResult Hit(ForceInit);
FVector StartTrace, EndTrace;
FVector CameraLoc;
FRotator CameraRot;
GetActorEyesViewPoint(CameraLoc, CameraRot);
StartTrace = CameraLoc;
EndTrace = CameraLoc + (CameraRot.Vector() * 500.f) + FVector(0, 0, 25);
ATTSPlayerController* PC = Cast<ATTSPlayerController>(Controller);
FCollisionQueryParams LineParams(FName("Collision params"), true, PC->GetPawn());
UWorld* World = GetWorld();
if (World)
{
World->LineTraceSingle(Hit, StartTrace, EndTrace, COLLISION_TRACE, LineParams);
DrawDebugLine(World, StartTrace, EndTrace, FColor::Cyan, false, 5.0f);
if (Hit.Actor != NULL)
{
GEngine->AddOnScreenDebugMessage(9, 1.0f, FColor::Green, TEXT("Hit something"));
ATTSItem* HitMesh = Cast<ATTSItem>(Hit.GetActor());
if (HitMesh)
{
//Just to see if it works..
HitMesh->bHidden = true;
}
}
else
{
GEngine->AddOnScreenDebugMessage(9, 1.0f, FColor::Red, TEXT("Didn't hit anything"));
}
}
}
The trace hits stuff of course but casting doesn’t work properly… I don’t know what’s the problem guys, can’t figure it the whole day spent in searching the net…
Now the casting works perfectly fine but I do this:
//In header file:
UPROPERTY(EditAnywhere, Category = "Inventory")
TArray<ATTSItem*> Inventory;
//Inside a function
ATTSItem* HitMesh = Cast<ATTSItem>(Hit.GetActor());
if (HitMesh)
{
Inventory.Add(HitMesh);
HitMesh->Destroy();
}
It doesn’t seem like it’s getting added to the inventory…
I hit play, pick the item, it dissapears from the world but doesn’t get added to the inventory. I tried commenting HitMesh->Destroy(); but it still wasn’t getting added… I tried creating a new copy and add it to the inventory but still didn’t work. Any ideas?
How and when do you check whether it is added? Since it does disappear, apparently Destroy() is called. Try checking immediately after Inventory.Add whether its added, like did the array size change?
I do make a check like this:
Spawn in the game, press E, function executes, I get message Hit something, then I hit Eject button, click on my character, then Edit Blueprint, and when I scroll down to inventory section, it says 0 elements like this: