So I’m currently trying to set up a custom snapping system in the editor so that I can move hexagon tiles around and have them perfectly to a grid.
I’m currently having a lot of trouble doing this however, and I can only get it to sorta kinda work. I’m using the OnConstruction function to attempt to snap the actors to the grid, but the Transform that is passed into OnConstruction is the actors location, so if I then modify that location all further changes become based on the amount I’ve moved the transform gizmo is the duration between frames. Ideally I would be able to access the world location of the transform gizmo and use that as my source of snapping, but I don’t know if its possible to access that information.
My current attempt (that isn’t working very well):
.h (variables being used)
UPROPERTY(VisibleAnywhere, Category = GridInformation)
FVector BuiltLocation;
UPROPERTY(VisibleAnywhere, Category = GridInformation)
FVector CurrentSnapLocation;
.cpp (contained with the OnConstruction function)
if ((Transform.GetLocation().X - CurrentSnapLocation.X) > 10
|| (Transform.GetLocation().Y - CurrentSnapLocation.Y) > 10
|| (Transform.GetLocation().Z - CurrentSnapLocation.Z) > 10)
{
}
else
{
BuiltLocation = FVector((BuiltLocation.X + (Transform.GetLocation().X - CurrentSnapLocation.X)),
(BuiltLocation.Y + (Transform.GetLocation().Y - CurrentSnapLocation.Y)),
(BuiltLocation.Z + (Transform.GetLocation().Z - CurrentSnapLocation.Z)));
}
UE_LOG(LogTemp, Log, TEXT("Built Location: %s"), *BuiltLocation.ToString());
UE_LOG(LogTemp, Log, TEXT("this->GetActorLocation(): %s"), *this->GetActorLocation().ToString())
int32 GridLocX, GridLocY, GridLocZ;
float LocX, LocY, LocZ;
GridLocY = (int32)(((BuiltLocation.Y / YMovement) + 0.5f));
LocY = GridLocY * YMovement;
GridLocZ = (int32)(((BuiltLocation.Z / 25.0f) + 0.5f));
LocZ = (GridLocZ >= 0) ? GridLocZ * 25.0f : 0.0f;
GridLocX = (int32)(((BuiltLocation.X / XMovement) + 0.5f));
LocX = (GridLocY % 2 == 0) ? GridLocX * XMovement : (GridLocX * XMovement) + (XMovement * 0.5f);
this->SetActorLocation(FVector(LocX, LocY, LocZ));
if ((Transform.GetLocation().X - CurrentSnapLocation.X) > 10
|| (Transform.GetLocation().Y - CurrentSnapLocation.Y) > 10
|| (Transform.GetLocation().Z - CurrentSnapLocation.Z) > 10)
{
CurrentSnapLocation = this->GetActorLocation();
BuiltLocation = this->GetActorLocation();
}
My thought process was to grab the movement of the transform and store it in the BuiltLocation, from that I thought I could replicate the movement of the transform gizmo, unfortunately when the snap update occurs everything gets through off and the system breaks down horribly.
I feel like if I could separate the transform and the movement of everything else, this method would work fine, unfortunately depending on the transform of the actor and also changing the transform of the actor isn’t a very good mix when doing this. Does anyone know how I could separate the components from the actor transform / location?