HI, quick question: Should I worry about how many variables and functions I use in a parent class?
I have a base class of enemy that i plan on making a lot of children off but i’m concerned about using to much Memory. The base class will have quite a lot variables and function that I need. I normal won’t worry about this if i only used a few in each level, but I plan on putting 100s or even 1,000s in a single level (only a few will ever be on screen at once with the others having Tick turned off to help on performance). Not to mention the pre-inherited variables from the Actor Class and any that i add on the children. Now i’m one of those guys who tries to optimize every little thing even if its not needed, and since i’m using blueprint for a 2D pixel art game in UE4 I have to be vary careful.
I dont know what the source code of the engine is like but I cant imagine the designers making it so that every instance of a class creates its own deep copy of the code functions. the variables yes - those would need their own instances and therefore take up additional memory for each object that has them. So to optimize those, you can identify any that should always be the same across all Actors of that class and instead put them in a shared singleton object.
there might even be a way to declare those as static class variables instead of object variables. If there is you could optimize that way I imagine.
@Shadowriver would probably know the answer.
When you’re placing blueprints in the map, you’re placing an instance of that blueprint, not a copy. But if you want to limit any (theoretical) overhead of having too many variables in your character, you can split any behaviour/script into Actor Components that only some characters need, instead of dropping them all on one character regardless if they need them or not.
Here’s a good example: Using BP Components for Game Behaviour | Live Training | Unreal Engine - YouTube
If you’re going to use a variable or function across all children by all means put it in a parent class. I personally avoid having too long of a inheritance hierarchy so sometimes I end up with variables and functions that I will need on only most subclasses.
My assumption in your case, though, is that you’re going to run into performance problems before you run into memory problems, but I suppose it depends on what platform you’re developing for and the scope of your game. Just try to not have anything unnecessary or redundant.
Also, it’s worth searching around for built in systems that accomplish what your functions do. There are a lot of tools already there to use.
Well I did an experiment on a packaged project; I made a child of the enemy class and added a ton of variables to it and spawned 10,000 in a level and according to Task Manager it only went up by a few MB. So I guess it was nothing to worry about?
Sorry for the late reply everyone and thanks for the responses.