Search Unity

Help with script planning

Discussion in 'Scripting' started by DarkEcho, Oct 26, 2014.

  1. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    Trying to make a working inventory/item system however i ran into problems...
    I have made it so the inventory (List) only accepts the item script however dosnt accept the usage script. Also when the item is deleted and stored into the inventory the gameobject is deleted along with the useage script.

    The plans are on the below screenshot...
    plan.png
    Also every item usage script is named differently per type of item (bag, weapon, consumable etc)

    How do professionals do this?
     
    MichaelABC likes this.
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    Because things like items often have multiple visual representations. Such as a GameObject for it in the world, where as another GameObject for how it looks when you pull up the inventory screen. What not and all.

    Personally, I have a class that represents the inventory that is NOT a MonoBehaviour script. And I have scripts that can handle said item class.
     
  3. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    How does everyone else do it? Im certain I have the inventory and Item structure sorted as I done some researth and from the looks of it everyone does it including the professianals, but now im stuck as to how im going to keep the item usage scripts intact when the gameobject is deleted and recreated...
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    You can't keep the script in tact when you delete the GameObject. The script is part of the GameObject.

    I'm betting people who represent it solely as a script on a GameObject probably don't destroy the GameObject and just disable it.

    Either option though is fine. Script and disable. Or have it represented as a completely separate class.
     
  5. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    Thats another issue i have, if i keep the game object active (in the hundreds) i think it will take up more resources and slow the game....

    Has anyone else got any other ideas on how the developers do this, i have absolutely no clue...
     
  6. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    I would store the actual data of the Item in a Scriptable object. Also, depending on what's inside your "usage" script, I would also store that as a ScriptableObject and have the item store a reference to how it's used.

    Then wherever in your game you pick up something. Write a component which takes a reference to 1 of your Item scriptable objects and adds it to your inventory. Then removing it from the inventory is as simple as just removing it from your list, without any gameobject deletions/disabling.

    http://docs.unity3d.com/ScriptReference/ScriptableObject.html

    This will allow you to create all your different item types within the project view. You can modify all parameters of a scriptable object the same as if it was a monobehaviour within the scene. Only it's now an asset stored in the project.
     
  7. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    Uhhh....this is new to me, from what I read this will fix the data usage correct?
    Also this looks very complex to me to understand, is there a video tutorial specified to this ScriptableObject?

    I seem to have arrived at the complicated part of unity...
     
  8. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    Anyone else?

    Im REALLY stuck, ive looked around and im still not 100% sure.

    I feel really lost and im losing hope...
     
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    You don't really need ScriptableObject. It really just makes it useful to create assets. If you want that, you'd use the ScriptableObject.

    But yes, it's the more advanced part of unity.

    I'm telling you. Just write a class (that does NOT inherit from MonoBehaviour).
     
  10. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    Ok I still feel we are going in the wrong direction, so lets start over...

    No doubt everyone's most likely losing patience with me, but hey...

    How, do developers structure, their inventory's and item/s (scripts)?
    Is my image correct as to how they do this?
     
  11. b1gry4n

    b1gry4n

    Joined:
    Sep 11, 2013
    Posts:
    146
    For systems I have created related to inventory management...

    Item class: I store information about the item including its "in-game object" or, the 3d model. Just like in your drawing, but most importantly it contains a unique ID. My items also contain information about use effects (if food/water/etc) as well as what type of item it is (consumable/weapon/etc).

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [System.Serializable]
    4. public class GameItem{
    5.  
    6.     public int ID;
    7.     public string itemName;
    8. public string description;
    9. //Etc
    10. }
    Database: A list of Items defining all the unique variables I created in the Item class. Under the "in game object" variable I include a path to the resources folder so I can find its in-game object. My database also contains a search function so I can type in a unique ID and it will return the item from the database that corresponds. That way I can access everything about it. I also define what loot table(seperate lists) I want to add the specific item to and its "rarity"

    Code (CSharp):
    1. GameItem sword= new GameItem ();
    2.         sword.ID = 1;
    3.         sword.itemName = "Sword";
    4.         sword.description= "Cut stuff";
    5.  
    6. GameItem club= new GameItem ();
    7.         club.ID = 2;
    8.         club.itemName = "Club";
    9.         club.description= "Hit stuff";
    Inventory: Heavily built and managed through UI. Moving between inventory slots, using items, equipping items. 2 main scripts control this system, InventorySlot(contains information about the item it holds) and the InventoryManager(manages the inventory slots). Highly recommend using Lists: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries

    Item Spawner: Since the items have already been added to lists in the database I can simply pick what type of loot table I want to spawn an item from, randomly chosen or specified. When the item is found by the database script It comes with a reference to the in game object as well as all information about the item. When I spawn the in game object from this script I pass along the item.

    Code (CSharp):
    1. public GameItem FindItem(int ID) {
    2.         for(int i = 0; i < items.Count; i++) { //items is a List<GameItem> that is located in the database
    3.             if(items[i].ID == ID){
    4.                 return items[i];
    5.             }
    6.         }
    7.         return null;
    8.     }
    In-Game Object: When the object is in the world another script is controlling how the player interacts with the object, what attributes should be attached to it . if its a weapon it will contain damage information, when the player equips the weapon a "PlayerInput" script knows where to look to apply those damage outputs. Picking up objects is simple. Since this object knows what it is, it can add the item passed to it from the item spawner to the player's inventory and destroy itself.

    That is the general gist of it, hopefully that gives a little bit more info

    EDIT*:
    I forgot I made a post on Unity Questions a while ago that addressed a similar question:
    http://answers.unity3d.com/questions/557290/how-to-start-an-inventory-system.html
     
    Last edited: Nov 3, 2014
  12. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    I tried doing what you say, an inventory drag / drop system to equip a character. (Rings, amulets, weapons, armor, shield, etc ...)

    So, Item class has a portion of code related with inventory(GUI) and another one with the GameObject(Usage).

    The part related to the GameObject you call it 'Item Usage' (Event script). In my opinion, in practice, it is impossible to be a ScriptableObject, one event script (MonoBehaviour) or only object class (custom class instance).

    'Item Usage' must be a prefab (GameObject with event script usage, Transform, Animation, AudioSource components, etc...) because we must indicate the position and character skeleton bone to which it is parented, and some items are formed by various GameObjects.

    The Item class, it would be to be a ScriptableObject, asset type, to be configured from the Editor. For example:

    Code (CSharp):
    1. public class Item : ScriptableObject{
    2.  
    3.     //Textures
    4.     public Texture2D guiNormal;
    5.     public Texture2D guiOn;
    6.     public Texture2D guiDrag;
    7.     public Texture2D guiDropYes;
    8.     public Texture2D guiDropNo;
    9.     //...
    10.  
    11.     //Information
    12.     public string name;
    13.     public string description;
    14.     public string tooltip;
    15.     public int price;
    16.     public int weight;
    17.     //...
    18.  
    19.     //Usage
    20.     public GameObject prefab;
    21.     [HideInInspector]
    22.     public GameObject instance; //It's instantiated?
    23.  
    24. }
    Project example: Inventory drag&drop

     
    Last edited: Nov 4, 2014