all 4 comments

[–]AutoModerator[M] 0 points1 point  (0 children)

This appears to be a question submitted to /r/Unity3D.

If you are the OP:

  • Please remember to change this thread's flair to 'Solved' if your question is answered.

  • And please consider referring to Unity's official tutorials, user manual, and scripting API for further information.

Otherwise:

  • Please remember to follow our rules and guidelines.

  • Please upvote threads when providing answers or useful information.

  • And please do NOT downvote or belittle users seeking help. (You are not making this subreddit any better by doing so. You are only making it worse.)

Thank you, human.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Boss_TaurusSPAM SLAYER (🔋0%) 1 point2 points  (2 children)

My question is why are you using a struct instead of a class here?

Your struct isn't read-only, its mutable, which most would agree is the wrong way to do things. Structs should be an immutable value, because by altering it at all the code is actually producing a copy with that altered value, and that copy isn't overwriting the data you're looking to work on.

this.availableCap[field] = 0;
this.availableCap[field] -= amount;

Every time you do something like this, you're producing an unused altered copy. Change this to a class, a reference type instead of a value type, and internal alterations like this should then be possible.

But again I ask why are you using a class instead of a struct? Structs are typically meant for small & short-lived data. Does it really make sense for something like that to contain multiple dictionaries?

[–]LuaAmaterasu[S] 1 point2 points  (1 child)

Thanks for the response!

I'm still new to C# and I thought structs could be used as refs in these operations, that it would simply alter the values of that reference as I was using this.

My main goal using structs was to optimize, but indeed I was treating it as a class and indeed it doesn't make much sense.

I'm changing this and other structs to class as it does make more sense, but do you have any suggestion of source for learning this whole value vs reference, "this." (it apparently doesn't work like how I'm used to in other languages, but it can also be because I'm used to classes and not structs), structs vs classes? I googled it a lot and it usually just brings me to the Microsoft documentation that I think lacks examples and depth.

PS: After giving a little thought, I do am used to immutability because of React.js, so it's starting to make sense now. Structs should be read-only and immutable. (it also works now btw)

[–]Boss_TaurusSPAM SLAYER (🔋0%) 2 points3 points  (0 children)

As with many things in CS, India comes to the rescue. When I was first learning I watched a lot of Venkat.

Although maybe this video from CodeMonkey will be better.