- Home /
Creating item-packages with properties
I actually feel rather stupid asking this, but here goes.
I'm trying to create a whole array of items to be used in my game, but all items have some base properties. I want create one script with the entire list of items. Now comes the hard part: I want to be able to call in this script for a particular item and its property, much like asking gameobject.transform.position.
I realise this sounds vague, so as example: I want to find an item called "Turnip" in this list, and I want to add it to my inventory-screen. So I need to ask from the list the Turnip.associatedIcon, Turnip.SellPrice, Turnip.QuantityInInventory, etc etc.
How would I best go about this?
Answer by Eric5h5 · Dec 31, 2010 at 09:56 PM
Use your own class:
class Item {
var associatedIcon : Texture2D;
var sellPrice : int;
var quantityInInventory : int;
}
Then make a Dictionary where the key is a string:
import System.Collections.Generic;
function Start () { var itemList = new Dictionary.<String, Item>(); itemList["Turnip"] = new Item(); itemList["Turnip"].sellPrice = 5; }
Use a constructor in the class to make it easier to work with.
Alternately, use a standard fixed array (say there are 3 different items):
var itemList = new Item[3];
Set up an enum with the names:
enum ItemName {Turnip, Apple, Bread}
Then you can refer to the entries using the enum:
itemList[ItemName.Turnip] = new Item();
itemList[ItemName.Turnip].sellPrice = 5;
Interesting, so whenever you want to access an item, you would use itemList["Turnip"]. Can you do stuff like itemList["Turnip"].transform.position = Vector3(0,0,0)? Finally, how would you destroy an item in the list? itemList["Turnip"].Destroy?
I like your answer :)
If you just want a position, make a Vector3 entry in the Item class (`var position : Vector3`). If you want it to have an actual GameObject, add that (`var gameObject : GameObject`). To get rid of an entry when using a Dictionary, set it to null (`itemList["Turnip"] = null`).
So if my class has an GameObject in it, I could do itemList["Obj"] = new Obj(); and that would create a new GameObject with the defined variables in it's class. Can you pass values to the newObj when creating? Like itemList["Obj"] = new Obj(x,y,anythingelse);
Really helpful, thanks.
Yes, as I mentioned you can use a constructor in the class definition.
Thank you all for the superfast answers! Just one question regarding this answer: I'm very new to coding, and I was not quite sure how to create various Items in one script?
Answer by Socapex · Dec 31, 2010 at 09:37 PM
I guess the best way is to launch a function when you are looking for an object.
Assuming Javascript and that 'name = "Turnip"'
function findObject(name : string){
for(var obj in ObjectArray){
if(obj.name == name){
print("doStuffWithYourTurnip");
print(obj.sellPrice);
}
else{
print("You do not have a " + name + "in your inventory");
}
}
}
Does this help?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Displaying/getting elements of a list in a certain order 3 Answers
Using List.Count on Unity iPhone 2 Answers