# Custom Serializable Objects

When using the `AddData` method to save Unity types such as Transform, Vectors, etc., ESave converts each to a savable type automatically. However, this will not happen if you try saving a custom serializable object that has unsavable Unity types as fields.

## Solution

You will have to manually convert each to a savable type.

### Example Saving

Here we have a custom serializable class with unsavable Unity types.

```csharp
[System.Serializable]
public class TestClass
{
    public Vector3 position;
    public Quaternion rotation;
    public Color color;
}
```

If you try saving this class, ESave will throw an error. To fix this, we can use the `SavableVector` class for both fields.

```csharp
[System.Serializable]
public class TestClass
{
    public SavableVector position;
    
    // A Quaternion is similar to Vector4 (x, y, z, w)
    public SavableVector rotation;
    
    // A Color is similar to Vector4 (r, g, b, a)
    public SavableVector color;
}
```

This will allow your class to be saved with ESave. For `Transform`, you can use `SavableTransform`.

```csharp
[System.Serializable]
public class TestClass
{
    public SavableVector position;
    public SavableVector rotation;
    public SavableVector color;
    public SavableTransform transform;
}
```

The `SavableTransform` saves the position and rotation, so the first 2 fields are technically not necessary, but we will leave them here just for informational purposes.

Now that we have our class, let's give it some data and then save it.

```csharp
// Create a color that we will save
var myColor = Color.blue;

// Create the savable class
var testClass = new TestClass
{
    // Converting each to a savable type
    position = transform.position.ToSavable();
    rotation = transform.rotation.ToSavable();
    color = myColor.ToSavable();
    transform = transform.ToSavable();
};

// Set the ID we will use for retreival
string testClassID = "test";

// Save the data
saveState.AddData(testClassID, testClass);
```

### Example Loading

Here's how we can load the `TestClass` from above:

```csharp
// Load the data
var test = saveState.GetData<TestClass>(testClassID);

// Assign loaded values
transform.position = test.position.vector3Value;
transform.rotation = test.rotation.quaternionValue;
var myColor = test.color.colorValue;
transform.SetPositionAndRotation(test.transform.Position, test.transform.Rotation);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://espergames.gitbook.io/esave/esave-pro/scripting/saving-and-loading/custom-serializable-objects.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
