Three things I don't understand

Hello,
There are two things that I don’t understand from this lecture.

  1. When the new TankAimingComponent is created, it, by default, creates two public specifiers and one protected one. Is there any difference between the two public sections and why does Unreal create 2 of them?

  2. Other than the game crashing, what is the difference between the two below?

UTankAimingComponent TankAimingComponent;
TankAimingComponent.AimAt(HitLocation);
// vs.
TankAimingComponent->AimAt(HitLocation);
  1. Also, why not sure a reference for this? Is it because if it were a reference it could not be initialized to nullptr?
UTankAimingComponent* TankAimingComponent = nullptr;

Thanks!

  1. To be honest I don’t know why they changed the generated code to be like that. Though there is no difference. You can put lines 27 and 29 under the other public section and it would be the same.

    One reason of doing that however is organisation. For example you might want to keep everything related to animation grouped together.

  2. Well that wouldn’t compile. -> is just a shorthand to dereference before accessing via .

    TankAimingComponent->AimAt(HitLocation);
    //same as
    (*TankAimingComponent).AimAt(HitLocation);
    

    The reason the () or -> are needed is due to operator precedence. The . operator has a higher precedence than * so if you were to do this

    *TankAimingComponent.AimAt(HitLocation);
    

    You would be doing

    *(TankAimingComponent.AimAt(HitLocation));
    

    i.e. Use . on the pointer and attempt to call the function AimAt then dereference the result. Since pointers don’t have members (it’s just an address) that doesn’t make sense so that’s a compilation error.

  3. Because references have to be initialised to something and can’t be reassigned.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms