Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Docs/VS_Scratch_Mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Scratch ブロックと FUnity 独自 Visual Scripting Unit の対応関係で
- Scratch 系のコルーチン Unit は Visual Scripting 標準の `flow.StartCoroutine` を用い、開始後に `ScratchUnitUtil.RegisterScratchFlow` で Flow を登録して停止ブロックと連動させる。
- Scratch のイベント Unit(緑の旗/キー押下/メッセージ受信/クローン開始など)は EventBus 登録時に Flow を新規作成し、`flow.StartCoroutine(trigger)` で起動してから `ScratchUnitUtil.RegisterScratchFlow(flow)` で ActorId/ThreadId を Flow.variables に保存する。Unity の Coroutine へは依存しない。
- `ScratchUnitUtil.RegisterScratchFlow` は Flow.variables に `FUNITY_SCRATCH_ACTOR_ID` / `FUNITY_SCRATCH_THREAD_ID` を格納し、`FUnityScriptThreadManager` の `(actorId, threadId)` テーブルへ登録する。停止系ユニットは Flow から逆引きした ActorId/ThreadId を使って停止対象を精密に選択する。
- Scratch のイベントリスナー状態は GraphReference 単位で static Dictionary に保持し、EventBus.Register/Unregister ではジェネリック型引数を明示する。`GraphStack.SetElementData` や EventUnit.Data 拡張に依存しない。
- Scratch のイベントリスナー状態は GraphReference と Unit(RuntimeHelpers.GetHashCode を使用)単位で static Dictionary に保持し、同一グラフ内に同種のイベントノードが複数あっても全て並行発火させる。EventBus.Register/Unregister ではジェネリック型引数を明示する。`GraphStack.SetElementData` や EventUnit.Data 拡張に依存しない。
- コルーチン専用ポート(例: Forever の `enter`)を叩く際は、Flow 上で `flow.StartCoroutine(trigger)` を使って実行し、コルーチンとしてのみ許可されているポートを正しく起動する。
- Presenter 取得は `ScratchUnitUtil.TryGetActorPresenter` を経由し、内部で `ResolveAdapter` と `ResolveActorPresenter` に委譲する共通ロジックを利用する。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using Unity.VisualScripting;
using FUnity.Runtime.Integrations.VisualScripting;
Expand Down Expand Up @@ -128,9 +129,12 @@ private IEnumerator Run(Flow flow)
[TypeIcon(typeof(FUnityScratchUnitIcon))]
public sealed class WhenIStartAsCloneUnit : EventUnit<CloneEventArgs>
{
/// <summary>GraphReference ごとに登録したクローン開始イベントのハンドラ管理。</summary>
private static readonly Dictionary<GraphReference, Action<CloneEventArgs>> s_Handlers
= new Dictionary<GraphReference, Action<CloneEventArgs>>();
/// <summary>
/// GraphReference と Unit ごとに登録したクローン開始イベントのハンドラ管理。
/// Unit 単位で登録することで、同一グラフ内に複数存在しても全て並行発火させます。
/// </summary>
private static readonly Dictionary<(GraphReference reference, int unitId), Action<CloneEventArgs>> s_Handlers
= new Dictionary<(GraphReference reference, int unitId), Action<CloneEventArgs>>();

/// <summary>EventBus に登録するかどうか。</summary>
protected override bool register => true;
Expand Down Expand Up @@ -179,8 +183,10 @@ public override void StartListening(GraphStack stack)
}

var reference = stack.ToReference();
var unitId = RuntimeHelpers.GetHashCode(this);
var key = (reference, unitId);

if (s_Handlers.ContainsKey(reference))
if (s_Handlers.ContainsKey(key))
{
return;
}
Expand All @@ -191,7 +197,7 @@ public override void StartListening(GraphStack stack)
if (hook.name != null && handler != null)
{
EventBus.Register<CloneEventArgs>(hook, handler);
s_Handlers[reference] = handler;
s_Handlers[key] = handler;
}
}

Expand All @@ -207,8 +213,10 @@ public override void StopListening(GraphStack stack)
}

var reference = stack.ToReference();
var unitId = RuntimeHelpers.GetHashCode(this);
var key = (reference, unitId);

if (!s_Handlers.TryGetValue(reference, out var handler) || handler == null)
if (!s_Handlers.TryGetValue(key, out var handler) || handler == null)
{
return;
}
Expand All @@ -219,7 +227,7 @@ public override void StopListening(GraphStack stack)
EventBus.Unregister(hook, handler);
}

