- Home /
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?
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 :)
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?
itemID would be any way to identify your item, should have kept it as weaponID for simplicity. Just a string identifier for your item :)
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
Follow this Question
Related Questions
Items with Statistics(such as attack damage) that actually effect the character? 2 Answers
Need help with my script 0 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Finding if a specific key contains a specific value in a Hashtable? 1 Answer