I have created the following method:
AActor* AFPSCharacter::LookingAtCollectable()
{
FHitResult HitRes;
FRotator Rotation;
FVector Location;
GetController()->GetPlayerViewPoint(Location, Rotation);
FVector Start = Location;
FVector End = Start + Rotation.Vector() * ReachDistance;
bool HasHit = ()->LineTraceSingleByChannel(HitRes, Start, End, ECC_Visibility, FCollisionQueryParams());
if (HasHit)
{
AActor* actor = Cast<AActor>(HitRes.GetActor());
if (actor->IsInA(ACollectable::StaticClass()))
{
return actor;
}
}
return nullptr;
}
…Which is intended to return either nullptr if the user is not looking at a ‘Collectable’ or return a pointer to the actor otherwise. This method worked when the code was within the input handler (return actor;
was replaced with actor->Destroy()
). However, it is now raising the C2440 error as follows:
2>Building 6 actions with 8 processes...
2> [1/6] FPSCharacter.cpp
2>C:\Program Files\Epic Games\UE_4.24\Engine\Source\Runtime\Core\Public\UObject/WeakObjectPtrTemplates.h(110): error C2440: 'initializing': cannot convert from 'U *' to 'T *'
2> with
2> [
2> U=AActor
2> ]
2> and
2> [
2> T=AActor *
2> ]
2> C:\Program Files\Epic Games\UE_4.24\Engine\Source\Runtime\Core\Public\UObject/WeakObjectPtrTemplates.h(110): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2> C:\Users\egrsi\Documents\Unreal Projects\FirstPerson\Source\FirstPerson\FPSCharacter.cpp(76): note: see reference to function template instantiation 'TWeakObjectPtr &TWeakObjectPtr::operator =(U *)' being compiled
2> with
2> [
2> U=AActor
2> ]
2> C:\Users\egrsi\Documents\Unreal Projects\FirstPerson\Source\FirstPerson\FPSCharacter.cpp(76): note: see reference to function template instantiation 'TWeakObjectPtr &TWeakObjectPtr::operator =(U *)' being compiled
2> with
2> [
2> U=AActor
2> ]
2>C:\Program Files\Epic Games\UE_4.24\Engine\Source\Runtime\Core\Public\UObject/WeakObjectPtrTemplates.h(111): error C2664: 'FWeakObjectPtr &FWeakObjectPtr::operator =(const FWeakObjectPtr &)': cannot convert argument 1 from 'T *' to 'const UObject *'
2> with
2> [
2> T=AActor *
2> ]
2> C:\Program Files\Epic Games\UE_4.24\Engine\Source\Runtime\Core\Public\UObject/WeakObjectPtrTemplates.h(111): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2> C:\Program Files\Epic Games\UE_4.24\Engine\Source\Runtime\CoreUObject\Public\UObject/WeakObjectPtr.h(57): note: see declaration of 'FWeakObjectPtr::operator ='
According to documentation, the types of these functions match up fine and the linter doesn’t have issues with any of it either. I’m fairly new to Unreal Engine, would be great if someone could help. Thanks