• 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, which is then checked for a certain key and depending on the key, loads a certain resource(in this 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, this may seem like anything 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 accomplishing 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, this is not terrible - as it works :)

However, I would do 2 things 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 thing is more of a design thing, which you might want to leave for later - however, in your example there is a close coupling between the inventory and the player character, this 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 this, break the ties between the inventory and the player entities, as they are two distinct entities which 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, this is more of a design thing, 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

That first part is brilliant! Thanks. Saves me so many lines of copy and pasting and tinkering. Can't believe I overlooked that.

From my general plans for my game, there aren't really any scenarios where I might need what you're saying below, but I might do it just as a future-proofing method. But, one thing confuses me with it. What is itemID in that scenario?

avatar image MartinCA · Mar 09, 2013 at 08:22 AM 0
Share

itemID would be any way to identify your item, should have kept it as weaponID for simplicity. Just a string identifier for your item :)

avatar image Tristsin · Mar 09, 2013 at 08:28 AM 0
Share

Ah! I thought maybe you were suggesting creating sort of a database for my items and assigning an ID to each! It's more clear now, thank you for your help!

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

[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

Need help with my script 0 Answers

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges