-
Notifications
You must be signed in to change notification settings - Fork 0
feat: register scratch threads for scratch events #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new OnKeyPressed trigger wraps the event in a coroutine that contains no yield before Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| /// <summary> | ||
| /// ポート定義を行い、キー入力を条件とするイベントを登録します。 | ||
| /// </summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The message-receive trigger runs the flow in a coroutine that immediately hits Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| /// <summary> | ||
| /// ポート定義と EventUnit 基底の初期化を行います。 | ||
| /// </summary> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the clone-start event fires, the coroutine used to run the flow has no yield before
yield break, soStartCoroutine(Routine())executes synchronously and disposesflowbefore callingEnsureScratchThreadRegistered. 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 👍 / 👎.