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

Advise for storing items in a simple way

Discussion in 'Scripting' started by VirusPunk, Nov 8, 2015.

  1. VirusPunk

    VirusPunk

    Joined:
    Aug 21, 2015
    Posts:
    25
    I am developing a Resident Evil clone (an exact clone of the original PS1 games - for non profit and solely for myself and the RE community), while everything has gone smoothly so far, I'm a little stumped on how I should store the items for the inventory. My current goal is to have two preset items placed in the inventory (weapons in which I can equip), there are a total of 8 item slots. As of right now, I can navigate the inventory and move from slot to slot using the d-pad and the GUI works accordingly. However, in code I want to be able to just store two preset items for the time being. The knife in slot 0, the handgun in slot 1, while the rest of the other slots are empty. What would be the simplest way to go about this? The weapon equips are already coded, I can temporarily equip the knife and handgun using F3 and F5 and it'll show up under the "equipped" slot in my hardcoded inventory GUI and the weapon is usable in game.

    The slots are governed by a simple integer; slotArray. The dpad was coded in which it works like a fixed button press, where moving right from slot 0 moves it to slot 1, then from slot 1 downward moves it to 3, etc.

    If I'm on slot 0 (where I want the knife to be placed), I want to be able to hit X/Square on the controller to open up the Equip tab. But it has to be stored in some sort of array that connects with the slotArray ID I have setup, like a list of slots where you can add the item ID via the inspector, and that item will show up accordingly. I'm not asking anyone to do the code for me, just advise on a simple way to go about this. Thank you.
     
    Last edited: Nov 8, 2015
  2. VirusPunk

    VirusPunk

    Joined:
    Aug 21, 2015
    Posts:
    25
    I figured it out. It's crude and messy but using booleans is the best/easiest way as usual at least as a temporary solution. You create an integer for each of the 8 slots where say 0-X each represents an unique item ID, then simply check each ID and assign the booleans true/false accordingly. Here's an example for those wanting to do the same thing or similar.

    Code (CSharp):
    1.         if (slot0Item == 0 || slot1Item == 0 || slot2Item == 0 || slot3Item == 0
    2.              || slot4Item == 0 || slot5Item == 0 || slot6Item == 0 || slot7Item == 0)
    3.         {
    4.             isInvEmpty = true; isInvKnife = false; isInvHandgun = false; isInvFAS = false;
    5.         }
    Then for the actual item select/equip/action/whatever code you do this:

    Code (CSharp):
    1.         if (isInInventory && inSlots && slotArray == 0 && slot0Item == 0 &&
    2.             Input.GetKeyDown(inputs[4]))
    3.         {
    4.             // Null
    5.             Debug.Log("Nothing there.");
    6.         }
    Which checks to make sure the selection is slotArray 0 (which I stated above), slot0Item 0 is 0 (which in my case represents nothing/empty) and the input to do something with.

    Obviously having a whole mess of items will mean alot of messy duplicate code, but it's great if you only want to test a few items for now. Although, if anyone has any better ideas feel free to speak up.