Pure Function with return type FRotator in C++ is correct?

I a trying to translate another pure bp function to C++, not sure if I am doing it correctly.
Please guide me if doing anything wrong.

BP:

C++:

FRotator AMyCharacter::GetControllerRotation()
{
	FRotator Rotation;
	FRotator ReturnRotation;
	if (_bAltPressed)
	{
		ReturnRotation = AltPressedRotation;
	}
	else
	{
		ReturnRotation = GetControlRotation();
	}
	Rotation = ReturnRotation;
	return Rotation;
}
2 Likes

You don’t need FRotator Rotation if its only purpose is to be returned. Plus, you can use as many returns as you want, provided that all paths have return at the end.

FRotator AMyCharacter::GetControllerRotation()
{
	if (_bAltPressed)
	{
		return AltPressedRotation;
	}
	return  GetControlRotation();
}
3 Likes

I will be setting it later like
image_2022-05-03_221907392

1 Like

Thank You for solving my problem and helping in my study progress … really appreciated

1 Like

BTW, if you get used to using ternary operators, it’ll make your coding life easier and code shorter without a lot of curly brackets and ifs/elses:

FRotator AMyCharacter::GetControllerRotation()
{
	return _bAltPressed ? AltPressedRotation : GetControlRotation();
}
2 Likes

never used them , thank You for sharing this with me, this is a great example for me to start with… very understandable to know the exact purpose of this operator… I was always confused watching this operator … now I have learned for what I can use these ? and : .

2 Likes