-
Notifications
You must be signed in to change notification settings - Fork 1
Add network execution helper for visual scripting instructions #8
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
Open
andreathar
wants to merge
1
commit into
atharmcp
Choose a base branch
from
codex/add-inetworkcharacteradapter-implementation
base: atharmcp
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...ages/GameCreator_Multiplayer/Runtime/VisualScripting/Core/NetworkInstructionExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using GameCreator.Runtime.Common; | ||
| using GameCreator.Runtime.VisualScripting; | ||
| using Unity.Netcode; | ||
| using UnityEngine; | ||
|
|
||
| namespace GameCreator.Multiplayer.Runtime.VisualScripting.Core | ||
| { | ||
| /// <summary> | ||
| /// Helper extensions that route Instruction execution through the multiplayer layer. | ||
| /// </summary> | ||
| public static class NetworkInstructionExtensions | ||
| { | ||
| private const string LogPrefix = "[NetworkInstruction]"; | ||
|
|
||
| /// <summary> | ||
| /// Executes an Instruction according to the requested network mode. | ||
| /// </summary> | ||
| /// <param name="instruction">Instruction instance invoking the execution.</param> | ||
| /// <param name="action">Instruction logic to run.</param> | ||
| /// <param name="mode">Network execution mode requested by the Instruction.</param> | ||
| /// <param name="args">Execution arguments used to locate the network context.</param> | ||
| public static async Task ExecuteNetworked( | ||
| this Instruction instruction, | ||
| Func<Task> action, | ||
| NetworkExecutionMode mode, | ||
| Args args) | ||
| { | ||
| if (action == null) | ||
| { | ||
| Debug.LogWarning($"{LogPrefix} No action provided for networked execution"); | ||
| return; | ||
| } | ||
|
|
||
| if (mode == NetworkExecutionMode.LocalOnly) | ||
| { | ||
| await action(); | ||
| return; | ||
| } | ||
|
|
||
| GameObject contextObject = args != null ? args.Self : null; | ||
| NetworkExecutionContext context = NetworkExecutionContext.FromGameObject(contextObject); | ||
| string instructionName = instruction != null | ||
| ? $"{instruction.GetType().FullName}:{Guid.NewGuid():N}" | ||
| : $"Instruction:{Guid.NewGuid():N}"; | ||
|
|
||
| // Fall back to local execution if we cannot find a network context | ||
| if (context == null || !context.IsSpawned || NetworkManager.Singleton == null) | ||
| { | ||
| Debug.LogWarning( | ||
| $"{LogPrefix} '{instructionName}' running locally because no network context is available", | ||
| contextObject | ||
| ); | ||
|
|
||
| await action(); | ||
| return; | ||
| } | ||
|
|
||
| switch (mode) | ||
| { | ||
| case NetworkExecutionMode.OwnerOnly: | ||
| if (context.IsOwner) | ||
| { | ||
| await action(); | ||
| } | ||
| else | ||
| { | ||
| Debug.LogWarning($"{LogPrefix} Skipping '{instructionName}' because this client is not the owner", contextObject); | ||
| } | ||
|
|
||
| break; | ||
|
|
||
| case NetworkExecutionMode.ServerOnly: | ||
| if (context.IsServer) | ||
| { | ||
| await action(); | ||
| } | ||
| else | ||
| { | ||
| Debug.LogWarning($"{LogPrefix} Skipping '{instructionName}' because this instance is not the server", contextObject); | ||
| } | ||
|
|
||
| break; | ||
|
|
||
| case NetworkExecutionMode.AllClients: | ||
| if (context.IsServer) | ||
| { | ||
| // Execute locally on the server and broadcast to every other client | ||
| await action(); | ||
| context.ExecuteOnAllClientsRpc(instructionName, action); | ||
| } | ||
| else | ||
| { | ||
| Debug.LogWarning($"{LogPrefix} AllClients execution requested from a non-server instance; running locally only", contextObject); | ||
| await action(); | ||
| } | ||
|
|
||
| break; | ||
|
|
||
| case NetworkExecutionMode.ServerAuthority: | ||
| if (context.IsServer) | ||
| { | ||
| await action(); | ||
| context.BroadcastResultToClients(instructionName); | ||
| } | ||
| else | ||
| { | ||
| context.RequestServerExecutionRpc(instructionName, action); | ||
| } | ||
|
|
||
| break; | ||
|
|
||
| default: | ||
| Debug.LogWarning($"{LogPrefix} Unknown network execution mode {mode}, running locally", contextObject); | ||
| await action(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
ExecuteNetworkedregisters the action delegate only on the local process before invoking network RPCs (case NetworkExecutionMode.AllClientshere); the RPC handlers inNetworkInstructionAdapterlook up the delegate in their own static registry (NetworkInstructionAdapter.cs:23-48, 64-82, 89-107), which is empty on other peers. Consequently AllClients runs only on the server and remote clients log “action not found”, and ServerAuthority calls from a client are dropped because the server cannot resolve the action. An instruction such asInstructionNetworkDebugLogexecuted in AllClients/ServerAuthority mode will never execute on remote peers, defeating those modes.Useful? React with 👍 / 👎.