Create the same actors, use a data table or better yet

Hello,guys.
I have a question.
I have a SoldierBasePawn,it’s has three member variables: Health, AttackDamage, StaticMesh(Model).
I want to use this as a basis to expand a lot of classes.For example, Pikemen and Swordsmen.They are the same except for health, attack damage, and models.
Should I create PikemenPawn,SwordsmenPawn? Wouldn’t this be a waste of resources? Because they differ by only 3 variables. or create a table.This table records the variables of the soldiers, and then the game generates soldiers based on those variables?
Or is there a better way?

It depends on what you are looking for at the end. I have a master unit class and then do a child that has a different static mesh… But when it comes to spawn all these guys, I use Structure of this master and Data Table that has lots of different units, they all can have different health, damage and what ever you want. Main point there is to have Class as a variable, so you can spawn actor of class in the game.
Let me know if you got the idea or you need more clarifications.

Best regards

You said that different units are generated according to the class, since there are already different class, do you still need to use a table to store the information of health value? Is it better to store health in Class?

@CorgiofLiu depends on what you want to do. For example:
We have Class Warrior- health 0, stamina 0
We have child class - samurai - health 200, stamina 200
We can have Ninja - health 150, stamina 150
here we need to have 1 master class and 2 child classes as BP with 2 different Skeletal meshes,

but you can make one Child class and in a Structure you can set the following:
Skeletal Mesh | Health | Stamina | Speed | whatever else
and then in the Data table set this the following way:
Samurai SkelMesh | 200 | 200 | 600 | Something
Black Samurai | 300 | 300 | 800 | Something
Ninja | 150 | 150 | 1000 | Something
Knight | 600 | 600 | 400 | Something

Then you Spawn Actor of Class and provide a new Actor with the following details from the DataTable. You need to make all variables either public and editable, or make a structure inside the class and set all variables in the Construct method

I hope you get my point. Let me know if you want more clarification.

I’m so sorry, I haven’t been back to the forum for too long and forgot to reply to you. I’m trying what you taught me.
I found some problems with this, if I were to spawn a large number of identical soldiers, such as ninja, I would need to get the ninja’s information from the datatable repeatedly.

you’d only access the datatable once on Beginplay most likely so its not an issue.

that said i’d recommend inheritance over datatables, as in the long run you’d likely have different meshes, soundfx, animations, vfx etc for different classes and that can get heavy if its all loaded into one data table

1 Like

Thank you,If I have many kinds of soldiers, do I want to create an actor classes for each of them as well?

using inheritance you’d create child actors from the base actor, then all you need to change is the variables, ie health, mesh etc all the functionality will work from the parent

You are right, but you need to store these variables to change them from? And here is your Datatables comes handy.
You can read this datatables once, or any time you need to spawn an actor.
Or, if you need to spawn the same class or same actor many times, you can make a structure, you can set array of structures filled once from datatble and when you need to spawn an actor you read it from structure.
I have an enemy manager. This class holds information of all enemies and can spawn different units when I need it. It loads all classes structures at start and when I need a new vawe of enemies, it spans different enemies from structures in places I need.
All this is in one datatable. Not so big, but holds a lot.
Loading take milliseconds, nobody cares as you have loading screen at this ti.e.
Then it is spawns and re spawns when needed.

The are many approaches, find the best that works for you and try it. Check the performance every time you do changes. See what works the best.
There is no right answer in software developing, same to games.

Cheers

@CorgiofLiu Think about your enemies:

  1. There is no point of making different classes if you have different Mesh, sound, FX, weapon… Look at Epic examples, they do one class, then change that on spawn from DT, or structure.
  2. You need different classes in case you have different functionality: I have 12 buildings, all child of Master Building. I have Townhall that spawns units, it is the only building that does it. SO the function is only there. I do not want to overload Master Building with all functions that I will have for all buildings and then turn it off/on based on the building name. Easier to work with different classes. But for units, they all have the same functions, find units, shoot units, consume food, die. They can be big, small, with pistol, with rifle, walk, run, fly… but all these functions are the same, different skeletal meshes, different weapon mashes, so I have one class Master Unit and 2 child, enemy and player unit. Player units has conroll functions and some more, Enemy unit doesn’t have it, but they do share some common functions as food consumption, death, shooting and many more.

So, think in front what is your approach here, and what you want to achieve.

the variables are stored in the child class,

either approach is fine, i personally dont like data tables because it creates a hard reference to all the assets and must load all the assets anytime its accessed. you could use soft references but then you have to load each thing individually, with child classes you just load one class and it loads all its dependencies.

also you never know if you may want different functionality down the line, say you create a flying unit and now need to override the movement, easily done with this system.

but we can agree one whatever suits your game best

I move from child class to Data assets is much more manageable, you can maintain inheritance for different enemies just add the pawn class. With datatables, only the one you are using at that moment is loaded. I use soft references for everything and load them when they spawn.

1 Like

Agreed i also love Data Assets

Thank you ,everyone @DomusLudus @Auran131 @chuklov , I’ll try each option to find the one that works best for my own project.