Time for action – Using local variables
We'll use a function we haven't used yet for this so we can keep it easily readable. In addition to
PostBeginPlay
, allActor
classes have aPreBeginPlay
event that is called beforePostBeginPlay
during startup. We'll use this for our experiments.Local variables are declared like instance variables, except we use
local
in the declaration line. Local variables can only be declared inside a function, and must be at the top of the function before any other lines of code. To see for ourselves, let's make aPreBeginPlay
function in ourAwesomeGame
class:function PreBeginPlay() { super.PreBeginPlay(); local int i; i = 5; `log("This is i:" @ i); }
If we try to compile this, we'll get a compiler error:
Error, 'Local' is not allowed here
Let's try rearranging the
PreBeginPlay
function to move the variable declaration to the top:function PreBeginPlay() { local int i; super.PreBeginPlay(); i = 5; `log("This is i:" @ i); }
It compiles...