[SOLVED]Request help related with Variables encapsulation

Hi everyone

Currently, I am doing the code encapsulation and I have some issue about how to deal with the variables.

Here is what happened, in some of my scripts, I have dozens of variables that could be devided into groupes. My gut tells me I should encapsulate them for cleaner code. However, I am kind entangled with how to encapsulate them.

My first ideal is using the “struct” , and then I thought using the “class” might be more nature. After that I was thinking about using the scriptable object So it is easier to store the data.
Could anyone give me some advice? Thank you.

What problem are you trying to solve? Just for “cleaner code”?

It’s not necessary to do so if those variables belong to that class. Now, if it’s something like, you have stats like HP, MP, etc and you know you can use that stats class for the player, monsters, whatever else, then feel free! That’s a great use. But, if those variables are only being used for stuff in that class, it’s probably not necessary.

Just split your variables up with line spaces. You can use the Header param also if you want them split up in the inspector.

1 Like

There is a world of difference between these two. You cannot just swap them out at random or on a lark.

Each has their points. Structs are value types, classes are reference types.

Learn and love the difference or you will have some of the most bafflingly bizarre bugs you have ever seen. :slight_smile:

Value Types vs Reference Types:

https://discussions.unity.com/t/826396/4

2 Likes

Sorry for the confuse, yes the variables I intend to encapsulation are stats, they will be used in other scripts.
My real issue is that I wish to learn more about the different and usage of “struct” and “class”. As well as, when should I apply ScriptOject.
Thanks.

Don’t group values together just for cleaner code. Encapsulation should work hand in hand with the Single Responsibility Principle, where values in the same class should all be part of some single (usually small) greater purpose.

If it makes sense for them to be combined in this fashion, then yes, encapsulate them!

Structs should be used for very small amounts of data and as mentioned, are value types. Think how Vector3 is just 3 float values.

Anything bigger should be a class, which are reference types. A very important distinction to learn.

Scriptable Objects should be used when you need instances of values that live as assets you can use around your project (this is the main purpose of SO’s, but not the only thing they can be used for).

Honestly the art of encapsulation will simply start to make more sense the more you code. Eventually you won’t even think about it.

1 Like

As a rule of thumb, if you want to make copies of your data (i.e. x=y), it will be easier to use structs.

Hi. I ran into what sounds like a similar situation. I have a large number of game objects that need to be exposed on UI menus so that other code can access them easily. I started with unique property names but that resulted in long names to preserve uniqueness. VideoPlayerStopButton VideoPlayerStopButtonTest, SkyboxShowButton, SkyboxHideButton and stuff like that.

I finally opted for structs and now the code flows much nicer and it is consistent and reusable.

Do note the use of the init syntax. It’s availability depends upon the version of C# you are using but it allows me to make the struct readonly…

using ui = UnityEngine.UI;

public readonly struct ImageStruct
{
    public ui.Button ShowButton { get; init; }
    public ui.Button HideButton { get; init; }

    public ui.Button FirstButton { get; init; }
    public ui.Button PreviousButton { get; init; }
    public ui.Button NextButton { get; init; }
    public ui.Button LastButton { get; init; }
    public ui.Button RandomButton { get; init; }

    public TextMeshProUGUI Title { get; init; }
}

You can clearly embed logic in them as well making (in this case) the button text and color change easy.

public readonly struct ShoutStruct
{
    public ui.Button Button { get; init; }
    public TextMeshProUGUI ButtonText { get; init; }


    public void ToggleState(Boolean isShouting)
    {
        Button.colors = Config.ButtonColors(isShouting, Button);
        ButtonText.text = isShouting ? "Shout Is On" : "Shout Is Off";
    }
}