When passing a lot of information (for example, 100 variables) to a function in this way:
MyFunction(int int, bool bool, string string, int int2, bool bool2, int int3, (etc.))
{
// Do something
}
According to what I read, one recommended way would be to use structs. However, I have no idea how to use structs properly. For instance, (1) I don’t know how to set or update the parameters inside a struct, (2) I don’t know where to place the struct (is it better to create a new cs and keep the struct there, or is it better for it to live inside another script? Does it need to be a singleton in order for it to be referenced and accessed?) (3) If I wanted to reference the struct info inside that function, would it be in this manner?
Structs are value type objects mostly used to store lightweight information. Fun fact, int, floats and other integral types are structs under the hood. Read up on value types and reference types if you don’t know the difference.
You use a constructor. Such as:
public struct MyStruct
{
//constructor
public MyStruct(int intValue, float floatValue)
{
this.IntValue = intValue;
this.FloatValue = floatValue;
}
public int IntValue;
public float FloatValue;
}
And you create it by instantiating the struct:
MyStruct myStruct = new MyStruct(someInt, someFloat);
In most cases if you intend the struct to be used in a number of places, it should be it’s own script asset. This is good practice in general. Sometimes if the struct is only used inside a class you can make a private struct inside the same file.
No and no. See above about instantiating an instance of a struct.
I have a couple of questions. Just to make sure, you instantiate the struct on the script where you want to reference it, right?
The other question is, when I use the constructor (or when I instantiate it), I noticed that I have to reference all the variables inside the struct (for example (int intValue, float floatValue, etc.)). If I have a hundred items inside the struct (this is an exaggeration, but imagine there’s a lot), this would be tedious to do. Is there an alternative way to do it? Or everybody does it one by one?
Pretty much. Look to Unity’s own structs for example, such as Vector2 or Vector3. They’re lightweight and small in scope and serve a specific if very useful purpose.
Worth noting that you don’t ‘reference’ structs as they’re value types. They are merely passed around like other integral types. When you assign one struct to another struct, it’s copying over values, unlike classes when you do have references (ergo, a pointer to the object in memory).
This is where you consider breaking down your data into smaller and and more manageable objects, be they classes or structs. It’s sort of the art of encapsulation. No doubt the single-responsibility principle (look it up) comes into play here too.
A struct/class with that much data in it sounds like a bit of a god-object and should be broken down into smaller parts.
I’ve been doing some research, and maybe for sending so many parameters is best to use JSON. I haven’t used it so far, so I’m still not sure if it suits my specific case (sending data host-client).