Struct?

first off @sampattuzzi , thanks for the bonus content! awesome… I was wondering how to save more than one var.

What’s a struct? Why would I use it over the Dictionary? coming from the unity 3d course, and going into the RPG course next, we’ve never seen a struct or know what it is… googling gives some idea but I trust you for the correct information.

1 Like

Clearly I’m not Sam.

However, the benefits Sam was suggesting may make the struct a better choice than the dictionary in this example are as follows:

  1. As the dictionary was storing a generic “object” type, there had to be casts and correct nesting of brackets in order to extract it out again when restoring; which at the very least makes the code less readable.
  2. it was simpler and less error prone - because the compiler will help catch issues before you even run the game - to use direct variable names for rotation and position instead of string keys for getting the data out of the dictionary index.

There can also be some runtime efficiencies gains from a struct too, but probably not a significant impact in this case, and just generally it helps keep the related data together in a meaningful way as you can quickly inspect the members of the struct in the source code and see what they are about and how they are related. Not so easy to scan for all the members in the source code added to the dictionary and what types/values they represent.

1 Like

Basically all the above. I want to add that a struct is actually more like a class than a dictionary. In fact, you could use a class too and that would work just fine. The difference between structs and classes is complicated for a beginner to understand but it has to do with how memory is allocated. Classes are created on the “heap” and then references to them are passed around. On the other hand structs are allocated on the “stack” and are copied into the functions they are passed to. The later is good for small objects or ones that you want to be sure you can’t accidentally be changed by a function you are calling (because they get a copy rather than the reference to the original).

3 Likes

Strictly speaking, structs are not exclusively allocated on the stack, however it’s good for a “rule of thumb” for thinking about them.

A fuller treatise on that is available here: https://stackoverflow.com/a/204009

If Jon can’t make the answer succinct, then a simple answer is probably not going to be correct.

There’s some relevant information from Eric about it too that is a little more readable/digestible for newer people to c#:

https://docs.microsoft.com/en-us/archive/blogs/ericlippert/the-truth-about-value-types

From a Unity standpoint probably the main reason you’d care about using a struct because it might be on the stack, vs. a dictionary that would be on the heap, is a thing called “garbage collection” (GC), which is C#'s way of automatically cleaning up unused data for you so that you don’t get memory leaks.

Whilst it’s generally fabulous at the job it performs, that GC can strike unpredictably when your code is running, and that it effectively halts all execution until the GC cycle completes, can create jarring stutters when you want to have smooth frame rates.

Partly for this reason, and also to perform as efficient memory alignment and packing as possible, the performance-boosting ECS / DOTS in Unity leans towards struct use heavily.

2 Likes

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

Privacy & Terms