s_Handlers.Remove(reference);
s_Handlers.Remove(key);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.VisualScripting;
using FUnity.Runtime.Core;
using FUnity.Runtime.Integrations.VisualScripting;
Expand All @@ -17,9 +18,12 @@ namespace FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits
[TypeIcon(typeof(FUnityScratchUnitIcon))]
public sealed class WhenGreenFlagClickedUnit : EventUnit<EmptyEventArgs>
{
/// <summary>GraphReference ごとに EventBus へ登録したハンドラを保持します。</summary>
private static readonly Dictionary<GraphReference, Action<EmptyEventArgs>> s_Handlers
= new Dictionary<GraphReference, Action<EmptyEventArgs>>();
/// <summary>
/// GraphReference と Unit ごとに EventBus へ登録したハンドラを保持します。
/// Unit 単位で管理することで、同一グラフ内に複数のイベントノードがあっても全て並行発火させます。
/// </summary>
private static readonly Dictionary<(GraphReference reference, int unitId), Action<EmptyEventArgs>> s_Handlers
= new Dictionary<(GraphReference reference, int unitId), Action<EmptyEventArgs>>();

/// <summary>Visual Scripting 側でイベント登録を行うかどうかを制御します。</summary>
protected override bool register => true;
Expand Down Expand Up @@ -81,8 +85,10 @@ public override void StartListening(GraphStack stack)
}

var reference = stack.ToReference();
var unitId = RuntimeHelpers.GetHashCode(this);
var key = (reference, unitId);

if (s_Handlers.ContainsKey(reference))
if (s_Handlers.ContainsKey(key))
{
return;
}
Expand All @@ -93,7 +99,7 @@ public override void StartListening(GraphStack stack)
if (hook.name != null && handler != null)
{
EventBus.Register<EmptyEventArgs>(hook, handler);
s_Handlers[reference] = handler;
s_Handlers[key] = handler;
}
}

Expand All @@ -109,8 +115,10 @@ public override void StopListening(GraphStack stack)
}

var reference = stack.ToReference();
var unitId = RuntimeHelpers.GetHashCode(this);
var key = (reference, unitId);

if (!s_Handlers.TryGetValue(reference, out var handler) || handler == null)
if (!s_Handlers.TryGetValue(key, out var handler) || handler == null)
{
return;
}
Expand All @@ -121,7 +129,7 @@ public override void StopListening(GraphStack stack)
EventBus.Unregister(hook, handler);
}

s_Handlers.Remove(reference);
s_Handlers.Remove(key);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.VisualScripting;
using UnityEngine;
using FUnity.Runtime.Integrations.VisualScripting;
Expand All @@ -18,9 +19,12 @@ namespace FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits
[TypeIcon(typeof(FUnityScratchUnitIcon))]
public sealed class OnKeyPressedUnit : EventUnit<EmptyEventArgs>
{
/// <summary>GraphReference 単位のキー入力リスナーを保持します。</summary>
private static readonly Dictionary<GraphReference, Action<EmptyEventArgs>> s_Handlers
= new Dictionary<GraphReference, Action<EmptyEventArgs>>();
/// <summary>
/// GraphReference と Unit ごとにキー入力リスナーを保持します。
/// 同一グラフ内で複数のキーイベントがあっても並列に処理できるよう unitId を含めて管理します。
/// </summary>
private static readonly Dictionary<(GraphReference reference, int unitId), Action<EmptyEventArgs>> s_Handlers
= new Dictionary<(GraphReference reference, int unitId), Action<EmptyEventArgs>>();

/// <summary>現在ひとつでもキー入力リスナーが登録されているかを返します。</summary>
internal static bool HasAnyListeners => s_Handlers.Count > 0;
Expand Down Expand Up @@ -121,15 +125,17 @@ public override void StartListening(GraphStack stack)
}

var reference = stack.ToReference();
var unitId = RuntimeHelpers.GetHashCode(this);
var key = (reference, unitId);

if (s_Handlers.ContainsKey(reference))
if (s_Handlers.ContainsKey(key))
{
return;
}

Action<EmptyEventArgs> handler = args => TriggerWithThreadRegistration(reference, args);

s_Handlers.Add(reference, handler);
s_Handlers.Add(key, handler);
}

/// <summary>
Expand All @@ -145,7 +151,10 @@ public override void StopListening(GraphStack stack)

var reference = stack.ToReference();

s_Handlers.Remove(reference);
var unitId = RuntimeHelpers.GetHashCode(this);
var key = (reference, unitId);

s_Handlers.Remove(key);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.VisualScripting;
using FUnity.Runtime.Integrations.VisualScripting;

Expand Down Expand Up @@ -139,9 +140,12 @@ private IEnumerator OnEnterCoroutine(Flow flow)
[TypeIcon(typeof(FUnityScratchUnitIcon))]
public sealed class WhenIReceiveMessageUnit : EventUnit<MessagingCommon.Args>
{
/// <summary>GraphReference ごとに登録済みのメッセージリスナーを保存します。</summary>
private static readonly Dictionary<GraphReference, Action<MessagingCommon.Args>> s_Handlers
= new Dictionary<GraphReference, Action<MessagingCommon.Args>>();
/// <summary>
/// GraphReference と Unit ごとに登録済みのメッセージリスナーを保存します。
/// 同一グラフ内に複数の「メッセージを受け取ったとき」が存在しても全て発火させるため、unitId を含めて管理します。
/// </summary>
private static readonly Dictionary<(GraphReference reference, int unitId), Action<MessagingCommon.Args>> s_Handlers
= new Dictionary<(GraphReference reference, int unitId), Action<MessagingCommon.Args>>();

/// <summary>EventUnit の自動登録を有効にします。</summary>
protected override bool register => true;
Expand Down Expand Up @@ -219,8 +223,10 @@ public override void StartListening(GraphStack stack)
}

var reference = stack.ToReference();
var unitId = RuntimeHelpers.GetHashCode(this);
var key = (reference, unitId);

if (s_Handlers.ContainsKey(reference))
if (s_Handlers.ContainsKey(key))
{
return;
}
Expand All @@ -231,7 +237,7 @@ public override void StartListening(GraphStack stack)
if (hook.name != null && handler != null)
{
EventBus.Register<MessagingCommon.Args>(hook, handler);
s_Handlers[reference] = handler;
s_Handlers[key] = handler;
}
}

Expand All @@ -247,8 +253,10 @@ public override void StopListening(GraphStack stack)
}

var reference = stack.ToReference();
var unitId = RuntimeHelpers.GetHashCode(this);
var key = (reference, unitId);

if (!s_Handlers.TryGetValue(reference, out var handler) || handler == null)
if (!s_Handlers.TryGetValue(key, out var handler) || handler == null)
{
return;
}
Expand All @@ -259,7 +267,7 @@ public override void StopListening(GraphStack stack)
EventBus.Unregister(hook, handler);
}

s_Handlers.Remove(reference);
s_Handlers.Remove(key);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.VisualScripting;
using FUnity.Runtime.Core;
using FUnity.Runtime.Integrations.VisualScripting;
Expand All @@ -22,13 +23,13 @@ namespace FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits
public sealed class WhenThisSpriteClickedUnit : EventUnit<EmptyEventArgs>
{
/// <summary>
/// GraphReference ごとに EventBus へ登録したハンドラを保持します。
/// GraphReference と Unit ごとに EventBus へ登録したハンドラを保持します。
///
/// ・キー: GraphReference
/// ・キー: (GraphReference, unitId)
/// ・値: EventBus から呼び出されるコールバック
/// </summary>
private static readonly Dictionary<GraphReference, Action<EmptyEventArgs>> s_Handlers
= new Dictionary<GraphReference, Action<EmptyEventArgs>>();
private static readonly Dictionary<(GraphReference reference, int unitId), Action<EmptyEventArgs>> s_Handlers
= new Dictionary<(GraphReference reference, int unitId), Action<EmptyEventArgs>>();

/// <summary>
/// EventUnit による EventBus への自動登録を有効にします。
Expand Down Expand Up @@ -104,8 +105,10 @@ public override void StartListening(GraphStack stack)
}

var reference = stack.ToReference();
var unitId = RuntimeHelpers.GetHashCode(this);
var key = (reference, unitId);

if (s_Handlers.ContainsKey(reference))
if (s_Handlers.ContainsKey(key))
{
return;
}
Expand All @@ -116,7 +119,7 @@ public override void StartListening(GraphStack stack)
if (hook.name != null && handler != null)
{
EventBus.Register<EmptyEventArgs>(hook, handler);
s_Handlers[reference] = handler;
s_Handlers[key] = handler;
}
}

Expand All @@ -132,8 +135,10 @@ public override void StopListening(GraphStack stack)
}

var reference = stack.ToReference();
var unitId = RuntimeHelpers.GetHashCode(this);
var key = (reference, unitId);

if (!s_Handlers.TryGetValue(reference, out var handler) || handler == null)
if (!s_Handlers.TryGetValue(key, out var handler) || handler == null)
{
return;
}
Expand All @@ -144,7 +149,7 @@ public override void StopListening(GraphStack stack)
EventBus.Unregister(hook, handler);
}

s_Handlers.Remove(reference);
s_Handlers.Remove(key);
}

/// <summary>
Expand Down