0
\$\begingroup\$
void AddItem(Item item)
{
    int indexOfDuplicateItem = System.Array.IndexOf(itemsInInventory, item);

    if (indexOfDuplicateItem > -1)
    {
        quantityText.text = (stackSize[indexOfDuplicateItem] += 1).ToString(); // Problem occurs here
    }
    else
    {
        for (int i = 0; i < numSlots; i++)
        {
            if (itemsInInventory[i] == null)
            {
                itemsInInventory[i] = item;
                GameObject itemImage = Instantiate(itemImagePrefab, slots[i].transform);
                itemImage.GetComponent<Image>().sprite = item.itemSprite;
                quantityText = slots[i].GetComponentInChildren<Text>();
                stackSize[i] = item.itemQuantity;
                quantityText.text = stackSize[i].ToString();
                return;
            }
        }
    }

The indicated line behaves differently if I write quantityText.text = (stackSize[indexOfDuplicateItem]++).ToString();

I'm a relative beginner to both C# and Unity. Can someone explain why the behaviour of (stackSize[indexOfDuplicateItem]++).ToString() is different from (stackSize[indexOfDuplicateItem] += 1).ToString()?

using += 1 to increment behaves correctly, but ++ ends up being the correct answer - 1.

\$\endgroup\$
  • 2
    \$\begingroup\$ For syntax questions, don't neglect to google it / search Stack Overflow first - they have good coverage of common questions about language fundamentals. \$\endgroup\$ – DMGregory Oct 12 '17 at 22:42
  • \$\begingroup\$ Indeed, this is a very basic issue with programming syntax (not that it is a bad question for a programming Q&A, but it has nothing to do with game dev, itself). \$\endgroup\$ – Gnemlock Oct 15 '17 at 3:10
3
\$\begingroup\$

This is expected behaviour given the usual syntax for ++, which has been around since the language B (circa 1969):

  • variable++ is a "post-increment" operator, ie. "Return to me the current value of the variable, then increment that variable afterward"

    So an expression containing this construct gets evaluated with the old value of the variable, before it was incremented.

    This is useful in a loop where you want to ensure the next use of a variable is incremented, eg...

    int p = 0; for(y = 0; y < height; y++) for(x = 0; x < width; x++) pixel_buffer[p++] = GetPixelColor(x, y);

    This loop will write to pixel_buffer[0] first, then each subsequent index in turn.

  • ++variable is a "pre-increment" operator, ie. "Increment the value of the variable before returning it, then return to me its new value"

  • variable += 1 is an (augmented) assignment expression. When it's evaluated, the whole expression gets run (incrementing the variable), then the resulting value of the variable is treated as its return value. So it behaves similarly to ++variable

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

This is most likely because of how the ++ operator works. The ++ operator works in two steps:

  1. Returns the value if the variable
  2. Increments the variable

This means that when you do stackSize[indexOfDuplicateItem]++ it first returns stackSize[indexOfDuplicateItem] THEN increases the value by 1. It's the same as writing this:

quantityText.text = (stackSize[indexOfDuplicateItem]).ToString();
stackSize[indexOfDuplicateItem] += 1

This is a small test case:

>>> int i = 5;
>>> Console.Write(i)
5
>>> Console.Write(i++)
5 // STILL 5!
>>> Console.Write(i)
6 // Now 6!

To circumvent this you could put the ++ before like so: ++stackSize[indexOfDuplicateItem]. This will increment the value by 1 and then return the value. This should give the desired result.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Consider the following:

int i = 0
strings[] myArray = ["a","b","c","d"];

...

Debug.Log(myArray[i++]); // Evaluates i first, which is zero -> log = "a"
                         // Then add 1 to i

Debug.Log(myArray[i++]); // Evaluates i first, which is 1 -> log =  "b"
                         // Then add 1 to i

Now we can also do ++ on the left side

int i = 0
strings[] myArray = ["a","b","c","d"];

...

Debug.Log(myArray[++i]); // add one to i first, then evaluate i -> log =  "b"


Debug.Log(myArray[++i]); // add one to i first, then evaluate i -> log =  "c"
| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.