How can I know if a FVector variable is set or not.

Hi!

I’m using Unreal 5.2.1. with C++ to develop a Tetris like game.

I have this method:


FVector UTBlocksSpawnerSubsystem::GetFallingLocation() const
{
	FVector StartLocation;

	if (UWorld* World = GetWorld())
    {
	    TArray<AActor*> ActorsToFind;
	    UGameplayStatics::GetAllActorsOfClass(GetWorld(), ATStartPointActor::StaticClass(), ActorsToFind);

        if (ActorsToFind.Num() == 1)
        {
	        const ATStartPointActor* StartPointActor = Cast<ATStartPointActor>(ActorsToFind[0]);

            if (StartPointActor != nullptr)
            {
                StartLocation = StartPointActor->GetActorLocation();
            }
        }
    }

	return StartLocation;
}

And now here it comes my problems with C++. How can I know if I get a valid location?

Maybe I can use a FVector* to check if it is nullptr. Or maybe I can implement the method in another way. But I don’t know.

Thanks.

Yeah afaik there is no such thing as an invalid vector, or an invalid struct in general, unless that struct implements an explicit validity boolean.

You could use ZeroVector as an “invalid” vector, although there’s a very very slim chance of false positive obviously.

A “proper” way to do it would be to return a boolean for success or failure, and return the vector by reference :

bool GetFallingLocation(FVector& OutVector) const
{
    //...
    if (StartPointActor)
    {
        OutVector = StartPointActor->GetActorLocation();
        return true;
    }

    return false;
}

Usage:

FVector Loc;
if (GetFallingLocation(Loc))
{
    //...
}
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.