Method.method.method.method...? Im a bit confused

Morning,

I have this code fragment from the building escape project where we are adding a component to the door. I am a little confused as to how we are calling these methods. At first I was thinking that this is some sort of shorthand for calling methods in the same class, but they’re not in the same class. Am I right just on the short hand part though?

Brain hurts.

Great course.

:wink:

// Called when the game starts
void UPositionReport::BeginPlay()
{
Super::BeginPlay();

FString ObjectPos = GetOwner()->GetTransform().GetLocation().ToString();
FString ObjectName = GetOwner()->GetName();
UE_LOG(LogTemp, Warning, TEXT("PositionReport reporting for %s is at %s"), *ObjectName, *ObjectPos);

}

Whats happening is a thing called method chaining. Its a topic thats usually poorly explained, but I’ll have a go for you.

Essentially each step a->b.c.d.e takes an object, does something with it, and passes the result on to the next guy

So GetOwner() gets the owner, a pointer to AActor
GetTransform() asks AACtor for the transform object. Note. because *AActor is a pointer we use -> instead of . (*GetOwner).GetTransform() would work too, but its hella goofy looking
GetLocation() Asks the transform object for the location object
ToString() Asks the location object for a string representing it.

1 Like

Method chaining. Awesome.

Thanks very much for that, I was a bit stumped :wink:

Privacy & Terms