# Callbacks

RS supports serialization callback methods. A callback can be used for manipulating objects after serialization and deserialization. There are two types of callback methods provided by RS, they are as follows:

## Implicit Callbacks

As the name suggests, this callback is sent to the object that is currently being serialized/deserialized. A typical usecase for this would be, if you have an object that wants to process information after it’s properties are deserialized completely. In that case, you can implement IRuntimeSerializationCallback to get a callback soon after the object is deserialized. Along with deserialization callback, IRuntimeSerializationCallback also provides callback for serialization as well.

This interface can be used on MonoBehaviours as well as on custom classes.

The usage of IRuntimeSerializationCallback is shown below:

```
using UnityEngine;
using System.Collections;
using VoxelBusters.RuntimeSerialization;

[RuntimeSerializable]
public class Car : IRuntimeSerializationCallback
{
    #region Fields

    [RuntimeSerializeField]
    private string m_model;

    [RuntimeSerializeField]
    private int m_wheelCount;

    #endregion

    #region Constructors

    public Car (string _model, int _wheelCount)
    {
        this.m_model         = _model;
        this.m_wheelCount    = _wheelCount;
    }

    #endregion

    #region Callbacks

    public void OnAfterRuntimeSerialize ()
    {
        Debug.Log("[RS] Serialization is completed");
    }

    public void OnAfterRuntimeDeserialize ()
    {
        Debug.Log("[RS] Deserialization is completed");
    }

    #endregion
}
```

## Observer Callbacks

In few cases, an object might be interested in knowing the serialization/deserialization of some other object. In that case, implement IRuntimeSerializationEventListener interface and then register for callbacks by providing the same serialization identifier that is being used to serialize/deserialize the other object.

Implementation of IRuntimeSerializationEventListener is very much similar to that of IRuntimeSerializationCallback interface. Unlike IRuntimeSerializationCallback, these callback methods wont get triggered on its own. Here as you are observing serialization process of some other object, you have to manually register for events inorder to get callbacks.

Following example shows how CarManager instance gets callback when RS serializes/deserializes some other object which is using serialization identifier as “cars”.

```
using UnityEngine;
using System.Collections;
using VoxelBusters.RuntimeSerialization;

public class CarManager : MonoBehaviour, IRuntimeSerializationEventListener
{
    #region Unity Methods

    private void Start ()
    {
        // Register observer
        RSManager.RegisterEventListener("cars", this as IRuntimeSerializationEventListener);
    }

    private void OnDisable ()
    {
        // Unregister observer
        RSManager.UnRegisterEventListener("cars", this as IRuntimeSerializationEventListener);
    }

    #endregion

    #region Callbacks

    public void OnAfterRuntimeSerialize (string _key, object _object)
    {
        Debug.Log("[RS] Serialization is completed");
    }

    public void OnAfterRuntimeDeserialize (string _key, object _object)
    {
        Debug.Log("[RS] Deserialization of object" + _object + " is completed.");
    }

    #endregion
}
```
