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
6 changes: 3 additions & 3 deletions Docs/VS_Scratch_Mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Scratch ブロックと FUnity 独自 Visual Scripting Unit の対応関係で
| ○秒待つ | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.WaitSecondsUnit | ○秒待つ | FUnity/Scratch/制御 | 指定時間待機。定義: Runtime/.../WaitSecondsUnit.cs |
| 自分のクローンを作る | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.CreateCloneOfSelfUnit | 自分のクローンを作る | FUnity/Scratch/制御 | 自身を複製。定義: Runtime/.../CloneUnits.cs |
| ○のクローンを作る | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.CreateCloneOfDisplayNameUnit | ○のクローンを作る | FUnity/Scratch/制御 | 指定俳優を複製。定義: Runtime/.../CloneUnits.cs |
| クローンされたとき | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.WhenIStartAsCloneUnit | クローンされたとき | Events/FUnity/Scratch/制御 | クローン生成時イベント。定義: Runtime/.../CloneUnits.cs |
| クローンされたとき | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.WhenIStartAsCloneUnit | クローンされたとき | Events/FUnity/Scratch/制御 | クローン生成時イベントとしてスクリプトを Scratch スレッド登録。定義: Runtime/.../CloneUnits.cs |
| このクローンを削除する | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.DeleteThisCloneUnit | このクローンを削除する | FUnity/Scratch/制御 | クローンを破棄。定義: Runtime/.../CloneUnits.cs |
| すべてを止める | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.StopAllUnit | Scratch/すべてを止める | FUnity/Scratch/制御 | Scratch 用スレッドテーブル経由で全スレッド停止。定義: Runtime/.../StopControlUnits.cs |
| このスクリプトを止める | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.StopThisScriptUnit | Scratch/このスクリプトを止める | FUnity/Scratch/制御 | 現在の Scratch スレッドのみ停止。定義: Runtime/.../StopControlUnits.cs |
Expand All @@ -73,10 +73,10 @@ Scratch ブロックと FUnity 独自 Visual Scripting Unit の対応関係で
| Scratch ブロック (日本語) | FUnity Unit クラス | UnitTitle | UnitCategory | 備考 |
| --- | --- | --- | --- | --- |
| 緑の旗が押されたとき | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.WhenGreenFlagClickedUnit | 緑の旗が押されたとき | Events/FUnity/Scratch/イベント | Runner 対象の緑の旗イベント。ScriptMachine.nest.macro を優先し、未設定時は machine.graph で Scratch 用スレッドを登録。定義: Runtime/.../GreenFlagUnits.cs |
| ○キーが押されたとき | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.OnKeyPressedUnit | ○キーが押されたとき | Events/FUnity/Scratch/イベント | 押下エッジで発火。定義: Runtime/.../InputEventUnits.cs |
| ○キーが押されたとき | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.OnKeyPressedUnit | ○キーが押されたとき | Events/FUnity/Scratch/イベント | 押下エッジで発火し、開始スクリプトを Scratch スレッドとして登録。定義: Runtime/.../InputEventUnits.cs |
| メッセージを送る | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.BroadcastMessageUnit | メッセージを送る | FUnity/Scratch/イベント | 即時配信。定義: Runtime/.../MessagingUnits.cs |
| メッセージを送って待つ | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.BroadcastAndWaitUnit | メッセージを送って待つ | FUnity/Scratch/イベント | 同期配信後に継続。定義: Runtime/.../MessagingUnits.cs |
| メッセージを受け取ったとき | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.WhenIReceiveMessageUnit | メッセージを受け取ったとき | Events/FUnity/Scratch/イベント | フィルタ一致時に発火。定義: Runtime/.../MessagingUnits.cs |
| メッセージを受け取ったとき | FUnity.Runtime.Integrations.VisualScripting.Units.ScratchUnits.WhenIReceiveMessageUnit | メッセージを受け取ったとき | Events/FUnity/Scratch/イベント | フィルタ一致時に発火し、受信スクリプトを Scratch スレッドとして登録。定義: Runtime/.../MessagingUnits.cs |

## 調べる
| Scratch ブロック (日本語) | FUnity Unit クラス | UnitTitle | UnitCategory | 備考 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,50 @@ public override EventHook GetHook(GraphReference reference)
return new EventHook(FUnityEventNames.OnCloneStart, reference.self);
}

