Struct Embedding in UnrealScript
Our first example demonstrates struct embedding in UnrealScript. While UnrealScript doesn’t have direct struct embedding like some other languages, we can achieve similar functionality using inheritance.
In this UnrealScript version:
We define a
Base
class with anum
field and aDescribe
method.We create a
Container
class that extendsBase
, adding astr
field.We use inheritance to achieve a similar effect to struct embedding. The
Container
class inherits all properties and methods fromBase
.We create an
Init
method forContainer
to initialize its fields, as UnrealScript doesn’t support constructor overloading.We demonstrate accessing fields and methods from the base class.
In UnrealScript, all classes implicitly implement the
Object
interface, so we don’t need to explicitly declare an interface for this example.We use the
log
statement (represented here by the`log
macro) instead offmt.Printf
orfmt.Println
for output.
To run this code in Unreal Engine:
- Create a new UnrealScript file with this code.
- Compile the script in your Unreal Engine project.
- You can then call the
Example
function from other parts of your game code or through the Unreal Engine console.
This example demonstrates how UnrealScript uses inheritance to achieve functionality similar to struct embedding, allowing for a form of composition and method inheritance.