Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Tutorial] Creating a slot-based RPG inventory system in Unity and UNET

Discussion in 'Scripting' started by JimWonderDog, Sep 12, 2017.

  1. JimWonderDog

    JimWonderDog

    Joined:
    Sep 12, 2017
    Posts:
    4
    Hi there, I wanted to share the first part of a tutorial I wrote about creating a slot-based inventory system in Unity that would sync-up in multiplayer.

    [mod edit : old link removed]

    If there's enough interest I'll post the second part soon.

    Tell me what you think, thanks :)
     
    Last edited by a moderator: Jan 11, 2019
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I'm curious why you used a Dictionary to manage your inventory instead of just a List where the index in the List is also the slot?
     
  3. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    I was wondering the same thing. With a list, you wouldn't need code like this:

    Code (csharp):
    1.  
    2. public int FindEmptySlot()
    3. {
    4.    for (int i = 0; i < slotAmount; i++)
    5.    {
    6.       if (!items.ContainsKey(i))
    7.       {
    8.          return i;
    9.       }
    10.    }
    11.    return -1;
    12. }
    13.  
    Plus, iterating through a dictionary is slower than iterating through a list.

    Now (hopefully) that shouldn't matter too much since this is UI code, ideally these iterators should only happen when something actually changes or updates. However... if it's more efficient using a list, why not use a list?

    Now, if you were to use a doll menu instead with specific equipment slots (left arm, right arm, torso, etc.) then it'd make sense.
     
  4. JimWonderDog

    JimWonderDog

    Joined:
    Sep 12, 2017
    Posts:
    4
    Good question, guys. As it is now there really is no reason to use a dictionary, as you pointed out. The list is faster and has less overhead, as far as I know. But the reason I used a dictionary is beacuse in the next part of the tutorial we will add the ability of an item to occupy more than one slot, and the key in the dictionary will change to an InventoryPos structure.

    I'll add that info in the tutorial, thanks for pointing that out!
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Even then it wouldn't; unless your intention was to classify loot by those slots (maybe).

    Not sure that this is the best way to handle that still but I'll withhold further critique until you show the actual code. Good subject to tackle though!
     
  6. Nrost

    Nrost

    Joined:
    Aug 23, 2013
    Posts:
    6
    It's very good.Thank you.
     
  7. Deleted User

    Deleted User

    Guest

    I think the tutorial needs to be broken down more because you just slapped some code onto the page and I don't really understand it. I'm sort of a noob and this really looks cool but I want to be able to understand it. What would be cool to is if you made videos explaining it too.
     
  8. Delforce

    Delforce

    Joined:
    Jun 3, 2015
    Posts:
    23
    My Virus scanner blocked your tutorial link as a malicious virus attack, I've reported this post in the meantime in order for someone at Unity to take a look.
     
    sivrikaya likes this.
  9. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,002
    I removed the link. Old post, op doesn’t appear to have the domain anymore. Also hasn’t been online here for over a year.