Trying to better/more deeply understand what's happening

So, i’m rewatching this lecture several times and trying to really get into the code, because I want to have a proper understanding of what’s happening and why everything is set up the way that it it. So, I have a few questions.

For this method, in DragItem

        private IDragDestination<T> GetContainer(PointerEventData eventData)
        {
            if (eventData.pointerEnter)
            {
                var container = eventData.pointerEnter.GetComponentInParent<IDragDestination<T>>();

                return container;
            }
            return null;
        }

Does this not just get the parent container of “this”? In other words, since we have this inventory object attached to the cursor during the unity DragEvent, does “pointer enter” not refer to said game object? If this refers to whatever UI element that the mouse is hovering over, how do we know that it’s going to be getting a UI slot, and not, say, just the inventory canvas and therefore cause a glitch? The only qualifier prior to launching this method is

            if (!EventSystem.current.IsPointerOverGameObject())
            {
                container = parentCanvas.GetComponent<IDragDestination<T>>();
            }

//note, parentCanvas = GetComponentInParent<Canvas>(); so it's just the default UI canvas

so does that not just point to the general canvas for the inventory UI? All it’s looking for is ANY UI element, correct? I’m really confused about how this works. I see that each Inventory Slot has a “Canvas Group” component but a canvas group is not a canvas.

I have a lot of other small questions too, I hope that we go over all of this in more detail. But that’s I guess the main one for now, as the whole thing (in my mind’s eye) doesn’t seem to make sense for how it works, and i really want to know.

Sorry, one more question lol. So for this method in DragItem:

    private void DropItemIntoContainer(IDragDestination<T> destination)
    {
        if (object.ReferenceEquals(destination, source)) return;

        var destinationContainer = destination as IDragContainer<T>;
        var sourceContainer = source as IDragContainer<T>;

        // Swap won't be possible
        if (destinationContainer == null || sourceContainer == null || 
            destinationContainer.GetItem() == null || 
            object.ReferenceEquals(destinationContainer.GetItem(), sourceContainer.GetItem()))
        {
            AttemptSimpleTransfer(destination);
            return;
        }

        AttemptSwap(destinationContainer, sourceContainer);
    }

Why are we attempting to place an item into the new container if is potentially fails the check of “Destination == null”? doesn’t that, like… not work?

If it doesn’t get an IDragDestination then nothing will happen, the item will be automatically returned to where it came from.

Actually, we’re testing to see if it’s an IDragContainer.
An IDragContainer is both an IDragSource and an IDragDestination. It can fail the Container test, but still be an IDragDestination, in which case a simple transfer will do the trick. (It must be an IDragDestination or DropItemIntoContainer would never be called.)

1 Like

Ah, I see. Thank you for answering my questions! For whatever reason I was just having trouble wrapping my head around this lesson… it makes more sense now.

I’ll be the first to admit Sam sorta glossed over this lesson. It took me some time to wrap my head around it too, and I’ve been coding in one language or another since the early 80s

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms