Time for action – Using dynamic arrays
Dynamic arrays are declared a bit differently than static arrays. Let's declare one now.
Change our variable declaration line to this:
var array<int> Baskets;
As we can see, with dynamic arrays we don't put a number anywhere in it; it can be whatever size we need it to be. By default they're completely empty, so if we tried to log any of its values we would get an out of bounds warning similar to our experiment with static arrays.
We can, however, assign values any time we want, so let's add this line to our
PostBeginPlay
function:Baskets[3] = 9;
Then log the value like this:
'log("Baskets:" @ Baskets[3]);
Now our function should look like this:
function PostBeginPlay() { Baskets[3] = 9; 'log("Baskets:" @ Baskets[3]); }
Compile and test, and we can see that the value logs fine, and we didn't get any warnings.
[0007.82] ScriptLog: Baskets: 9
When we assign a value, the size of the array automatically changes to that value. As with before, if we tried...