Actor vs ActorHandle

SDT4 offers a distinction between ActorHandle and Actor. This distinction is for performance reasons, as querying possibly up to thousands of actors will create unnecessary heap allocations.

The difference is that ActorHandle is a readonly struct whilst Actor is a class. They both contain the same functionality when it comes to the Actor Component System, however Actor is the required class whenever dealing with scripts (see Scene and Actor Scripts)

using System;
using SDT4.Managed.Core;
using SDT4.Managed.Debugging;
// ...
Scene scene = /*...*/;
ActorHandle structActor = scene.CreateEmptyActor("New actor");
Actor classActor = structActor.AsActor(); // This will get a class instance of an actor.
DebugConsole.Print($"ActorHandle              = {someActor}"); 
DebugConsole.Print($"Actor                    = {classActor}");
DebugConsole.Print($"ActorHandle (from Actor) = {classActor.Handle}");

Converting to script instances

When spawning prefabs (see Spawning Scripts), you may want to access the instance, this cannot be done through ActorHandle, so you must access using the AsActor() or AsScript<>() functions.

ActorHandle myPrefabActor = ...;
// Suppose myPrefabActor has "A_MyScript" attached
A_MyScript script = myPrefabActor.AsScript<A_MyScript>()!;
// This is an equivalent statement
script = (myPrefabActor.AsActor() as A_ScriptedActor)!;

This power guarantees that any Actor instance correlates to the underlying script instance, while ActorHandle is a performant alternative to allow querying thousands of actors without the overhead of memory allocations or the garbage collector.