Download

using a UENUM to safely manage and check an object's movement direction

Imagine I had an object that could go left, right or down. I was thinking UENUM would be ideal to hold these values, and by calculating it’s actual movement using a vector (taking in location from previous frame then adding it to the location from this current frame). Then I could just look at the vector.y and determine if it was moving left, right or down then use the enum to hold that value, by passing the enum instance into the function and assigning it the specific direction.

So this would be in the .h above the class declaration:



UENUM()
enum class EnumMoveDirection: uint8
{
	DIR_Left		UMETA( DisplayName = 'Moving Left' ),
	DIR_Right 		UMETA( DisplayName = 'Moving Right' ) ,
	DIR_Down		UMETA( DisplayName = 'Moving Down' ) 
};


The function signature in the .h:



UFUNCTION()
		void CalcDirectionFromTickVector( const FVector MoveVectorThisTick , EnumMoveDirection& EMoveDirection );


I created instances of the enum in .cpp, and here’s the function call:



EnumMoveDirection EMoveDirection;
EMoveDirection = EnumMoveDirection::DIR_Down;

CalcTaxiDirectionFromTickVector( MoveVectorThisTick , ETaxiMoveDirection );


this is the function definition I was trying to use for this purpose in the .cpp:



void CalcDirectionFromTickVector( const FVector MoveVectorThisTick , EnumMoveDirection EMoveDirection )
{

	if (MoveVectorThisTick.Y > 0)
	{
		EMoveDirection = EnumMoveDirection::DIR_Right;
	}
	else if (MoveVectorThisTick.Y < 0)
	{
		EMoveDirection = EnumMoveDirection::DIR_Left;
	}
	else 
	{
		EMoveDirection = EnumMoveDirection::DIR_Down;
	}
}


And as errors I get this for each line where I try to assign to EMoveDirection:
Error 1 error C2664: ‘void AtwinStickPawn::CalcDirectionFromTickVector(const FVector,EnumMoveDirection &)’ : cannot convert argument 2 from ‘TEnumAsByte<EnumMoveDirection>’ to ‘EnumMoveDirection &’
I read up the UENUM documentation in the API and I really don’t have any idea what TEnumAsByte is, is that how the enum holds it’s value as a uint8? How do I use this instead if this is more proper?

Thanks!

I’m not certain which is the cause, but:

Your enum name is not really following convention. The enum type name should have the ‘E’ prefix, so:


enum class EMoveDirection: uint8

Then your variable name would become just ‘MoveDirection’.

You have EnumMoveDirection& in your declaration, and EnumMoveDirection (without the &) in your definition.

You’d be better off just returning the enum from your function rather than passing it in by reference.

Failing that, since it’s a UFUNCTION you may need to wrap your parameter/return type as TEnumAsByte< EMoveDirection >, but I wouldn’t have thought that was necessary.

I honestly have struggled to understand how to use UENUM objects because the documentation is really sparse. This is the doc I’m referring to:

This is what it says:



// Old property
    UPROPERTY()
    TEnumAsByte<EThing::Type> MyProperty;

    // New property
    UPROPERTY()
    EThing MyProperty;


I actually don’t even understand what it means to wrap parameter/return type. All I know is that TEnumAsByte is a template that returns the byte corresponding to the Enum Value right?

All I meant by wrapping was to try replacing EThing with TEnumAsByte< EThing >, but as your link suggests this should no longer be needed. Although I have come across cases where a UPROPERTY is treated differently from a UFUNCTION parameter.

Did you try renaming your type as I proposed and then altering the function signature to be:



UFUNCTION()  
EMoveDirection CalcDirectionFromTickVector( const FVector MoveVectorThisTick) 


I just did that yeah. And thanks for your time btw.

Here’s what I rewrote the function definition to:



EMoveDirection AtwinStickPawn::CalcDirectionFromTickVector( const FVector MoveVectorThisTick )
{
	
	if (MoveVectorThisTick.Y > 0)
	{
		return( EMoveDirection::DIR_Right);
	}
}


And there’s the function call in the tick:



MoveDirection = CalcDirectionFromTickVector( MoveVectorThisTick );


Followed by this error:
Error 1 error C4715: ‘AtwinStickPawn::CalcDirectionFromTickVector’ : not all control paths return a value h:\users\dirtrobot\my documents\unreal projects winstick\source winstick winstickpawn.cpp 375 1 twinStick

I don’t even know what that means…?

You’ve specified that your function returns a value of type EMoveDirction, but when it gets to the end of the function (ie, the if is false) - it doesn’t return anything. You need to return something there :slight_smile:

Ah nm, I googled it, so I mod’d the definition to this now:



EMoveDirection AtwinStickPawn::CalcDirectionFromTickVector( const FVector MoveVectorThisTick )
{
	
	if (MoveVectorThisTick.Y > 0)
	{
		return( EMoveDirection::_DIR_Right);
	}
	else return( EMoveDirection::_DIR_Down );
}


and now it seems to compile!

Thanks again!

Haha yeah, you were one step ahead of my googling, thanks :wink: