- Home /
Best way to organize and access several distinct health values in one object
The Situation
Here's the overall situation. I have a tank object with several hitboxes that are childed to a "hitboxmanager" object. Each hitbox uses an enum identifier to match it to it's respective part (wheels, body, turret, barrel, etc).
When one of these hitboxes are struck, they report to the hitbox manager:
Damage was received
The damage amount
The part that was hit (the one the individual hitbox is responsible for)
The Idea
Now that's all well and good. But I am at a loss of how to store the data in the hitbox manager and access it reasonably.
The hitbox manager should, in the ideal situation:
At Start(), use foreach to create an entry for each distinct enum in the children (may be more than one barrel hitbox, etc) along with an associated float for maximum health, and another float for current health.
When a child reports they were hit, uses the child's enum as a key and changes the current health accordingly.
Sum current health and maximum health of all enum types (as in, the total current health of the object) for a percentage health display on the gui.
The Solution(???)
Now it sounds the best method would be to use a dictionary, but I've read that dictionaries don't show up on the inspector so I can't monitor it for debugging purposes. And I've looked into arrays and lists but those seem to be messy and restrictive (have to search each for the proper value instead of using a key)
I could just declare a boatload of public variables and write a massive if() statement to determine which to edit, but that's messy and would be a problem with rapid hits and multiple objects in the scene.
The dictionary issue is due to it not being serializable out of the box. You would need to implement your own dictionary that is serializable and perhaps even it's own render for the inspector. You will find examples on the net of a serializable dictionary that implements the interface IXmlSerializable and the methods needed to write the key value pairs. In unity's case - since 4.5 - you can do the a similar type of thing with ISerializationCallbackReceiver and methods OnAfterDeserialize and OnBeforeDeserialize. The linked documentation also has examples of using List's for $$anonymous$$ey/Value pairs, your mileage may vary when it comes to speed.
Answer by MakeCodeNow · Oct 08, 2014 at 04:50 AM
Enums are also integers, which are perfect for indexing into arrays (just cast to int). If you store you part definitions and part instances in simple array whose length is the size of your enums, then it'll "just work". Here's the basic idea:
enum PartSlots
{
Chassis, // the first item in an enum is 0
Turret, // then each item is one higher than the previous, so this is 1
LeftWheel,
RightWheel,
// ... etc
NumParts // put this special enum last
}
public PartInstance[] partInstances = new PartInstance[NumParts];
public PartDefinition[] partDefinition = new PartDefinition[NumParts];
void Start()
{
for(int i = 0; i < NumParts; ++i)
{
if(partDefinitions[i] != null)
{
partInstances[i] = CreatePartFromDefinition(partDefinitions[i]);
}
}
}