Dear Everyone,
If you are using code to spawn objects, you more than likely want to keep track of those objects!
You also are probably wanting to spawn objects that are instances of your own Classes.
Doing this is can seem a bit syntactically convoluted at first, but once you get used to it it's easy!
Here are the basics:
Simple Dynamic Array
Note that you do not use new, or try to make an instance of this var in anyway.
~~~
Deleting Dynamic Arrays
The .length of the array can be adjusted to 0 to delete entries:
~~~
Memory Allocation
The memory for the array is automatically allocated when you use the array function AddItem.
~~~
.AddItem()
//continuing above example
The additional memory is allocated by Unreal engine for you.
So
let me repeat:
You NEVER need to use the new command with dynamic arrays, in regards to the array itself.
~~~
Making Instances of Your Class Via Code
To make new instances of a class you can say:
~~~
Local vs Global Vars With Dynamic Arrays
How to Save a Local var To a Global Dynamic Array
You use "local" for all variables when inside a function, "var" for global variables that are part of class itself
local variables get deleted at the end of the function execution, so a dynamic array you want to repeatedly refer to needs to be GLOBAL and made with "var" not local.
Locally created variables, made inside a function, persist after the end of the function if stored inside a global dynamic array variable.
After this function runs willGetLost is invalid, but v will live on, referenced by the global array happyArray.
In this way you can create elements of your global dynamic variable locally, inside a function, but then access them again later in your game's running time via the dynamic array.
~~~
Making Array of Instances of Your Class
But how to make a DynamicArray of your custom class?
You might find info as I did on the net saying things like
with everyone discussing how you have to space out the two >> so it is not read as an operator.
But this will make an array of CLASSES of your type of class, not objects that are the instances of your class.
~~
The Actual Correct Code
to make an array of the actual INSTANCES of your class, you must use this code
THIS IS THE KEY SYNTAX!
THE BIG THING which I had to figure out after hours of research
That's it!
Surprisingly simple.
~~~
Summary:
Making dynamic arrays and handling their memory allocation is very simple using Unreal Script using the above bits of code.
Now you can dynamically generate Actors / Objects / Level Geometry and keep track of it easily!
~~~
Dynamic World Making In Game
I've used dynamic arrays understanding to make a game with UDK where you can build the level yourself inside of the actual game, and save it to file, and load it after closing the Unreal Game Engine and even restarting your computer.
~~~
Enjoooy!

♥
EverNewJoy
PS: See http://wiki.beyondunreal.com/Dynamic_array
for dynamic array functions
PSS:
No Internet Replication
Dynamic Arrays cannot be easily used for multiplayer games, as dynamic arrays are never replicated to the client from the server.
So when planning your game structure keep in mind you will have to send data from the server to the client some other way
Kismet
Use kismet to handle dynamic objects via the Actor Factory in multiplayer levels.
I've tested this and it works wonderfully.
If you are using code to spawn objects, you more than likely want to keep track of those objects!
You also are probably wanting to spawn objects that are instances of your own Classes.
Doing this is can seem a bit syntactically convoluted at first, but once you get used to it it's easy!
Here are the basics:
Simple Dynamic Array
Code:
var array<int> intArray;
~~~
Deleting Dynamic Arrays
The .length of the array can be adjusted to 0 to delete entries:
Code:
yourArray.length = 0;
//deletes all entries from array, but does not delete actual objects in the array
Memory Allocation
The memory for the array is automatically allocated when you use the array function AddItem.
~~~
.AddItem()
//continuing above example
Code:
intArray.AddItem(5);
//adds a single item to intArray whose int value is 5
So
let me repeat:
You NEVER need to use the new command with dynamic arrays, in regards to the array itself.
~~~
Making Instances of Your Class Via Code
To make new instances of a class you can say:
Code:
local MyHappyClass happyObj; happyObj = new class'MyHappyClass';
Local vs Global Vars With Dynamic Arrays
How to Save a Local var To a Global Dynamic Array
You use "local" for all variables when inside a function, "var" for global variables that are part of class itself
local variables get deleted at the end of the function execution, so a dynamic array you want to repeatedly refer to needs to be GLOBAL and made with "var" not local.
Locally created variables, made inside a function, persist after the end of the function if stored inside a global dynamic array variable.
Code:
class Joy extends Happiness; //Global Variable var array<MyHappyClass> happyArray; function newHappyObj(){ local MyHappyClass v; local int willGetLost; //note the name v = new class'MyHappyClass'; happyArray.AddItem(v); willGetLost = someVeryImportantValueButAlasNoOneWillKnow; return; //end of local context }
In this way you can create elements of your global dynamic variable locally, inside a function, but then access them again later in your game's running time via the dynamic array.
~~~
Making Array of Instances of Your Class
But how to make a DynamicArray of your custom class?
You might find info as I did on the net saying things like
Code:
var array<class<Your class> >;
//this code is NOT an array of variables of your custom class type
with everyone discussing how you have to space out the two >> so it is not read as an operator.
But this will make an array of CLASSES of your type of class, not objects that are the instances of your class.
~~
The Actual Correct Code
to make an array of the actual INSTANCES of your class, you must use this code
THIS IS THE KEY SYNTAX!
THE BIG THING which I had to figure out after hours of research
Code:
var array <MyHappyClass> happyOBJArray;
That's it!
Surprisingly simple.
~~~
Summary:
Making dynamic arrays and handling their memory allocation is very simple using Unreal Script using the above bits of code.
Now you can dynamically generate Actors / Objects / Level Geometry and keep track of it easily!
~~~
Dynamic World Making In Game
I've used dynamic arrays understanding to make a game with UDK where you can build the level yourself inside of the actual game, and save it to file, and load it after closing the Unreal Game Engine and even restarting your computer.
~~~
Enjoooy!

♥
EverNewJoy
PS: See http://wiki.beyondunreal.com/Dynamic_array
for dynamic array functions
PSS:
No Internet Replication
Dynamic Arrays cannot be easily used for multiplayer games, as dynamic arrays are never replicated to the client from the server.
So when planning your game structure keep in mind you will have to send data from the server to the client some other way

Kismet
Use kismet to handle dynamic objects via the Actor Factory in multiplayer levels.
I've tested this and it works wonderfully.
Comment