カスタムFlowノードを作る
Noraサンプルのキャラクターを好きな方向に歩かせようとしたが、atan2ノードが意図した値を返してくれなかったため、
公式ドキュメントを参考にカスタムFlowノードを作ることにした。
今回は定義ファイルに直接コードを載せているが、他のluaファイルの関数名を設定して、そちらを参照させることもできる。
ノード定義スクリプトの作成
以下のようなファイルを作成し、script/luaの下に配置する。
(Atan2以外テストしてないので、何か記述ミスがあるかも)
custom_math.script_flow_nodes
nodes = [
{
name = "Atan2"
category = "Math/Custom"
visibility = "all"
args = {
x = "float"
y = "float"
}
returns = {
result = "float"
}
query = true
function = """ return function(t) t.result = math.atan2(t.y, t.x) return t end """
}
{
name = "Cos"
category = "Math/Custom"
visibility = "all"
args = {
r = "float"
}
returns = {
result = "float"
}
query = true
function = """ return function(t) t.result = math.cos(t.r) return t end """
}
{
name = "Sin"
category = "Math/Custom"
visibility = "all"
args = {
r = "float"
}
returns = {
result = "float"
}
query = true
function = """ return function(t) t.result = math.sin(t.r) return t end """
}
{
name = "Pi"
category = "Math/Custom"
visibility = "all"
args = {
}
returns = {
result = "float"
}
query = true
function = """ return function(t) t.result = math.pi return t end """
}
{
name = "Rad2Deg"
category = "Math/Custom"
visibility = "all"
args = {
r = "float"
}
returns = {
result = "float"
}
query = true
function = """ return function(t) t.result = t.r * 180 / math.pi return t end """
}
{
name = "Deg2Rad"
category = "Math/Custom"
visibility = "all"
args = {
d = "float"
}
returns = {
result = "float"
}
query = true
function = """ return function(t) t.result = t.d * math.pi / 180 return t end """
}
]
query = trueというのは、クエリーノードにするという宣言。 クエリーノードにすることによって、Out-Inのソケットの接続が不要になる。
クエリー ノードには入力イベントがありません。その代わりに、別のノードがクエリー ノードの出力データを必要としている場合は、オンデマンドで自動的にトリガされます。クエリー ノードは通常、ユニットの現在位置など、いつでも古くなる可能性のある変動しやすいデータを取得する場合に役立ちます。
実際どういうことになるかについては、最後に記載した画像を参照。
実際に使ってみる
適当なシーンを作成して、Level Flowを開く。
Math>Custom>Atan2を選択。
確認のため、以下のようにノードを接続する。
接続されていないほうのAtan2はMath>Trigonometryにある、Stingray組込機能のAtan2。
再生すると、コンソールにAtan2の結果が毎フレーム表示されているはず。
クエリーノードにしなかった場合
こうなってしまうので、副作用の無い問い合わせ関数はなるべくクエリーノードにしよう。