I’m really new to C++ (just learned pointers, haven’t really dealt with classes, objects and the like), and there’s something that keeps coming up that confuses me in Unreal. For e.g, I’m going through a tutorial here (https://docs.unrealengine.com/latest/INT/Programming/Tutorials/PlayerInput/3/index.html)…
In the line “float CurrentScale = OurVisibleComponent->GetComponentScale().X;”, what does".X" mean? I’ve also seen “.Y” used, and I can’t quite get my head around where these (variables?) came from. Are they accessing data relative to a particular axis in 3D space or something? It’d be most helpful if someone could just explain the full line of code I’ve quoted, but the full contextual code is shown below. Thanks!
void AMyPawn::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
// Handle growing and shrinking based on our "Grow" action
{
float CurrentScale = OurVisibleComponent->GetComponentScale().X;
if (bGrowing)
{
// Grow to double size over the course of one second
CurrentScale += DeltaTime;
}
else
{
// Shrink half as fast as we grow
CurrentScale -= (DeltaTime * 0.5f);
}
// Make sure we never drop below our starting size, or increase past double size.
CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
}
GetComponentScale() returns a vector consisting of three floats. If you want to access those floats individually, you can get to them via .X, .Y or .Z (for the first, second or third component respectively).
So, when appending .X, it means we’re just grabbing the single float describing the X-component of the vector (scale vector, in your example).
Hi Olliepm,
's answer is correct in this instance, but I just wanted to expand on it a little to hopefully help increase the understanding of anyone else who may be confused about the dot operator (.) in C++.
As mentioned, the dot operator allows you to access the members of an object. In the example you provided (that explained), you use the dot operator to access a specific member of the FVector that is returned by GetComponentScale(). X, Y, and Z are all variables that are part of the FVector, but you can also use the dot operator to access any of the functions included in FVector. For example, you could use GetComponentScale().GetMax() to retrieve the maximum value in the FVector.
To attempt to simplify it even more, imagine your game has an apple object with a float variable that contains it’s mass in grams. If your character has a variable that contains an instance of the apple object (not a pointer), and you wanted to access the mass of the apple, you would use the dot operator in a line something like this:
MyApple.MassInGrams;
Pointers have a different operator that does the same thing: the arrow operator (->). Using the apple example, if your character has a variable holding a pointer to an apple object you would use the arrow operator to access the mass of the apple located at the memory location the pointer is pointing to. It would look something like this:
MyApple->MassInGrams;
Great expansion on that topic, !
Thanks, that’s also helpful to know!