# ConvertNode

When connecting data ports, you can use a script to create a node that performs data type conversion.

## How to write a script

- Create a class that inherits from `LogicToolkit.ConvertNode`.
- Apply `System.SerializableAttribute` to the type.
- Define a method called `static bool Convertible(System.Type from, System.Type to)` to determine whether type conversion is possible.
- Define a method called `protected override void Convert(IInputDataPort input, IOutputDataPort output)` and write the type conversion process.

When you connect a port with a data type that meets the conditions, a conversion node is automatically inserted. Each input/output port is created from the data type when connected.

## Code example

The built-in EvaluateToQuaternionNode is implemented as follows.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    namespace LogicToolkit
    {
        [System.Serializable]
        public class EulerToQuaternionNode : ConvertNode
        {
            static bool Convertible(System.Type from, System.Type to)
            {
                return from == typeof(Vector3) && to == typeof(Quaternion);
            }

            protected override void Convert(IInputDataPort input, IOutputDataPort output)
            {
                if (input.TryGetValue<Vector3>(out var value))
                {
                    output.TrySetValue(Quaternion.Euler(value));
                }
            }
        }
    }