UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
LightWeightInstanceManager.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
9#include "UObject/Object.h"
10#include "Engine/EngineTypes.h"
13
14#include "GameFramework/Actor.h"
16
17#include "LightWeightInstanceManager.generated.h"
18
19
20class AActor;
22
23// Used for initializing light weight instances.
28
29
30UCLASS(BlueprintType, Blueprintable, Experimental, MinimalAPI)
32{
34
38
39public:
41
42 ENGINE_API virtual void Tick(float DeltaSeconds);
43
44 // Returns the location of the instance specified by Handle
45 ENGINE_API FVector GetLocation(const FActorInstanceHandle& Handle) const;
46
47 // Returns the rotation of the instance specified by Handle
48 ENGINE_API FRotator GetRotation(const FActorInstanceHandle& Handle) const;
49
50 // Returns the name of the instance specified by Handle
51 ENGINE_API FString GetName(const FActorInstanceHandle& Handle) const;
52
53 // Returns true if this manager stores instances that can be turned into full weight objects of class OtherClass
54 ENGINE_API bool DoesRepresentClass(const UClass* OtherClass) const;
55
56 // Returns true if this manager is capable of representing objects of type OtherClass
57 ENGINE_API bool DoesAcceptClass(const UClass* OtherClass) const;
58
59 // Returns the base class of types that this can manage
60 ENGINE_API UClass* GetAcceptedClass() const;
61
62 // Sets the specific class that this manages
63 ENGINE_API virtual void SetRepresentedClass(UClass* ActorClass);
64
65 // Returns the index of the light weight instance associated with InActor if one exists; otherwise we return INDEX_NONE
66 ENGINE_API int32 FindIndexForActor(const AActor* InActor) const;
67
68 // Returns a handle to a light weight instance representing the same object as InActor and calls destroy on InActor if successful.
69 ENGINE_API FActorInstanceHandle ConvertActorToLightWeightInstance(AActor* InActor);
70
71 // LWI grid size to use for this manager.
72 ENGINE_API virtual int32 GetGridSize() const;
73
74 // Helper that converts a position (world space) into a coordinate for the LWI grid.
75 ENGINE_API FInt32Vector3 ConvertPositionToCoord(const FVector& InPosition) const;
76
77 // Helper that retrieve the world space bounds of the LWI grid cell encompassing the provided coordinate.
78 ENGINE_API FBox ConvertPositionToGridBounds(const FVector& InPosition) const;
79
80 ENGINE_API bool HasAnyValidInstancesOrManagedActors() const;
81
82 // IActorInstanceManagerInterface begin
83 ENGINE_API virtual int32 ConvertCollisionIndexToInstanceIndex(int32 InIndex, const UPrimitiveComponent* RelevantComponent) const;
86 virtual UClass* GetRepresentedClass(const int32 InstanceIndex) const override { return GetRepresentedClassInternal(); }
87 virtual ULevel* GetLevelForInstance(const int32 InstanceIndex) const override { return GetLevel(); }
88 ENGINE_API virtual FTransform GetTransform(const FActorInstanceHandle& Handle) const;
89 // IActorInstanceManagerInterface end
90
91protected:
92 UClass* GetRepresentedClassInternal() const;
93
94 // Creates an actor to replace the instance specified by Handle
95 ENGINE_API AActor* ConvertInstanceToActor(const FActorInstanceHandle& Handle);
96
97 ENGINE_API int32 AddNewInstance(FLWIData* InitData);
98
99 // Adds a new instance at the specified index. This function should only be called by AddNewInstance.
100 // Provided to allow subclasses to update their per instance data
101 ENGINE_API virtual void AddNewInstanceAt(FLWIData* InitData, int32 Index);
102
103 // Removes the instance
104 ENGINE_API virtual void RemoveInstance(int32 Index);
105
106 // Returns true if we have current information for an instance at Index
107 ENGINE_API bool IsIndexValid(int32 Index) const;
108
109 // Checks if we already have an actor for this handle. If an actor already exists we set the Actor variable on Handle and return true.
110 ENGINE_API bool FindActorForHandle(const FActorInstanceHandle& Handle) const;
111
112 // Sets the parameters for actor spawning.
113 ENGINE_API virtual void SetSpawnParameters(FActorSpawnParameters& SpawnParams);
114
115 // Returns the class to use when spawning a new actor
116 ENGINE_API virtual UClass* GetActorClassToSpawn(const FActorInstanceHandle& Handle) const;
117
118 // Called after actor construction but before other systems see this actor spawn
119 ENGINE_API virtual void PreSpawnInitalization(const FActorInstanceHandle& Handle, AActor* SpawnedActor);
120
121 // Called after spawning a new actor from a light weight instance
122 ENGINE_API virtual void PostActorSpawn(const FActorInstanceHandle& Handle);
123
124 // Called after a spawned actor is destroyed
125 ENGINE_API virtual void OnSpawnedActorDestroyed(AActor* DestroyedActor, const int32 DestroyedActorInstanceIndex);
126
127 // Helper functions for converting between our internal storage indices and the indices used by external bookkeeping
130
131 //
132 // Data and replication functions
133 //
134
135public:
136
137 ENGINE_API virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
138
139private:
140 UFUNCTION()
141 ENGINE_API void OnSpawnedActorDestroyed(AActor* DestroyedActor);
142
144
145 FString BaseInstanceName;
146
147 UPROPERTY(EditDefaultsOnly, Replicated, Category=LightWeightInstance)
148 TSubclassOf<AActor> RepresentedClass;
149
150 UPROPERTY()
151 TSubclassOf<AActor> AcceptedClass;
152
153 //
154 // Per instance data
155 // Stored in separate arrays to make ticking more efficient when we need to update everything
156 //
157
158 // Current per instance transforms
159 UPROPERTY(ReplicatedUsing=OnRep_Transforms, EditInstanceOnly, Category=LightWeightInstance)
160 TArray<FTransform> InstanceTransforms;
161 UFUNCTION()
162 ENGINE_API virtual void OnRep_Transforms();
163
164 //
165 // Subclasses should override the following functions if they need to store any additional data
166 //
167
168 // Increases the size of all of our data arrays to NewSize
169 ENGINE_API virtual void GrowDataArrays();
170
171 // Populates the data array entries at Index with the data stored in InData;
172 ENGINE_API virtual void UpdateDataAtIndex(FLWIData* InData, int32 Index);
173
174 // Allocates the appropriate subclass of FLWIData to initialize instances for this manager. The caller is responsible for freeing the memory when they are finished.
175 ENGINE_API virtual FLWIData* AllocateInitData() const;
176
177 // Gathers info from InActor and stores it in InData. Returns true if the relevant data was successfully copied.
178 ENGINE_API virtual bool SetDataFromActor(FLWIData* InData, AActor* InActor) const;
179
180 //
181 // Bookkeeping info
182 //
183
184 // keep track of which instances are currently represented by an actor
187
188 TArray<int32> DestroyedActorIndices;
189
190 // list of indices that we are no longer using
192 TArray<int32> FreeIndices;
193
194 // handy way to check indices quickly so we don't need to iterate through the free indices list
196 TArray<bool> ValidIndices;
197#if WITH_EDITOR
198 //
199 // Editor functions
200 //
201protected:
202 ENGINE_API virtual void PostEditChangeProperty(struct FPropertyChangedEvent& PropertyChangedEvent) override;
203#endif // WITH_EDITOR
204
205#if !UE_BUILD_SHIPPING
207#endif
208};
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define UPROPERTY(...)
UObject definition macros.
Definition ObjectMacros.h:744
#define UFUNCTION(...)
Definition ObjectMacros.h:745
#define GENERATED_UCLASS_BODY(...)
Definition ObjectMacros.h:768
#define UCLASS(...)
Definition ObjectMacros.h:776
Definition Actor.h:257
Definition LightWeightInstanceManager.h:32
virtual int32 ConvertInternalIndexToHandleIndex(int32 InInternalIndex) const
Definition LightWeightInstanceManager.h:128
virtual ULevel * GetLevelForInstance(const int32 InstanceIndex) const override
Definition LightWeightInstanceManager.h:87
TArray< int32 > InstanceToActorConversionsInProgress
Definition LightWeightInstanceManager.h:206
virtual int32 ConvertHandleIndexToInternalIndex(int32 InHandleIndex) const
Definition LightWeightInstanceManager.h:129
Definition ActorInstanceManagerInterface.h:23
Definition Array.h:670
Definition UnrealString.h.inl:34
Definition SubclassOf.h:30
Definition Class.h:3793
Definition Level.h:423
Definition LightWeightInstanceBlueprintFunctionLibrary.h:12
Definition RobinHoodHashTable.h:18
U16 Index
Definition radfft.cpp:71
Definition ActorInstanceHandle.h:33
Definition World.h:419
Definition LightWeightInstanceManager.h:25
FTransform Transform
Definition LightWeightInstanceManager.h:26
Definition LightWeightInstanceSubsystem.h:16
Definition UnrealType.h:6865
Definition ObjectPtr.h:488