• 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 Tristsin · Mar 09, 2013 at 07:33 AM · javascriptinventory

How to simplify my Equipment method?

Ok. Here it goes.

Well, first off, you equip items pressing a button on a GUI. From there, depending on the button you press, it adds an entry to a hashtable, w$$anonymous$$ch is then checked for a certain key and depending on the key, loads a certain resource(in t$$anonymous$$s case a prefab for the weapon)

private var InventoryManager : InventoryManager; InventoryManager = GetComponent("InventoryManager");

function OnGUI () { if ( GUI.Button(Rect(10,10,50,50),"Equip Platinum Gladius") ) { InventoryManager.equipWeapon.Add("Platinum Gladius", 1);
InventoryManager.hasWeapon = true; InventoryManager.CheckForWeapon(); } }

From that point, it goes through to the InventoryManager Script

(rh = righthand of my character)

var equipWeapon : Hashtable; equipWeapon = new Hashtable();

var hasWeapon : boolean = false; var rh : GameObject;

function CheckForWeapon () { if ( hasWeapon ) { if ( equipWeapon.ContainsKey("Copper Short Sword") ) { Debug.Log("Copper Short Sword"); mesh = Instantiate (Resources.Load("Copper Short Sword") ) as GameObject; mesh.transform.parent = rh.transform; mesh.transform.position = rh.transform.position; } ... // From here it continues on through checking for each entry possible into the Hashtable, currently // it's going through about 20 items but in the end it'll be going through about 160-200 ... ... ...

// Until it reaches the one for the button

if ( equipWeapon.ContainsKey("Platinum Gladius") ) { Debug.Log("Platinum Gladius"); mesh = Instantiate (Resources.Load("Platinum Gladius") ) as GameObject; mesh.transform.parent = rh.transform; mesh.transform.position = rh.transform.position; } }

And then it perform's it's duty.

I know, t$$anonymous$$s may seem like anyt$$anonymous$$ng but a robust system. I'm rather new to the coding of games(I'm typically just an artist). Is there a more simple, or perhaps more efficient method of accomplis$$anonymous$$ng the same goal?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by MartinCA · Mar 09, 2013 at 08:05 AM

First off, t$$anonymous$$s is not terrible - as it works :)

However, I would do 2 t$$anonymous$$ngs to come to mind immediatley:

Generalize your CheckForWeapon method. If you replace the hardcoded strings with a variable you could pretty much shorten your code to several lines and it will be much more robust and future proof:

 function CheckForWeapon ( var weaponID)
 {
     if ( hasWeapon )
     {
         if ( equipWeapon.ContainsKey(weaponID) )
         {
             Debug.Log("Found entry " + weaponID + " in inventory!");
             mesh = Instantiate (Resources.Load(weaponID) ) as GameObject;
             mesh.transform.parent = rh.transform;
             mesh.transform.position = rh.transform.position;
         }

Now you should be able to have any number of N weapons in your inventory and not have to treat them differently.

The next t$$anonymous$$ng is more of a design t$$anonymous$$ng, w$$anonymous$$ch you might want to leave for later - however, in your example there is a close coupling between the inventory and the player character, t$$anonymous$$s makes it unusable in future scenarios (let's say you want to add an inventory to your enemies for loot, or any other use).

To handle t$$anonymous$$s, break the ties between the inventory and the player entities, as they are two distinct entities w$$anonymous$$ch really should not know of one another to operate properly. What it means is the player should be the one equipping and handling items attached to it, the inventory is a rather dumb component only aware of the items it contains and wether or not they exist.

a rough outline of that would be

 Inventory {
     /* Checks if an item exists in the inventory, returns true if does and false otherwise
     bool HasItem( itemID );
 
     /* Checks if an item exists in the inventory, if does it returns a reference to an instantiate copy of the item, otherwise - null. 
     GameObject GetItem( itemID );
 }

 Player {
 
     /* Reference to my inventory */
     Inventory m_inventory;
 
     EquipFromInventory( itemID )
     {
        /* Make sure we have an active inventory */
        if ( m_inventory ) 
        {
              /* Make sure the item exists in the inventory
              if ( m_inventory.HasItem( itemID ) )
              {
                  /* Get the item - we assume inventory handles the instantiation when we request it */
                  GameObject item = m_inventory.GetItem( itemID );
 
                  /* The actual equipping of the item */
                  item.transform.parent = rh.transform;
                  item.transform.position = rh.transform.position;
              }
        }
        else
        {
            Debug.LogWarning(" Trying to access unreferenced inventory! did you lose your satchel? ");
        }
     }
 }

It's put in a C-esque notation, but you should be able to understand the general underlying principles. Now, t$$anonymous$$s is more of a design t$$anonymous$$ng, and there are no absolute rights or wrongs (as you said, your version works), so down the line, whatever it is that works for you - works.

Good luck :)

Comment
Add comment · Show 3 · 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
avatar image Tristsin · Mar 09, 2013 at 08:21 AM 0
Share
avatar image MartinCA · Mar 09, 2013 at 08:22 AM 0
Share
avatar image Tristsin · Mar 09, 2013 at 08:28 AM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[SOLVED]Possible Alternation of Transform.Rotate 1 Answer

Sorting through the same variable attached to several GameObjects from least to greatest, then returning an enum state based on that order 0 Answers

Randomly Generated Objects 1 Answer

Items with Statistics(such as attack damage) that actually effect the character? 2 Answers

Need help with my script 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