Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Epitaph778 · Oct 07, 2014 at 04:08 AM · multiplehitboxhphealth

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:

  1. Damage was received

  2. The damage amount

  3. 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:

  1. 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.

  2. When a child reports they were hit, uses the child's enum as a key and changes the current health accordingly.

  3. 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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Landern · Oct 07, 2014 at 04:31 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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]);
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Health Bar Division like in MOBA 1 Answer

Player Health Goes Down Too Fast/Too Much 0 Answers

Atack and HP 'universal' scripts, begginer need guidance. 1 Answer

Adding fall damage with the default FPSWalker 1 Answer

Finding a certain variable is failing 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges