Hello Everyone, I want to know what is ABS in C++ and how to use it in C++?
I want to write the same logic in C++.
This works for me but Correct me if I am wrong:
if (abs(-LocTopVlue) < abs(-LocBottomValue))
{
}
Hi Alexa.kl,
yes - abs() is good, it works with both integers and floating point numbers.
There is also fabs() which only works with floats - fabs() is slightly faster because of this.
Great information and in my case what would You recommend for me me I think later I will need Integers in my logic, and I am in trouble to set the
GetControlRotation().Yaw = LocTopVlue;
GetControlRotation(); is const
if You can help thank You.
bool MyCharacter::LimitPitchAngle(float Axis)
{
float LocBottomValue;
float LocTopVlue;
//Calculate the pitch limit values for the directions up / down in several poses
if (_bProne)
{
LocBottomValue = 290.0f;
LocTopVlue = 75.0f;
}
if (GetControlRotation().Yaw > LocBottomValue || GetControlRotation().Yaw < LocTopVlue)
{
if (abs(-LocTopVlue) < abs(-LocBottomValue))
{
GetControlRotation().Yaw = LocTopVlue;
}
}
}
Yes, there is also a “SetControlRotation”, so to clamp the rotation you can create a FRotator containing the values from GetControlRotation(), clamp those, then pass that in with SetControlRotation().
FRotator rot=GetControlRotation();
if(rot.Yaw>LocBottomValue) rot.Yaw=LocBottomValue;
if(rot.Yaw<LocTopVlue) rot.Yaw=LocTopVlue;
SetControlRotation(rot);
Sir Do I need a pointer to the PlayerController to call access SetControlRotation(); in character class?
I call it directly and it says UnDefined.
Yes, you’re right - use “GetController()->” to get the controller - it’s usually used with GetControlRotation() too - do you have your own routine called that?
I have a base controller class, I just created its reference and called that function using it.
MyControllerRef->SetControlRotation(rot);
Works like a Charm, Thank You very much, I really appreciate the help and the knowledge You are sharing with students.
This is the final output and working fine as expected, Sir Thank You very very much .
if (GetControlRotation().Yaw > LocBottomValue || GetControlRotation().Yaw < LocTopVlue)
{
if (abs(-LocTopValue) < abs(-LocBottomValue))
{
FRotator rot = GetControlRotation();
rot.Yaw = LocTopVlue;
MyControllerRef->SetControlRotation(rot);
}
else
{
FRotator rot = GetControlRotation();
rot.Yaw = LocBottomValue;
MyControllerRef->SetControlRotation(rot);
}
}
Sir this is the whole logic I want to implement in C++, I tried it in C++ but the engine is crashing when I click play the game in the editor.
The code compile successfully.
bool MyCharacter::LimitPitchAngle(float Axis)
{
float LocBottomValue ;
float LocTopValue ;
//Calculate the pitch limit values for the directions up / down in several poses
if (_bProne)
{
LocBottomValue = 350.0f;
LocTopValue = 15.0f;
}
else if (_bCrouching)
{
LocBottomValue = 290.0f;
LocTopValue = 15.0f;
}
else
{
LocBottomValue = 290.0f;
LocTopValue = 75.0f;
}
//If the limit is currently exceeded, set the boundary value
if (!(GetControlRotation().Pitch > LocBottomValue) || !(GetControlRotation().Pitch < LocTopValue))
{
if (abs(GetControlRotation().Pitch -LocTopValue) < abs(GetControlRotation().Pitch - LocBottomValue))
{
FRotator rot = GetControlRotation();
rot.Pitch = LocTopValue;
MyControllerRef->SetControlRotation(rot);
}
else
{
FRotator rot = GetControlRotation();
rot.Pitch = LocBottomValue;
MyControllerRef->SetControlRotation(rot);
}
}
if (((MyControllerRef->InputPitchScale = -2.5 * Axis + GetControlRotation().Pitch) > LocBottomValue)
|| ((MyControllerRef->InputPitchScale = -2.5 * Axis + GetControlRotation().Pitch) < LocTopValue))
{
return true;
}
else
{
return false;
}
}
void MyCharacter::MouseLookUp(float UAxisValue)
{
if (LimitPitchAngle(UAxisValue * 0.5))
{
AddControllerPitchInput(UAxisValue * 0.5);
}
}
My guess is that the “MyControllerRef” is not valid. The controller is a nullptr when the game starts, and can be a nullptr at other times too. It can also change.
At the top of LimitPitchAngle() is a good place to assign it, and to check it - something like this:
bool MyCharacter::LimitPitchAngle(float Axis)
{
MyControllerRef=GetController();
if(MyControllerRef==nullptr) return false;
...
Yes, after adding this now the editor is not crashing , and I can look only left and right.
Thank You very much Sir for Help , it really helps but still have the pointer null and I have no idea why it is nulled.
You’re welcome. Did you add the line “MyControllerRef=GetController();” before the test if it’s a nullptr? If you’re only assigning it in the constructor, it will be a nullptr there.
yes I tried it now and the compiler says: value of type AController * cannot be assigned to an entity of type APlayerController
I also tried to declare it as AController* MyControllerRef
class “AController” has no member "InputPitchScale"
My mistake, try assigned it to “UGameplayStatics::GetPlayerController(GetWorld(),0);”
APlayerController * MyControllerRef = UGameplayStatics::GetPlayerController(GetWorld(), 0);
The pointer is still null, I think something is not correct with my code or class.
My bad, I was not doing it Correctly.
Now fixed.
.h
APlayerController * MyControllerRef;
.cpp
void MyCharacter::BeginPlay()
{
Super::BeginPlay();
MyControllerRef = UGameplayStatics::GetPlayerController(GetWorld(), 0);
}
Thank You very much for Help Sie, I really appreciate.