Construct By-Value VS By-Ref
Updated 453 Days AgoPublic

Advanced Users Only: The use of the new and local Keywords is not something most users need to be concerned with, as it is almost entirely Deprecated. Heap or stack allocation is almost always handled automatically by the engine based off whether the constructed type is a class or struct, however, new and local still remain in the language for explicit allocation practices. Users attemptting to use new or local keywords should be aware that they override the standard allocation functionality of Zilch, but reference counted garbage collection will still happen for objects explicitly constructed with new and local.


When working with by-value types that have constructors, such as structs or more complex stack primitive data types, the keyword local may be used:

var up : Real3 = local Real3(0.0, 1.0, 0.0);
var stackStructInstance : CustomStruct = local CustomStruct();

The compiler will infer the use of local for by-value types:

// This is also valid.
var up : Real3 = Real3(0.0, 1.0, 0.0);
var stackStructInstance : CustomStruct = CustomStruct();


When working with Classes and Structs, references, or other or anything located on the heap with a constructor, use the new keyword:

var heapObject : CustomClass = new CustomClass();
var heapStructInstance : CustomStruct = new CustomStruct();

Once again the compiler will infer the use of new for by-ref types:

// This is also valid.
var heapObject : CustomClass = CustomClass();

Since structs are by-value, in order to get a reference you must specify new.

// This does not compile
var heapStructInstance : CustomStruct = CustomStruct();
Console Output
The value being assigned to 'heapStructInstance' must be of type 'CustomStruct'. Its type is 'CustomStruct'.

Related Material


Classes and Structs
Memory Management

Last Author
Last Edited
Oct 25 2017, 1:55 PM