Engine crashing when attempting to upgrade Toon Tanks

Hi all,

While attempting to add a new functionality to the Toon Tanks exercise, I have found that the engine crashes and I do not know how to go forward.

I am attempting to add a function to count the amount of projectiles the tank has fired, while having a widget on the screen that tells the user how many projectiles are available.

For this, I have done the following:

  • In Tank.h, I have added 2 private variables, Max Ammo, and Current Ammo:

  • In Tank.h, I have added the following public function:

  • In Tank.h, in BeginPlay, I assign the Current Ammo to be Max Ammo:

  • In the Fire function in BasePawn, I call the Tank function CountAmmo once I have checked that the projectile being fired belongs to the Tank and not the Tower:

This function is being called correctly from BasePawn, as I have introduced another UE_LOG within CountAmmo, and it is being correctly printed.

However, when I attempt to use the Tank private variable CurrentAmmo within the CountAmmo() function, Unreal Engine crashes.

I would like this function to count down every time the tank fires, and call the GameOver function from ToonTanksGameMode once the tank has run out of ammo. I have implemented it like this:

But in my testing it seems that any call to CurrentAmmo will generate a crash. Shouldn’t this variable be visible to CountAmmo(), as it is declared in Tank.h? Aren’t we doing something similar with the variable Health in HealthComponent.cpp?

Also, is this the best way to implement such a function? I tried to go from things we have done before, as I am not experienced in this engine.

Thank you for your time.

It’s crashing because you are dereferencing an uninitialised pointer.

AToonTanksGameMode* ToonTanksGameMode; // uninitialised pointer, points to random address
ToonTanksGameMode-> // dereference it. BOOM

Yes but it’s actually pointing to the game mode rather than something random

It seems more complicated that it should be. Implement it in ATank

void ATank::Fire()
    if (CurrentAmmo <= 0)

Hi Dan,

Thank you for your answer. I have corrected the reference to ToonTanksGameMode as you point out (as it’s done in HealthComponent), but Unreal Engine still crashes.

I have added this to the BeginPlay section of Tank.cpp (Variable is forward-declared in Tank.h, and header is added as it should in Tank.cpp):


My CountAmmo function now looks like this:


I have tested commenting either the line of CurrentAmmo, or the GameOver call, and both will result in a crash. The only thing that will not crash the engine is only having the UE_LOG.

The error message from Unreal is an Exception_Access_Violation, and points to both my CountAmmo, and BasePawn (In BasePawn is where I am calling CountAmmo if the owner of a projectile is a tank).

Although I’m planning on implementing it like you suggest (and it has helped me understand better the Super:: functionality that we have seen in the courses), would you have any idea as to what might be causing my implementation to still crash?

Thanks a ton for your help.

Could you show the crash log and code please?

Hi Dan,

Here is a link to my project files, as well as the crash log: https://github.com/kraglos/ToonTanksTesting

Many thanks for taking the time to look into this.



You have the same problem here

Tank is an uninitialised pointer.

This did it, now it works properly. I declared the Tank variable using Cast<> like we had done in ToonTanksGameMode, and now it correctly tracks within the Count Ammo function. This little exercise also helped me better understand why we needed to cast in the first place.

Thanks a lot for your help Dan!

I followed a similar strategy to set up the widget on the screen. I have not started the Blueprints course yet, but I managed to print the correct results adding this to the ToonTanksGameMode blueprint:

As I understand it, at BeginPlay we create the Ammo Widget and we add it to the viewport. Then, at every tick, the Tank that belongs to the player pawn is accessed to get its Current Ammo variable, which is then printed through SetText.

Could you tell me if this way of doing it would be considered apropriate? Or Is there an easier way to go around it?

Thanks again for your effort on this topic.

I would promote the return of the cast to a variable and then use a property binding

Property Binding for UMG in Unreal Engine | Unreal Engine 5.3 Documentation

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

Privacy & Terms