I have a custom scene component which should always be restricted to locations on a grid. I would like to be able to drag the component in the Blueprint Editor viewport, and on release have it snap to the nearest grid location. I tried doing this with:
void UGridCell::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
Super::PostEditChangeProperty(PropertyChangedEvent);
// Snap to grid
FVector Loc = GetRelativeLocation();
FVector New = FVector(FMath::RoundToInt(Loc.X / GRID_SIZE) * GRID_SIZE + (GRID_SIZE * 0.5f), FMath::RoundToInt(Loc.Y / GRID_SIZE) * GRID_SIZE + (GRID_SIZE * 0.5f), 100.0f);
SetRelativeLocation(New);
MarkRenderStateDirty();
}
But it doesn’t work, I can drag the component to any position (GRID_SIZE is 400). If I manually enter a location, it does snap.
How would I go about auto-snapping (if it’s possible)?