Class Data Storage / System Serializable

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By msx07

Hi,

I’m coming from Unity and I have some issues trying to understand how Godot works with pure storage classes and arrays editable in the editor.

In Unity, classes that can go on object inherits from Monobehaviour but we can create classes that won’t inehrit from Mono and that will serve as data holders mainly.

For instance, if I want to make a quiz game, I will make a QuizManager class that inherits from Monobehaviour and that will go on a game object.

I will also create two other classes that won’t inherit from Mono, QuestionData and AnswerData and I will make these classes serializable, the answerData will be an array in the QuestionData and the QuestionData will be an array in the QuizManager.

In code this would give something like this :

QuizManager.cs :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QuizManager : MonoBehaviour
{
    [SerializeField] private QuestionData[] _questionData;
}

QuestionData.cs :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class QuestionData
{
    public string _question;
    public AnswerData[]  _answers;
    public int _correctAnswerID;
}

AnswerData.cs :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class AnswerData
{
    public string _answer;
    
}

Theses scripts would give the following result in the editor once designer worked on them :

ImageEditor

Of course at the end there is a system that allows data to be imported from CSV or JSON or else in order to avoid to manually enter these data in the engine but that’s another step for another time, for the moment, how do you reproduce a similar architecture in Godot ? Is this something possible ?

Thanks

I believe Resource class may be that thing.
check out some tutorials on them. I found them actually icky to set up, so I prefer to store data in script classes ( which extend Resource anyway) or autoloaded scripts. Main advantage of resources classes is that You can edit them within editors UI.

Inces | 2022-09-23 19:03