Download

"Fabricating" a unit

Hi there. I’m working on a simple prototype of a naval warfare but don’t have many ideas on how some of the functionality can be achieved. Let’s say that player has a way to “configure” a ship.
For example, you can specify that ship should have:

  • 1 engine
  • 1 rudder
  • 2 small cannons
    Each of these items is an actor with it’s own 3D model and associated blueprint to define their behavior.
    As you might already guessed, what I want to do is dynamically spawn a hull of the ship, which would be a “root” for other actors and “attach” them to the hull. By “attach” I don’t only mean connecting the meshes themselves but getting access to their functionality as well.
    For example, engine contains a specification of how much power it can generate, fuel consumption, methods to calculate how much force it can produce per delta time and etc. Hull can query each of the engines (if there are more than one) and calculate the final force which have to be applied taking into account mass of the whole body.

So far, I can build a pre-configured ship manually, by adding all meshes and static arrays with specification of items. But this way, all of the functionality of engines, cannons and etc. is described by the blueprint of the hull. Which means that if I want to have more than one type of engine, their functionality have to be coded in the same place. What if I want to use two completely different engines on the same ship? All this options make hull blueprint more and more complicated. So I don’t think this is a right approach at all.
The end goal is to generate ship from a specification of components (output from a ship editor) and spawn it in the world.

Any ideas how this can be done?

What you are describing sounds fairly straight forward, what are you having trouble with exactly?

Here is a (rough) way to go about it…

You have your “root” hull blueprint right? Inside that blueprint you can have a few Actor variables (for cannons, engines etc.) then your “root” blueprint will spawn whatever “attachment” blueprints you want to attach to it then save these spawned actors to the variables.
Then your root can communicate with those “attachments” (via interface messages or casting etc… whatever works for you).

For example your root will call a “fire” function that is inside your cannon actor, then the cannon can do damage depending on what type of cannon it is etc.

You can also make a “base” class for each type of attachment (one for engines, one for cannons, one for rudders) and then just change a few variables each time (like mesh, power, max ammo… whatever) instead of making a bunch of blueprints.

I see, yeah, this makes sense. Let me iterate it to make sure I understand:

  1. Hull blueprint will have few arrays of variables to store configurable components (Engines, Cannons and etc.)
  2. Each array stores actors of a specific component group. For example Engines array can store multiple Engine actors.
    Now we need to somehow spawn components. One way would be to spawn them in a construction graph of the Hull but it means that first I need to somehow pass a complete specification of the components into the Hull, including what kind of mesh should be there (if different engine have different visual appearance for example).
    The other solution would be to spawn a base component Engine, in the world. During spawn provide it’s parameters such as which mesh should be used and what other parameters it should have. Then spawn an empty Hull and attach components to it one after another, storing pointers to them in Hull’s array of component. Something like that?

Yeah pretty much… you can make a function in your root blueprint that takes in some parameters, then spawns and attaches actors during begin play and/or in construction graph (if you need to construct a ship with specific parameters within the editor).

You could also add a few empty “pre-attached” static mesh components to your root blueprint, then during construction/begin play, add any meshes you need to those components, then simply store some variables (like power, ammo etc.) so that your root knows what kind of attachment it has.

So you can do everything from within one blueprint really if you want to, it all depends on the complexity I guess… if every part needs to have some really specific functionality that goes beyond storing a few variables then it makes more sense to have a base blueprint for each type.

Just tested it. Indeed it’s very easy. Made a “ShipFactory” blueprint which spawns both “Hull” and "Engine, then attaches “Engine” actor to the “Hull” and passes pointer to it for local access.
Thank you! I’ll be looking into communication using Interfaces now.