How to spawn and reference non-UObject instances in the world

I have a class called Group that holds an array of AActors (this is just an example so bare with any silliness) along with some other functions and variables. Groups do not need to be placed onto the stage as they are non-physical.

Somewhere in the game I would need to store a list of existing groups for a particular level. Where would be best place for this be, and how would I be able to reference the stored groups?

Example setup (I don’t want to use this because I’d be making a static class just for these groups, and I don’t know how the staticness would affect level changing):

class Group {
	TArray<AActor*> actors;
	void doThingsToActors();
}

class GroupUsingActor : AActor {
	UPROPERTY()
	uint32 groupID;
	void BeginPlay() {
		Somewhere::groups[groupID].doThingsToActors();
	}
}

class Somewhere {
	static TArray<Group> groups;
}

Kind of odd question, obviously somewhere global (like GameMode?) and you reference it normally. I also don’t understand your idea of using static classes, actors are reinitialized on each level and all groups will be invalidated with invalid pointers and need to be recreated on each level, so you could just place groups array on AGameMode or level blueprint class ALevelScriptActor and it will automatically reset on each level as those actors are destroyed at the end of each level. Static non-pointer objects don’t need to be spawned, they like definitions of data structures in memory, if you create new element in array the new group will appear in memory of array and it’s ready to go.

You need to keep in mind that you might have problems from fact that you doing objects outside of reflection system not to mention they won’t be visible to garbage collector, so you could at least consider using struct with USTRUCT() instead of class, structs can do functions too in C++. Reflection system don’t support class instances variables (non-pointer) where it support instences of structs, so it’s another pro of using structs here.

Also by convention non-UObject should have “F” or “T” prefix, in this case you making type so you could use “T”

After sleeping on it I realized what a convoluted question I was asking. I did indeed want to just essentially extend the level blueprint so ALevelScriptActor is what I’m looking for.

Without reflection, I just need to clean things up the normal way right (using a destructor)?

If you have array of objects not pointers or static object (not pointer) variable in class, they will be destroyed together with that object. Not sure with TArray thru how it deals with it as it using pointers to dynamicly resize array, but looking on source it seem it destroys items on destruction:

https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/Runtime/Core/Public/Containers/Array.h#L575

If you create dynamiclly new object with pointer using “new” operator, then you need to use “delete” operator to remove it from memory, if you don’t do that with non-UObject and you lose all pointers to it, then you gonna have sit there in memory forever until process end (aka memory leak)