How would you implement a stack of selectables?

I am rather new to Unity and my development experience is mostly in boring corporate software written in C.

Suppose that you have a game in which you have selectable units, each unit having multiple selectable weapons and some weapons have selectable subsystem. So player can say “I want to aim the grenade launcher part of the right arm of this mecha”. Units are not the only selectables, a player may select a certain factory and then alloys division within factory to set the specific division’s funding priority to above average.

In turn based game course on there are selectable units and each unit holds components for selectable actions while actions point back to unit. With unit selected you see the buttons for available actions and the UnitActionSystem which handles most of the input knows what is selected and acts accordingly.

How would you generalize it to UnitActionSystem holding a stack of selectables and letting top of the stack handle the input potentially adding more handlers to the stack in the process? What are the possible trade-offs?

The selection can be done with a left click and deselection with a right click for example.

No less important than game development. Part of my regular job involves maintaining a database with a Visual Basic back end (more boring, hehe).

What you’re thinking of is a fairly complex system, but not impossible. The hardest part may be dealing with the visual damage/destruction on parts of the robot, as most games models are one complete model. This could instead be simulated using some sort of UI display of which body parts are in what condition.

I would likely start with a UnitBodyPart class. These could be added as GameObjects under the unit, or even be added to the Unit’s skeleton in corresponding points…

public class UnitBodyPart : MonoBehaviour
    [SerializeField] string displayName;
    public string DisplayName => displayName;

I would put a Heath component on the UnitBodyPart GameObject as well. If the Health component is dead, then the body part is dead.

Getting a collection of body parts is a matter of using GetComponentsInChildren

UnitBodyPart[] bodyParts = GetComponentsInChildren<BodyParts>();

Setting up a UI bar to select the body part would use this bodyparts collection to add buttons to a container just like in the Action bar. Updating the buttons, make any button whose corresponding body part’s `GetComponent() is alive interactable, other wise not interactable.

That’s just a start, of course, and it may not even be the exact direction you’re looking for. From there, you need to set your weapons up to head for that body part (that’s where putting them on the skeleton helps, so you can see the weapon hitting the arm, for example).

Weapons are ultimately just actions… An interesting idea might be to associate an Action with each body part, so if the left arm is destroyed, then the Negasonic Inverting Proton emitter weapon won’t be available as an action… Lots to think on, and almost could be a course in itself.

Privacy & Terms