in the dictionary we store dockeditemslots. why is dockeditemslot a class and not a struct?
Because we need to be able to easily modify the values within the Dictionary, and structs do not allow this. Here’s a simple test: Change the class to a struct, and you should immediately get these errors:
This is because we’re setting the variables within the slots array directly. Within a Dictionary, you can’t modify structs. By using a class, the indexer becomes a pointer to a class, and you CAN modify that.
As a struct, we would need to create a new struct every time we wanted to modify the contents of the slot.
This is almost exactly the answer I wrote, but then I thought “we do this 'cos it’s easier” seemed like the wrong answer.
With structs, we would have to do this
var slot = dockedItems[index];
slot.number += number;
dockedItems[index] = slot;
which is not as easy as
dockedItems[index].number += number;
You get the same issues when trying to change the x, y, or z of transform.position
, for instance
great thnx!
This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.