Creating Actors And Objects
New keyword
The New keyword is used to create new objects which are:
- not a subclass of Actor
- not created within the context of an Unreal level (like the MeshActor in the player selection window)
The parameters are the owner object, the object's name and the object's flags. (Probably the object flags constants in the Object class?)
local Object to; to = new class'TestObj'; to = new()class'TestObj'; to = new(self)class'TestObj'; to = new(self,'')class'TestObj'; to = new(self,'',0)class'TestObj';
A guess about the "declaration" of the New operator:
Object New(optional Object NewOuter, optional name NewName, optional int NewFlags) Class NewClass
Parameters:
- NewOuter
- An object which will be assigned to the new object's Outer property. Self if omitted.
- NewName
- The new object's name.
- NewFlags
- The new object's ObjectFlags. (add the RF_anything constants for valid values)
- NewClass
- The new object's class.
When non-actor objects are created in UT2003 then the object's Created function is called.
Creating actors with the New keyword causes the game or UCC commandlets to crash.
Be careful when using the new keyword to create objects as well has maintaining references between Objects and Actors, as it is very easy to cause crashes during serializiation if not used properly. The first element you specify with the new keyword specifies an Objects Outer, which can be roughly be equated to it's owner, and this can cause problems very quickly if you specify an Actor as the outer of an Object. When garbage collection occurs, if the Actor is deleted before the Object is then you will crash the engine when it attempts to access the Object's Outer. Your best bet using new is to use possibly the Level as the outer, or just None as the outer, that way you can avoid the whole garbage collection issue altogethor.
When dealing with Objects you must be absolutely sure to clean up any references between Actors and Objects, generally by explicitly setting those references to None where appropriate. In cases where an Actor has a reference to an Object, a good place to clean that up would be to just overload the Destroyed() event:
class MyActor extends Actor; var MyObject Obj; event PostBeginPlay() { Obj = new(NONE) class'MyObject'; } event Destroyed() { Obj = NONE; Super.Destroyed(); }
Spawn function
The Spawn function is used to create new actors. Other that the New keyword Spawn can only be called from a non-static function and can only create actors. If you need to call the Spawn function from a non-actor object make sure it has access to an Actor whose Spawn function can be used then.
native(278) final function actor Spawn ( class<actor> SpawnClass, optional actor SpawnOwner, optional name SpawnTag, optional vector SpawnLocation, optional rotator SpawnRotation );
If SpawnLocation and/or SpawnRotation is not specified the spawner's Location and Rotation is used. (The spawner is the actor Spawn is called from.)
Spawn returns an object of the class specified in SpawnClass, not an object of class Actor, i.e. you don't have to cast it to the class you need. This is hardcoded in the compiler.
local Projectile P; P = Spawn(ProjectileClass, Instigator,,, Instigator.ViewRotation);
You don't have to use P = Projectile(Spawn(ProjectileClass)), in fact this will cause an error when compiling.
Warning: Spawning an actor for an owner whose deletion is pending reliably crashes the game with a "Unknown code token" error. If in doubt, make sure you check the new owner's bDeleteMe property first.