Download

What class is most suitable for pure data storage?

I’m implementing a variation of the command pattern where I want to encapsulate every possible AI command (“Receive an item, attack another NPC, change locations,” etc) in a class, and store sequences of these commands in an array to be executed in order. I originally tried to do this with structs, the idea being that each child would have its own unique variables (storing which item they receive, which character they would attack, or anything else), and an override of a common Execute function that would actually call the desired manager/logic objects and enact the action like this:


 USTRUCT(BlueprintType)
 struct FTestBase {
     GENERATED_BODY()    
         virtual void Execute() {
         GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Base function");
         }
 };
 
 USTRUCT(BlueprintType)
 struct FTestChild : public FTestBase {
     GENERATED_BODY()    
         void Execute() override {
         GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Child function");
         }    
 };

However, I learned that you can’t override structs like this; if you have an array of type FTestBase, even if you fill it with variations of FTestChild the compiler will treat them like instances of FTestBase. The obvious solution to this seems to be doing the exact same logic with classes, instead of structs, but I’m really unclear on what class would actually be appropriate for data storage; the two simplest things I can think of would be to derive from AInfo or even just UObject, but there’s an awful lot of functionality there that I don’t need. Is there an obvious class I should be using for this?

Fundamentally, a class and a struct are pretty much the same thing. A class just extends the functionality of a struct. You don’t lose any performance for using classes instead of structs, so go ahead and use classes. If you want your classes to be exposed to UE4, you’ll want to at least inherit from the UObject class.

So wait, not to ask a really dumb question, but I had been under the impression that the only way to declare/define a new class in UE was to use the editor and go through the rigmarole of selecting a parent class and letting the editor slip all the code into the project… is it possible to literally declare an empty class like a struct inside another object’s code, and just add something like this inside any .h?



class FTestBase {	
	public:
		virtual void TestFunc() {
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Base function");
		}
};


class FTestChild : public FTestBase {
	public:
		void TestFunc() override {
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Child function");
		}
};

That compiles cleanly, but doing something like the following displays “Base function” twice, not “Base” once and “Child” once, so I’m not sure if it actually works properly:


	TArray<FTestBase> testArr;
	FTestBase base;
	FTestChild child;
	testArr.Add(base);
	testArr.Add(child);
	for (int i = 0; i < testArr.Num(); i++) {
		testArr*.TestFunc();
	}