-
-
Notifications
You must be signed in to change notification settings - Fork 20
PredictedModule GetInitialState and Destroyed #55
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 |
|---|---|---|
|
|
@@ -37,6 +37,12 @@ public override string ToString() | |
| return $"State:\n{fullPredictedState.state}"; | ||
| } | ||
|
|
||
| private void ResetStateToInitialState() | ||
| { | ||
| fullPredictedState.prediction.wasOnSimulationStartCalled = false; | ||
| fullPredictedState.state = GetInitialState(); | ||
| } | ||
|
Comment on lines
+40
to
+44
Contributor
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. Dispose the previous full state before resetting to a new initial state.
Suggested fix private void ResetStateToInitialState()
{
+ fullPredictedState.Dispose();
+ fullPredictedState = default;
fullPredictedState.prediction.wasOnSimulationStartCalled = false;
fullPredictedState.state = GetInitialState();
}As per coding guidelines, "State types implementing IPredictedData must also implement IDisposable and IPackedAuto for auto-serialization codegen." Also applies to: 61-69 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
|
|
||
| internal override void OnCoreInitialize() | ||
| { | ||
| var tickRate = predictionManager.tickRate; | ||
|
|
@@ -52,6 +58,16 @@ internal override void OnCoreInitialize() | |
| ); | ||
| } | ||
|
|
||
| protected override void Setup(PredictedIdentity parent, PredictionManager world) | ||
| { | ||
| base.Setup(parent, world); | ||
| ResetStateToInitialState(); | ||
| _history?.Clear(); | ||
| _viewState?.Dispose(); | ||
| _viewState = null; | ||
| _interpolatedState?.Teleport(fullPredictedState.DeepCopy()); | ||
| } | ||
|
|
||
| protected sealed override void UpdateView(float delta) | ||
| { | ||
| if (_interpolatedState == null) return; | ||
|
|
@@ -135,6 +151,13 @@ protected override void Simulate(ulong tick, float delta) | |
| /// </summary> | ||
| protected virtual void SimulationStart() { } | ||
|
|
||
| /// <summary> | ||
| /// Called when the module is first created. | ||
| /// Future updates will come only through Simulate. | ||
| /// </summary> | ||
| /// <returns>The initial state of the module.</returns> | ||
| protected virtual TState GetInitialState() => default; | ||
|
|
||
| /// <summary> | ||
| /// Executes the simulation logic for this module. | ||
| /// Modify the <paramref name="state"/> directly to advance the simulation. Or use the `currentState` | ||
|
|
||
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.
Avoid iterating the live
_moduleslist during destroyed callbacks.If a module disposes itself inside
Destroyed(),_modulesis mutated and later modules can be skipped. Dispatch from a snapshot (or iterate safely against mutation).Suggested fix
private void TriggerModuleDestroyedEvents() { - for (int i = 0; i < _modules.Count; i++) - _modules[i].TriggerDestroyedEvent(); + var modulesSnapshot = new List<PredictedModule>(_modules); + for (int i = 0; i < modulesSnapshot.Count; i++) + modulesSnapshot[i].TriggerDestroyedEvent(); }📝 Committable suggestion
🤖 Prompt for AI Agents