Sorry for the late response;
This has worked to delete the lnk2019 error, but now i’m faced with a new error ( i coded something wrogn, but, since we’re here, i will ask you for your attention one last time)
The error is this:
C:\Users\giugi\OneDrive\Documents\Unreal Projects\attrition\Source\attrition\Public\TFSpatialHash.h(76): error C2440: ‘=’: unable to convert from ‘const T *’ to ‘T’
with
[
T=AActor *
]
C:\Users\giugi\OneDrive\Documents\Unreal Projects\attrition\Source\attrition\Public\TFSpatialHash.h(76): note: Types shown are not related. Conversion requires reinterpret_cast, C-type cast, or function-type cast in parenthesis
C:\Users\giugi\OneDrive\Documents\Unreal Projects\attrition\Source\attrition\Public\TFSpatialHash.h(68): note: while compiling the member function ‘T
Utils::TFSpatialHash<T,int,TMap<FVector2D,T,FDefaultSetAllocator,TDefaultMapHashableKeyFuncs<InKeyType,InValueType,false>>>::SearchNearest(const FVector2D &) const’ of model class
with
[
T=AActor *,
InKeyType=FVector2D,
InValueType=AActor *
]
C:\Users\giugi\OneDrive\Documents\Unreal Projects\attrition\Source\attrition\Private\HealthComponent.cpp(103): note: see instance reference ‘T
Utils::TFSpatialHash<T,int,TMap<FVector2D,T,FDefaultSetAllocator,TDefaultMapHashableKeyFuncs<InKeyType,InValueType,false>>>::SearchNearest(const FVector2D &) const’ of the template function being compiled
with
[
T=AActor *,
InKeyType=FVector2D,
InValueType=AActor *
]
C:\Users\giugi\OneDrive\Documents\Unreal Projects\attrition\Source\attrition\Public\HealthComponent.h(66): Notes: See the instance reference ‘Utils::TFSpatialHash<AActor *,int,TMap<FVector2D,T,FDefaultSetAllocator,TDefaultMapHashableKeyFuncs<InKeyType,InValueType,false>>>’
of model class that is being compiled
with
[
T=AActor *,
InKeyType=FVector2D,
InValueType=AActor *
]
Translation done by me at midnight so errors may appear
This is the TFSpatialHash.h file (The one that is having compiler errors)
// Copyright LinearGames. All rights reserved.
#pragma once
#include <CoreMinimal.h>
#include <Math/UnrealMathUtility.h>
#include <Math/Vector2D.h>
#include <Containers/Map.h>
#include <Containers/Array.h>
#include <GameFramework/Actor.h>
namespace Utils
{
template<typename T, typename CellSizeType = int, typename Hash = TMap<FVector2D, T>>
class ATTRITION_API TFSpatialHash final
{
public:
//You have to call this function
void SetCellSize(CellSizeType CellSize)
{
CellSize_ = CellSize;
};
void Insert(const FVector2D& Point, const T Object)
{
const FVector2D Key = HashPoint(Point);
HashTable_.Add(Key, Object);
}
//Works only if Object is or is derived from AActor class
void Insert(const T Actor)
{
const FVector2D Point(Actor->GetActorLocation().X, Actor->GetActorLocation().Y);
const FVector2D Key = HashPoint(Point);
HashTable_.Add(Key, Actor);
}
void Remove(const FVector2D& Point)
{
FVector2D Key = HashPoint(Point);
HashTable_.Remove(Key);
}
TArray<T> Search(const FVector2D& Point, const double Range) const
{
TArray<T> Results;
const FVector2D MinKey = HashPoint({Point.X - Range, Point.Y - Range});
const FVector2D MaxKey = HashPoint({Point.X + Range, Point.Y + Range});
for(double I = MinKey.X; I <= MaxKey.X; ++I)
{
for(double J = MinKey.Y; J <= MaxKey.Y; ++J)
{
FVector2D Key = FVector2D(I, J);
const T* Object = HashTable_.Find(Key);
if(Object && FVector2D::DistSquared(Point, Key) <= Range * Range)
{
Results.Add(*Object);
}
}
}
return Results;
}
T SearchNearest(const FVector2D& Point) const
{
T NearestObject = nullptr;
double NearestDistance = DBL_MAX;
for(auto& [Key, Object] : HashTable_)
{
if(const double Distance = FVector2D::DistSquared(Point, Key); Distance < NearestDistance)
{
NearestObject = Cast<AActor*>(&Object);
NearestDistance = Distance;
}
}
return NearestObject;
}
void Update(const FVector2D& OldPoint, const FVector2D& NewPoint, const T Object) const
{
if(OldPoint == NewPoint) return;
if(HashTable_.Find(OldPoint) == &Object)
{
Remove(OldPoint);
Insert(NewPoint);
}
}
void Clear()
{
HashTable_.Reset();
}
private:
CellSizeType CellSize_;
Hash HashTable_;
static FVector2D HashPoint(const FVector2D& Point)
{
const double X = FMath::FloorToInt(Point.X / CellSize_);
const double Y = FMath::FloorToInt(Point.Y / CellSize_);
return FVector2D(X, Y);
}
virtual void BeginPlay() {};
};
}
Many thanks for your help again, i’m new in the coding community