Local versus instance variables
Most of our work so far has involved instance variables, that is, variables in a class that are accessible from any function in that class and even from other classes. Instance variables persist until that object is destroyed, so they're used for things we need to know about that object all the time, like Health
for our Pawn
class or the amount of ammo we have in our weapon. We've also used a few when we needed to store a variable until a different function needed to use it, like the
CurrentCameraLocation
and CurrentCameraRotation
in our AwesomePlayerController
class, which were processed in PlayerTick
and used in GetPlayerViewPoint
.
Sometimes, however, we'll want "throwaway" variables that we only need while we're in a function, and we don't need to keep them or access them from anywhere else. For this, we use local variables.
Local Variables
Let's try some experiments with local variables in our AwesomeGame
class.