Underflow appenning on array without consistency

Hi everyone !

I have some trouble with a function that seem simple to me but appended to work without consistency.
The goal of the function is to provide an array of point from a grid system. I have other function that get me static position and I use them in order to calculate the array.

But when I check the return on blueprint, the result is underflow or overflow.

The parameters RoomPosition, RoomSize are correct.

I have some skills on C++ from school but it has been a long times from there and UE is not a classic C++ so I don’t exclude the fact that I made a simple mistake.

Here is my code:

#include "CellUtils.h"

void UCellUtils::GetNearbyRoom(const FVector RoomPosition, FVector &North, FVector &East, FVector &South, FVector &West, bool Relative = false)
{
	FVector Pos;

	if (!Relative) Pos = RoomPosition;
	
	East = Pos + FVector(0.0f, 1.0f, 0.0f);
	West = Pos + FVector(0.0f, -1.0f, 0.0f);
	South = Pos + FVector(-1.0f, 0.0f, 0.0f);
	North = Pos + FVector(1.0f, 0.0f, 0.0f);
}

TArray<FVector> UCellUtils::GetArrayNearbyRoom(const FVector RoomPosition, bool Relative = false)
{
	FVector North, South, East, West;
	GetNearbyRoom(RoomPosition, North, East, South, West, Relative);
	TArray<FVector> Ret;

	Ret.Push(North);
	Ret.Push(South);
	Ret.Push(East);
	Ret.Push(West);
	return Ret;
}

void UCellUtils::GetDiagonalRoom(const FVector RoomPosition, FVector& NorthEast, FVector& EastSouth, FVector& SouthWest,
                                 FVector& WestNorth, bool Relative = false)
{
	FVector Pos;

	if (!Relative) Pos = RoomPosition;
	
	NorthEast = Pos + FVector(1.0f, 1.0f, 0.0f);
	EastSouth = Pos + FVector(-1.0f, 1.0f, 0.0f);
	SouthWest = Pos + FVector(-1.0f, -1.0f, 0.0f);
	WestNorth = Pos + FVector(1.0f, -1.0f, 0.0f);
}

TArray<FVector> UCellUtils::GetArrayDiagonalRoom(const FVector RoomPosition, bool Relative = false)
{
	FVector NorthEast, EastSouth, SouthWest, WestNorth;
	GetDiagonalRoom(RoomPosition, NorthEast, EastSouth, SouthWest, WestNorth, Relative);
	TArray<FVector> Ret;

	Ret.Push(NorthEast);
	Ret.Push(EastSouth);
	Ret.Push(SouthWest);
	Ret.Push(WestNorth);
	return Ret;
}

TArray<FVector> UCellUtils::GetPointFromCellPosition(const FVector RoomPosition, const FIntVector RoomSize, bool ReturnMiddle, bool ReturnCorner)
{
	TArray<FVector>		Points;
	TArray<FVector>		Directions;
	FVector				RoomPositionReal = RoomPosition;

	RoomPositionReal.X *= RoomSize.X;
	RoomPositionReal.Y *= RoomSize.Y;
	
	if (!ReturnMiddle && !ReturnCorner)
	{
		Points.Push(RoomPositionReal);
		return Points;
	}
	
	if (ReturnMiddle)
	{
		Directions += GetArrayNearbyRoom(RoomPosition, true);
	}
	if (ReturnCorner)
	{
		Directions += GetArrayDiagonalRoom(RoomPosition, true);
		Directions.Push(RoomPosition);
	}

	for (const FVector Direction : Directions)
	{
		FVector CurrentPoint = RoomPositionReal;
		CurrentPoint.X += Direction.X * static_cast<float>(RoomSize.X) / 2.0f;
		CurrentPoint.Y += Direction.Y * static_cast<float>(RoomSize.Y) / 2.0f;
		Points.Push(CurrentPoint);
	}
	
	return Points;
}

If you have any questions don’t hesitate. Thanks in advance for your help !

The default constructor for FVector does NOT initialize the x,y,z members if I remember correctly. Make sure you properly initialize the Pos variable in GetNearbyRoom and GetDiagonalRoom even when Relative is true. Otherwise you could get some random value that just happend to be in memory. And since the floating point representation of the value is so large adding or subtracing one from -3483178070003679232 won’t have any effect.
If that doesn’t help just debug the function to see where the values go wrong.

1 Like

I feel dumb… That worked thank you !