Calculation Issue (Could be int vs int32 issue)

Hello,

I’m having problems with my code. When my player pawn is further than about 46,000 away from a quadrant in my map I get -2147483648 as a result instead of the actual distance. It works fine when it’s closer than that. I researched it a lot and found that an issue could be that I was using int instead of int32. I corrected all the spots I could think of but the issue remains.

Here is my code that figures out the distance between the center of the four quadrants of my landscape. Any ideas why I’m getting -2147483648 instead of my actual distance when I get too far away?

Header File:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "MiningMaterialSpawn.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class HAVESTING_API UMiningMaterialSpawn : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UMiningMaterialSpawn();
	
private:
	void ShowCharacterLocation();
	int32 DetermineMapSize();
	int32 MapSize;
	void GenerateMaterialSpawn();
	void DisplayMaterialConcentration();
	int32 DistanceToSection(int32 SectionNumber);

	AActor* Character;

	UPROPERTY(EditAnywhere)
	AActor* ActorSelection;

	UPROPERTY(EditAnywhere)
	int32 NumberOfSections;

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

};

Here is my implementation:

// Fill out your copyright notice in the Description page of Project Settings.


#include "MiningMaterialSpawn.h"
#include "Engine/World.h"
#include "GameFramework/PlayerController.h"
#include "GameFramework/Actor.h"

// Sets default values for this component's properties
UMiningMaterialSpawn::UMiningMaterialSpawn()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}


// Called when the game starts
void UMiningMaterialSpawn::BeginPlay()
{
	Super::BeginPlay();
	MapSize = DetermineMapSize();
	//GenerateMaterialSpawn();
	
}


// Called every frame
void UMiningMaterialSpawn::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
	
	UE_LOG(LogTemp, Warning, TEXT("Distance to section 1 is: %d"), DistanceToSection(1));
	UE_LOG(LogTemp, Warning, TEXT("Distance to section 2 is: %d"), DistanceToSection(2));
	UE_LOG(LogTemp, Warning, TEXT("Distance to section 3 is: %d"), DistanceToSection(3));
	UE_LOG(LogTemp, Warning, TEXT("Distance to section 4 is: %d"), DistanceToSection(4));


}

int32 UMiningMaterialSpawn::DetermineMapSize()
{
	FVector ActorLocation = ActorSelection->GetActorLocation();
	MapSize = (-ActorLocation.X) * 2;
	//UE_LOG(LogTemp, Warning, TEXT("MapSize is: %f"), MapSize);
	return MapSize;
}

void UMiningMaterialSpawn::GenerateMaterialSpawn()
{
	int32 quad1 = FMath::RandRange(0,100);
	int32 quad2 = FMath::RandRange(0,100);
	int32 quad3 = FMath::RandRange(0,100);
	int32 quad4 = FMath::RandRange(0,100);

	//UE_LOG(LogTemp, Warning, TEXT("Concentration Map is: %i, %i, %i, %i"), quad1, quad2, quad3, quad4);
}

int UMiningMaterialSpawn::DistanceToSection(int32 SectionNumber)
{
	int32 SectionXLocation;
	int32 SectionYLocation;

	if (SectionNumber==1)
	{
		SectionXLocation = MapSize/4;
		SectionYLocation = -MapSize/4;
	}
	if (SectionNumber==2)
	{
		SectionXLocation = MapSize/4;
		SectionYLocation = MapSize/4;
	}
	if (SectionNumber==3)
	{
		SectionXLocation = -MapSize/4;
		SectionYLocation = -MapSize/4;
	}
	if (SectionNumber==4)
	{
		SectionXLocation = -MapSize/4;
		SectionYLocation = MapSize/4;
	}

	Character = GetWorld()->GetFirstPlayerController()->GetPawn();
	FVector CharacterLocation = Character->GetActorLocation();

	int32 PlayerX = CharacterLocation.X;
	int32 PlayerY = CharacterLocation.Y;

	int32 DistanceToSection = 
	FMath::Sqrt(((SectionXLocation-PlayerX)*(SectionXLocation-PlayerX))+((SectionYLocation-PlayerY)*(SectionYLocation-PlayerY)));

	return DistanceToSection;
}

Thanks so much, hope I can get this figured out. Edited because it wasn’t showing on forum.

20 hours and still in moderation? Is it broken?

Hi there! You can check this line of code

int32 DistanceToSection =
FMath::Sqrt(((SectionXLocation-PlayerX)(SectionXLocation-PlayerX))+((SectionYLocation-PlayerY)(SectionYLocation-PlayerY)));

Overflow can happen in it.