Jacob Pipers

Game Developer

Create the Base Slot Functionality

Goals

  • Create a prefab for item slots
  • Create scripts needed to show item details

Personal

Jacob Pipers

Technologies, Tools, and Resources

Task Undertaken

Create a prefab called slot Making Sure that it is a Rect Transform and attach a new script called slot script. the next step is to add four children to the SlotPrefab these act as quadrants. On each one Make sure to anchor to each corner and to the middle Add another script that will be made to the quad called slot sector script. These quads help the highlighting look smoother.

In the SlotScript.cs add the following

public InvenGridManager invMan;
public SlotClass slotInfo;

The InventoreyClass Script is another script we will make and it will contain a struct called SlotClass.

in the Start function add this:

private void Start()
{
//   text.text = gridPos.x + "," + gridPos.y;
var slotsQuads = GetComponentsInChildren<SlotSectorScript>();
  foreach(var slotQuad in slotsQuads)
{
    slotQuad.invenGridManager = invMan;
}
}

The text is for debugging, the current slot position.

Create a new script and add the following code. (Note I put it in a script called inventory Class but it can sit in any script.)

public struct SlotClass
{

    public IntVector2 gridPos;

    public GameObject storedItemObject;
    public IntVector2 storedItemSize;
    public IntVector2 storedItemStartPos;
    public ItemClass storedItemClass;
    public ItemType allowedItems;
    public bool isOccupied;
}

This class holds the state and information of each slot.

Next, add the code for the SlotSector Script this handles the OnMouseEventEnter and exit events for each slot.

public void OnPointerEnter(PointerEventData eventData)
    {

      //  print(eventData.ToString());

        sectorScript = this;
        if (invenGridManager == null)
            print("No Inv Manager");
        else
        {
            invenGridManager.highlightedSlot = slotParent;


            invenGridManager.isOverUI = true;
            PosOffset();
            if (ItemScript.selectedItem != null)
            {
                invenGridManager.RefrechColor(true, invenGridManager.highlightedSlot);
            }
            if (parentSlotScript.slotInfo.storedItemObject != null && ItemScript.selectedItem == null)
            {
                if (parentSlotScript.slotInfo.gridPos.x > 1000)
                    invenGridManager.ColorChange(SlotColorHighlights.Blue, parentSlotScript.slotInfo.storedItemSize, parentSlotScript.slotInfo.storedItemStartPos);
                else
                    invenGridManager.ColorChangeLoop(SlotColorHighlights.Blue, parentSlotScript.slotInfo.storedItemSize, parentSlotScript.slotInfo.storedItemStartPos);        
            }
            if (parentSlotScript.slotInfo.storedItemObject != null && invenGridManager.GetComponent<CanvasGroup>().interactable)
            {
                ShowToolTip.DoesShowOverLay(parentSlotScript.slotInfo.storedItemClass, true);
            }
        }

    }
 public void OnPointerExit(PointerEventData eventData)
    {
        if (invenGridManager == null)
            print("No INV MAN");
        else
        {
            sectorScript = null;
            invenGridManager.isOverUI = false;
            invenGridManager.highlightedSlot = null;
            //overlayScript.UpdateOverlay(null);
            //overlayScript.gameObject.GetComponent<CanvasGroup>().alpha = 0;
            ShowToolTip.DoesShowOverLay(null, false);

            if (ItemScript.selectedItem != null)
            {
                invenGridManager.RefrechColor(false, invenGridManager.highlightedSlot);
            }
            posOffset = IntVector2.Zero;
            if (parentSlotScript.slotInfo.storedItemObject != null && ItemScript.selectedItem == null)
            {
                if (parentSlotScript.slotInfo.gridPos.x > 1000)
                    invenGridManager.ColorChange(SlotColorHighlights.Blue2, parentSlotScript.slotInfo.storedItemSize, parentSlotScript.slotInfo.storedItemStartPos);
                else
                    invenGridManager.ColorChangeLoop(SlotColorHighlights.Blue2, parentSlotScript.slotInfo.storedItemSize, parentSlotScript.slotInfo.storedItemStartPos);
            }
        }
    }

This is the base setup for the Slot Scripts Next will look at the Tooltip and Getting the Information out of the slot.

Justification

After doing much research I found this Resource, Looking at the solutions that the resource provided had many of the solutions that I was looking at I decided to base my code upon it to save time.

Next Post

Previous Post

Leave a Reply

© 2020 Jacob Pipers

Theme by Anders Norén