Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by 4illeen · Aug 10, 2011 at 01:26 PM · listdatabaseinventoryitem

Item Database

I've got an item class (with few children for various item types) which holds all the variables such as name, price, weight etc. For my characters' inventory I use a List<Item>. Whenever I let's say find a mushroom inside the forest and pick it up, the mushroom object is destroyed and new Item is added to the list.

My question is - what is the easiest way to create an easy-to-edit database of all the items I have in-game, so I can just call them by name, whenever I need to add new thing to my inventory. I want it all in one place.

Comment
Add comment · Show 2
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 Statement · Aug 10, 2011 at 01:37 PM 0
Share

This question could use a little more clarification. Easy-to-edit through code? What do you mean with "calling them by name"? Do you mean you'd want to do like inventory.AddNew("$$anonymous$$ushroom"); ?

avatar image 4illeen · Aug 10, 2011 at 01:42 PM 0
Share

Easy to edit, so I can just add another item somewhere inside the database without the need of editing other items already there.

inventory.AddNew("$$anonymous$$ushroom"); sounds good

3 Replies

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

Answer by Statement · Aug 10, 2011 at 01:54 PM

Maintain a Dictionary of your prefabs or prototypes.

 class Inventory 
 {
     private List<Item> items = new List<Item>();
     private Dictionary<string, Item> itemPresets = new Dictionary<string, Item>();

     // Presets (Prototypes) are meant to be predefined instances that are later 
     // copied to actual instances for game use. Set these in your init,
     // like hardcoded, or from a file...
     public void SetPreset(string name, Item preset)
     {
         itemPresets[name] = preset;
     }
 
     public Item AddNew(string name)
     {
         Item preset = itemPresets[name]; // Get the preset
 
         Item newItem = new Item(preset); // Create a clone, 
                                          // I opted for a copy constructor...
 
         items.Add(newItem);              // Register to database
 
         return newItem;                  // And return it since you probably
                                          // want to use it immediately.
     }
 }


Hardcoded

 Inventory inv = new Inventory();

 Item preset;

 preset = new Item();
 preset.name = "Apple";
 preset.cost = 25;
 preset.weight = 1;
 inv.SetPreset("Apple", preset);

 preset = new Item();
 preset.name = "Sword";
 preset.cost = 100;
 preset.weight = 5;
 inv.SetPreset("Sword", preset);

 preset = new Item();
 preset.name = "Shield";
 preset.cost = 75;
 preset.weight = 8;
 inv.SetPreset("Shield", preset);

From a text file

 Inventory inv = new Inventory();
 string[] lines = System.File.IO.ReadAllLines("Presets.txt");
 for (int i = 0; i < lines.Length;)
 {
     Item preset = new Item();
     preset.name = lines[i++];
     preset.cost = int.Parse(lines[i++]);
     preset.weight = int.Parse(lines[i++]);
     inv.SetPreset(preset.name, preset);        
 }

Assumed text format (Presets.txt)

 Apple
 25
 1
 Sword
 100
 5
 Shield
 75
 8

Comment
Add comment · Show 4 · 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 4illeen · Aug 10, 2011 at 02:19 PM 0
Share

Ok, after some reading it's clear to me (except for the cloning part - why can't I use the preset?). But still, I don't know how to create the database - either hardcoded or from a file. I'm new to coding.

avatar image Statement · Aug 10, 2011 at 03:02 PM 0
Share

Oh you can use the preset if it doesn't contain any instance specific properties (if they are read only). Let me update the code with an example of how to populate the presets...

avatar image Statement · Aug 10, 2011 at 03:55 PM 0
Share

So yeah, you don't need to clone/copy the presets into new instances if they have no individual data (that is, no two apples are different from each other, ever).

avatar image azmundai · Aug 02, 2013 at 08:15 PM 0
Share

I'm curious is it possible to encrypt this data as you write it to the file and still maintain the simple spreadsheet like formatting?

avatar image
0

Answer by Waz · Aug 10, 2011 at 01:46 PM

Have an array to which you drag a prefab of each item type, and add all the helper functions you need to that class. Use the singleton pattern to access it:

 // Items.js
 var items : Item[]; // Fill out in Inspector
 private var instance : Items;
 function Awake() { instance = this; }
 function Get(name : String) : Item { ... }

Used like this:

 Instantiate(Items.Get("toothbrush"));

Note that if you have quite a few, you'll want to build a Dictionary.<String,Item> at startup.

Comment
Add comment · Show 1 · 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 4illeen · Aug 10, 2011 at 01:58 PM 0
Share

First of all I'm a C# guy, so I don't understand everything you said, but I think we're not talking about the same thing. I don't have prefabs for all the objects yet, also I don't want the items to hold my Item info. GameObject's name may be used to call inventory.AddNew("GO's Name Here") though.

I'd rather like to have a script or a text file, where in one place I can write all the variables name etc down by myself

avatar image
0

Answer by AaronG · Aug 10, 2011 at 03:21 PM

I'm not entirely sure what the issue is. You already have a list containing your items so that IS your database. If you need to find something specifically within it you can either look up the MSDN documentation on Lists or you can do something like this...

 foreach (Item_Class foundItem in Inventory) {
 if (foundItem.ItemName == "Mushroom")
 //DoStuff

}

Comment
Add comment · Show 1 · 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 4illeen · Aug 10, 2011 at 04:35 PM 0
Share

The List is characters current equipment. I needed a library which could hold all items that might be added to the list like a crossbow or a carrot, with all it's stats

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

9 People are following this question.

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

Related Questions

Problem with dropping items from my inventory 0 Answers

How can I store game data outside of scripts.(e.g Total list of inventory items) 2 Answers

Custom Editor Window to Create and Store Item Data 1 Answer

Preventing Items from shifting in a list 1 Answer

Scroll List Problem 1 Answer


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