/// <summary>
/// クローン開始イベントをコルーチンとして実行し、Scratch 用スレッドとして登録します。
/// ScriptMachine が存在しない場合は従来の処理にフォールバックします。
/// </summary>
/// <param name="reference">現在のグラフ参照。</param>
/// <param name="args">クローンイベント引数。</param>
public new void Trigger(GraphReference reference, CloneEventArgs args)
{
var machine = reference?.machine as ScriptMachine;
if (machine == null)
{
base.Trigger(reference, args);
return;
}

var flow = Flow.New(reference);
AssignArguments(flow, args);
if (!ShouldTrigger(flow, args))
{
flow.Dispose();
return;
}

var adapter = ScratchUnitUtil.ResolveAdapter(flow);
ScriptGraphAsset graph = null;
if (machine.nest != null)
{
graph = machine.nest.macro as ScriptGraphAsset;
}

IEnumerator Routine()
{
using (flow)
{
flow.Invoke(trigger);
}

yield break;
}

var coroutine = machine.StartCoroutine(Routine());
ScratchUnitUtil.EnsureScratchThreadRegistered(flow, adapter, graph, coroutine);
Comment on lines +185 to +189
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Clone event registers thread only after script finishes

When the clone-start event fires, the coroutine used to run the flow has no yield before yield break, so StartCoroutine(Routine()) executes synchronously and disposes flow before calling EnsureScratchThreadRegistered. That registers a completed coroutine and sets thread context after the graph has already executed, leaving scripts started by "クローンされたとき" without a tracked Scratch thread while they run, so stop/stop-this-script/stop-all cannot control them.

Useful? React with 👍 / 👎.

}

/// <summary>EventUnit 基底の定義を呼び出します。</summary>
protected override void Definition()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections;
using Unity.VisualScripting;
using UnityEngine;
using FUnity.Runtime.Integrations.VisualScripting;
Expand Down Expand Up @@ -39,6 +40,50 @@ public override EventHook GetHook(GraphReference reference)
return EventHooks.Update;
}

/// <summary>
/// キー押下イベントをコルーチンとして起動し、Scratch 用スレッドとして登録します。
/// ScriptMachine を取得できない場合は従来の同期実行にフォールバックします。
/// </summary>
/// <param name="reference">現在のグラフ参照。</param>
/// <param name="args">空のイベント引数。</param>
public new void Trigger(GraphReference reference, EmptyEventArgs args)
{
var machine = reference?.machine as ScriptMachine;
if (machine == null)
{
base.Trigger(reference, args);
return;
}

var flow = Flow.New(reference);
AssignArguments(flow, args);
if (!ShouldTrigger(flow, args))
{
flow.Dispose();
return;
}

var adapter = ScratchUnitUtil.ResolveAdapter(flow);
ScriptGraphAsset graph = null;
if (machine.nest != null)
{
graph = machine.nest.macro as ScriptGraphAsset;
}

IEnumerator Routine()
{
using (flow)
{
flow.Invoke(trigger);
}

yield break;
}

var coroutine = machine.StartCoroutine(Routine());
ScratchUnitUtil.EnsureScratchThreadRegistered(flow, adapter, graph, coroutine);
Comment on lines +80 to +84
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Register Scratch thread before coroutine completes

The new OnKeyPressed trigger wraps the event in a coroutine that contains no yield before yield break, so StartCoroutine(Routine()) executes the entire flow and disposes flow before EnsureScratchThreadRegistered is called. That registers a coroutine that has already finished and only sets the thread context after the script has run, meaning key-press scripts still execute without a Scratch thread ID and stop/stop-this-script units cannot stop them. The registration needs to occur before invoking the graph or the routine needs to yield so the thread is tracked while the script runs.

Useful? React with 👍 / 👎.

}

/// <summary>
/// ポート定義を行い、キー入力を条件とするイベントを登録します。
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,50 @@ public override EventHook GetHook(GraphReference reference)
return new EventHook(MessagingCommon.EventName);
}

/// <summary>
/// メッセージ受信イベントをコルーチンで実行し、Scratch 用スレッドとして登録します。
/// ScriptMachine を取得できない場合は既存の同期処理にフォールバックします。
/// </summary>
/// <param name="reference">現在のグラフ参照。</param>
/// <param name="args">メッセージイベント引数。</param>
public new void Trigger(GraphReference reference, MessagingCommon.Args args)
{
var machine = reference?.machine as ScriptMachine;
if (machine == null)
{
base.Trigger(reference, args);
return;
}

var flow = Flow.New(reference);
AssignArguments(flow, args);
if (!ShouldTrigger(flow, args))
{
flow.Dispose();
return;
}

var adapter = ScratchUnitUtil.ResolveAdapter(flow);
ScriptGraphAsset graph = null;
if (machine.nest != null)
{
graph = machine.nest.macro as ScriptGraphAsset;
}

IEnumerator Routine()
{
using (flow)
{
flow.Invoke(trigger);
}

yield break;
}

var coroutine = machine.StartCoroutine(Routine());
ScratchUnitUtil.EnsureScratchThreadRegistered(flow, adapter, graph, coroutine);
Comment on lines +199 to +203
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Message receive scripts not tracked during execution

The message-receive trigger runs the flow in a coroutine that immediately hits yield break, so StartCoroutine(Routine()) finishes before EnsureScratchThreadRegistered runs. Thread registration therefore happens after the script has already executed (with flow disposed), so handlers for "メッセージを受け取ったとき" still run without a Scratch thread ID and cannot be stopped by the Scratch thread manager. Registration must happen before invoking the graph or the routine needs to yield to keep the thread alive while the script runs.

Useful? React with 👍 / 👎.

}

/// <summary>
/// ポート定義と EventUnit 基底の初期化を行います。
/// </summary>
Expand Down