简体   繁体   中英

Primitive inventory data

I have been building a very basic inventory for a RPG game I am attempting as a little side project.

The inventory works fine, the only real issue is, once the player fills the bag then the script will keep trying to pick up more stuff.

Another issue I have is revamping the code so it works more elegantly.

Here is the full script, it is very long

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class inventoryRedo : MonoBehaviour 
{
    public GameObject fullWarning;
    public Sprite coalSprite;
    public Sprite manaPotionSprite;
    public Sprite healthPotionSprite;
    public Sprite shrimpSprite;

    private bool isPickingUp = false;

    public Button[] itemSlots;
    private bool isFull = false;
    private int itemNum = 0;


void Update()
{

    if (playerPickUp.hasPickedUpDroppedCoal == true)
    {
        itemNum = 1;
        if (itemNum == 1)
        {
            CheckForFreeSlot();
            playerPickUp.hasPickedUpDroppedCoal = false;

        }
    }
   if (playerPickUp.hasPickedUpHealthPot == true)
   {
        itemNum = 2;
        if (itemNum == 2)
        {
            CheckForFreeSlot();
            playerPickUp.hasPickedUpHealthPot = false;
        }
   }
    if (playerPickUp.hasPickedUpManaPot == true)
    {
        itemNum = 3;
        if (itemNum == 3)
        {
            CheckForFreeSlot();
            playerPickUp.hasPickedUpManaPot = false;
        }
    }
}


void CheckForFreeSlot()
{
    if (isFull == false)
    {
        if (itemSlots[0].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[0].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[0].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[0].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[0].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[0].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[0].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }

        }
        else if (itemSlots[1].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[1].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[1].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[1].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[1].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[1].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[1].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[2].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[2].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[2].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[2].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[2].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[2].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[2].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[3].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[3].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[3].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[3].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[3].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[3].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[3].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[4].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[4].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[4].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[4].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[4].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[4].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[4].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[5].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[5].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[5].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[5].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[5].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[5].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[5].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[6].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[6].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[6].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[6].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[6].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[6].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[6].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[7].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[7].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[7].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[7].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[7].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[7].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[7].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[8].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[8].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[8].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[8].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[8].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[8].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[8].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[9].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[9].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[9].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[9].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[9].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[9].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[9].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[10].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[10].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[10].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[10].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[10].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[10].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[10].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[11].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[11].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[11].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[11].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[11].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[11].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[11].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[12].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[12].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[12].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[12].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[12].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[12].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[12].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[13].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[13].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[13].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[13].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[13].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[13].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[13].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[14].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[14].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[14].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[14].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[14].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[14].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[14].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[15].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[15].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[15].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[15].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[15].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[15].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[15].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }


        else if (itemSlots[16].transform.tag == "slotFree")
        {
            if (itemNum == 1)
            {
                itemSlots[16].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[16].gameObject.GetComponent<Image>().sprite = coalSprite;
                itemNum = 0;
            }
            else if (itemNum == 2)
            {
                itemSlots[16].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[16].gameObject.GetComponent<Image>().sprite = healthPotionSprite;
                itemNum = 0;
            }
            else if (itemNum == 3)
            {
                itemSlots[16].transform.tag = "slotTaken";
                Sprite slotOne = itemSlots[16].gameObject.GetComponent<Image>().sprite = manaPotionSprite;
                itemNum = 0;
            }
        }else
        {
            print("No Room");
        }
    }

}

}

The first issue is that the else at the bottom does not work.

other than that I think from this you can see pretty quickly that this will get very long the more items the game has.

I have been trying to think of a way to water this down even more, sort of having a script with its own function for each item slot I could not think of one.

I was thinking of maybe, having a single small function that loops through an array that checks what the first item is that has a tag of "freeSlot" and putting the item in that slot and so on.

But the main issue really is that the else and the bottom of the script does not fire.

I would suggest a little redesign. First i would make a class for slots. I would prefer a data object class (but it could also be MonoBehaviour):

public class CSlot
{
    public GameObject item;
    ...

}

Then make an array with choosen size of it:

public class inventoryRedo : MonoBehaviour
{ 
    public CSlot[] inventory;

    private int FindIndexOfNextFreeSlot()
    {
        int nIndex = -1;
        for (int i=0; i < inventory.Length; i++)
        {
            if (inventory[i].item == null)
            {
                nIndex = i;
            }
        }
        return nIndex;
    }

    private void PutIntoInventory(int nIndex, GameObject gameObject)
    {
        inventory[nIndex].item == gameObject;
    }
}

Then i would do some changed to playerPickUp , so that you can do:

if (playerPickUp.PickedUpGameObject != null)
{
    int nIndex = FindIndexOfNextFreeSlot();
    if (nIndex > -1)
    {
        PutIntoInventory(nIndex, playerPickUp.PickedUpGameObject);
    }
}

With this, you may not have to repeat yourself that often. Hope you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM