Is it possible to use a linetrace, then checking if the hit object is of a class then using the instance object of that class. Sort of like a pickup system.
Yes, that is possible to do. If you’re curious on how to do it I recommend that you try looking at how casting works.
Also check out the isA function. If you can’t figure it out and someone hasn’t posted the answer for you then I will do it… but you should try to figure out how to do it first!
Quick TL;DR:
-Casting
-isA
If conditions.
:), good luck!
if (Trace(HitActor, this, StartofTrace, EndofTrace))
{
if (SelectedActor == NULL)
{
SelectedActor = HitActor.GetActor();
if (SelectedActor->IsA(APickUp::StaticClass()))
{
APickUp* FlashLightBat = Cast(HitActor.GetActor());
}
}
}
This is how i am doing it but it keeps crashing how do you properly use isA
if (GetWorld()->LineTraceSingle(HitResult, StartTrace, EndTrace, ECC_Visibility, TraceParams))
{
TraceParams.AddIgnoredActor(this);
if (HitResult.GetActor() != NULL)
{
if (HitResult.GetActor()->IsA(AAICharacter::StaticClass()))
{
AICharacter = Cast<AAICharacter>(HitResult.GetActor());
}
}
}
This is how I achieved it. Basically, the pickup should be declared in your header (Though not necessarily! It does make things easier in the long run though especially if you want to use / access it later!)
In this case I had it named as AICharacter. In the header file I am declaring it using the following:
class AAICharacter * AICharacter = NULL;
I’m also doing a check to make sure that the Hit Result doesn’t return as NULL – otherwise you’re trying to declare something that may not even be there which is likely why you’re getting a crash.
Your methodology is right but you need to make sure you’re performing checks (Especially with pointers) because for the most part they can be the death of a program if they’re not used correctly.
Thank you it worked i was doing it right but i split it in different functions so they were not communicating properly lol
I’m glad to hear that! If you could do me one last favor and be sure to mark this answer as answered that would be greatly appreciated!
This helps others who might be searching for something similar. :), thanks and have an awesome day!