Skip to content

Commit

Permalink
S04-139 successfully refactor the aimingAt method
Browse files Browse the repository at this point in the history
  • Loading branch information
randomwangran committed Dec 6, 2018
1 parent 0fd1bd9 commit b3c8415
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 27 deletions.
32 changes: 16 additions & 16 deletions Unreal/Section04/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org8e07690">1. L139 Creat default sub objects in C++</a>
<li><a href="#orgdebd7c6">1. L139 Creat default sub objects in C++</a>
<ul>
<li><a href="#org8f24d46">1.1. goal</a></li>
<li><a href="#org83d1946">1.2. result</a></li>
<li><a href="#org3097914">1.3. notes in lecture</a></li>
<li><a href="#orgfb6f80a">1.4. question that I do not understand</a>
<li><a href="#orgdab1df7">1.1. goal</a></li>
<li><a href="#org2162189">1.2. result</a></li>
<li><a href="#orge7b7feb">1.3. notes in lecture</a></li>
<li><a href="#org509c028">1.4. question that I do not understand</a>
<ul>
<li><a href="#org65412d1">1.4.1. crash</a></li>
<li><a href="#orgbcfaaf2">1.4.1. crash</a></li>
</ul>
</li>
<li><a href="#org496c3e6">1.5. a-ha moment</a>
<li><a href="#orgf0531f1">1.5. a-ha moment</a>
<ul>
<li><a href="#orged3db71">1.5.1. pointer to pointer</a></li>
<li><a href="#orgb8a609c">1.5.1. pointer to pointer</a></li>
</ul>
</li>
</ul>
Expand All @@ -23,49 +23,49 @@
</div>
</div>

<a id="org8e07690"></a>
<a id="orgdebd7c6"></a>

# L139 Creat default sub objects in C++


<a id="org8f24d46"></a>
<a id="orgdab1df7"></a>

## goal

Refactoring the code so that the aiming log is a separated class.


<a id="org83d1946"></a>
<a id="org2162189"></a>

## result

![img](Source/screenCapture/tankBodyOutPutCorrectAimingLocation.png)


<a id="org3097914"></a>
<a id="orge7b7feb"></a>

## notes in lecture


<a id="orgfb6f80a"></a>
<a id="org509c028"></a>

## question that I do not understand


<a id="org65412d1"></a>
<a id="orgbcfaaf2"></a>

### crash

When I try to get the owner of the 'aiming component', the UE
crush without any info.


<a id="org496c3e6"></a>
<a id="orgf0531f1"></a>

## a-ha moment


<a id="orged3db71"></a>
<a id="orgb8a609c"></a>

### pointer to pointer

Expand Down
13 changes: 8 additions & 5 deletions Unreal/Section04/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@
useful when you want to separate a function in a class.

I've stuck in this issue for a long time. At first, I think I
should write another getter to return a 'UTankAimingComponent' so
that the tank can use AimAt method defined in
should write another getter to return a 'UTankAimingComponent'
pointer so that the tank can use ~AimAt~ method defined in
'UTankAimingComponent' class.

#+BEGIN_SRC
#+BEGIN_SRC c++
UTankAimingComponent* ATankAIController::GetAimingComponent() const
{
return Cast<UTankAimingComponent>(GetPawn());
return Cast<UTankAimingComponent>(GetPawn());
}
#+END_SRC


However, when I call ~GetAimingComponent~ From class
~TankAIController~, the code compiled, which is the result of
commit ~4903f7304a0c02b15983c36c88ffa025a503753a~. But as soon as
I click ~Play~ button, UE4 crash without any information.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ void ATankAIController::Tick(float DeltaTime)
// TODO Move towards the player

// Aim towards the player
GetAimingComponent()->AimAt(GetPlayerTank()->GetActorLocation());

//GetControlledTank()->TankAimingComponent->Test();

GetControlledTank()->TankAimingComponent->AimAt(GetPlayerTank()->GetActorLocation());

// Fire if readyk
}
Expand Down
14 changes: 12 additions & 2 deletions Unreal/Section04/Source/BattleTank/Private/TankAimingComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,17 @@ void UTankAimingComponent::TickComponent(float DeltaTime, ELevelTick TickType, F
void UTankAimingComponent::AimAt(FVector HitLocation)
{
auto OurTankName = GetOwner()->GetName();
//UE_LOG(LogTemp, Warning, TEXT("%s aiming at %s"), *OurTankName, *HitLocation.ToString());
UE_LOG(LogTemp, Warning, TEXT("Tank name is %s"), *OurTankName);
//auto OurTankName = GetWorld();
UE_LOG(LogTemp, Warning, TEXT("%s aiming at %s"), *OurTankName, *HitLocation.ToString());
//UE_LOG(LogTemp, Warning, TEXT("Tank name is %s"), *OurTankName);
//UE_LOG(LogTemp, Warning, TEXT("Aiming at %s"), *HitLocation.ToString());
}

//void UTankAimingComponent::Test()
//{
// auto TestName = GetOwner()->GetName();
// UE_LOG(LogTemp, Warning, TEXT("Tank name is %s"), *TestName);
// //UE_LOG(LogTemp, Warning, TEXT("Success"));
//}


Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void ATankPlayerController::AimTowardsCrosshair()
FVector HitLocation; // Out parameter
if (GetSightRayHitLocation(HitLocation)) // Has "side-effect", is going to line trace
{
///GetControlledTank()->AimAt(HitLocation);
GetControlledTank()->TankAimingComponent->AimAt(HitLocation);
}
}

Expand Down Expand Up @@ -84,7 +84,7 @@ bool ATankPlayerController::GetLookDirection(FVector2D ScreenLocation, FVector&
FVector CameraWorldLocation; // To be discarded
return DeprojectScreenPositionToWorld(
ScreenLocation.X,
ScreenLocation.Y,
ScreenLocation.Y,
CameraWorldLocation,
LookDirection
);
Expand Down
2 changes: 1 addition & 1 deletion Unreal/Section04/Source/BattleTank/Public/Tank.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class BATTLETANK_API ATank : public APawn
public:
//void AimAt(FVector HitLocation);

protected:
public:
UTankAimingComponent* TankAimingComponent = nullptr;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class BATTLETANK_API ATankAIController : public AAIController
{
GENERATED_BODY()


private:
virtual void BeginPlay() override;

Expand Down

0 comments on commit b3c8415

Please sign in to comment.