Data types - ItsDeltin/Overwatch-Script-To-Workshop GitHub Wiki

OSTW provided 2 kinds of data types which can be used to encapsulate data: classes and structs.

Value types and reference types

Variables with value types holds data directly. Variables with reference types (classes) holds a pointer to the actual data. Using a value type as parameter requires all of the struct values to be copied unless the parameter is marked with in or ref.

Value types includes String, Number, Boolean, Player, Vector, enumerators, arrays, and structs.

struct MyStruct { public Number Value; } // 'MyStruct' is a Value type.
class MyClass { public Number Value; } // 'MyClass' is a Reference type.
MyStruct a = { Value: 5 };
MyStruct b = a; // A copy of 'b' is assigned to 'a'.
b.Value = 3;

// a.Value is 5, b.Value is 3.
MyClass c = new MyClass();
c.Value = 10;

MyClass d = c; // The reference to the class created via new is copied to the variable 'd'.
d.Value = 15; // Set d.Value to 15. Since 'd' references the same object as 'c', it is the same as doing 'c.Value = 15'.

// c.Value and d.Value is 15

Classes versus structs

Structs are more lightweight compared to classes, so it is recommended to use structs by default if a class-only feature isn't required for the situation.

Classes

+ Can be passed via reference.
+ Supports inheritance.
+ Variables are reused throughout different classes.
+ Supports constructors.
+ Recursive references. This means that you can have a class within the same class.
- Maximum of 999 instances. (max workshop array length - 1)
- Requires instantiation before use and clean-up after.

Structs

+ Lower server load.
+ Workshop output is cleaner and easier to understand.
- Values cannot be set in all contexts.