MathsEngine is designed to be extensible.
You can create your own mathematical operations by implementing custom Math Nodes.
All nodes derive from BaseMathNode.
Creating a Custom Node
To create a new node:
- Create a class that inherits from BaseMathNode
- Define the node inputs
- Implement the Calculate() function
- Implement ToEquation() for debugging and visualization
Example: Square Node
The following example creates a node that squares a value.
using UnityEngine;
{
[SerializeField]
public override float Calculate(object[] parameters)
{
return v * v;
}
public override string ToEquation()
{
return $"({value.ToEquation()}^2)";
}
}
Base class for all math nodes used in the formula engine.
Definition BaseMathNode.cs:12
float Calculate(object[] parameter)
Calculates the value of this node.
Definition BaseMathNode.cs:7
Node Inputs
Nodes usually take other nodes as inputs.
Example:
Multiply
├─ Node A
└─ Node B
Implementation example:
[SerializeField]
private BaseMathNode inputA;
[SerializeField]
private BaseMathNode inputB;
Using the Custom Node
Once created, the node can be added to a MathFormula.
Example graph:
Result: