Time for action – Out parameters
Passing parameters to functions lets us do things with the values that are passed, but what if we need to modify the variables themselves and pass them back? For this we would use out parameters.
Let's change the
DoSomething
function in ourAwesomeEnemySpawner
class:function DoSomething(out float MyFloat, out int MyInt) { MyFloat = 5.0; MyInt = 18; }
We've declared two out parameters, one
int
, and onefloat
. In the function we change their values, and that's it.Now let's rewrite our
PostBeginPlay
function to call it:function PostBeginPlay() { local float MyF; local int MyI; super.PostBeginPlay(); DoSomething(MyF, MyI); `log(MyF @ MyI); }
We'll use two local variables, and then call
DoSomething
using them. Afterward we'll log their values.Compile the code and test. Now let's look at the log:
[0004.54] ScriptLog: 5.0000 18
It's important to remember that the values aren't passed back to the function that called us unless our parameters...