Skip to content

PredictedModule GetInitialState and Destroyed - #55

Merged
BlenMiner merged 1 commit into
PurrNet:devfrom
ashtonmeuser:module-lifecycle
Jun 11, 2026
Merged

PredictedModule GetInitialState and Destroyed#55
BlenMiner merged 1 commit into
PurrNet:devfrom
ashtonmeuser:module-lifecycle

Conversation

@ashtonmeuser

@ashtonmeuser ashtonmeuser commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Lifecycle events mirror PredictedIdentity.


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.

Summary by CodeRabbit

  • New Features
    • Added module lifecycle hooks for cleanup and despawn handling
    • Introduced configurable initial state setup for module instances
    • Enhanced module event dispatch to ensure proper cleanup sequencing during destruction

Lifecycle events mirror PredictedIdentity
@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds module lifecycle management to PurrDiction. Modules gain a protected virtual Destroyed() hook for cleanup logic, wired into identity destruction through a new dispatcher that calls destroyed events on all modules when the identity is torn down. Stateful modules can override GetInitialState() to provide initial state and reset state on respawn via a new Setup() override.

Changes

Module lifecycle management

Layer / File(s) Summary
Module destruction contract and dispatcher
Assets/PurrDiction/Runtime/Core/PredictedModule.cs, Assets/PurrDiction/Runtime/Core/PredictedIdentity.Module.cs
PredictedModule adds protected virtual Destroyed() hook and internal TriggerDestroyedEvent() method. PredictedIdentity adds TriggerModuleDestroyedEvents() helper to iterate all attached modules and trigger their destroyed events.
Identity destruction wiring
Assets/PurrDiction/Runtime/Core/PredictedIdentity.cs
Both TriggerDestroyedEvent() and OnDestroy() call TriggerModuleDestroyedEvents() after the identity's own Destroyed() event, ensuring module cleanup events fire during identity teardown.
Stateful module setup and state reset
Assets/PurrDiction/Runtime/Core/PredictedModuleState.cs
Introduces Setup() override that resets predicted state, clears prediction history, and reinitializes view-state. New GetInitialState() hook and ResetStateToInitialState() helper provide module-supplied initial state on respawn.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • PurrNet/PurrDiction#46: Adds module iteration/inspection capability that complements this PR's module event dispatching and lifecycle wiring.

Suggested labels

released on @dev``

Poem

🐰 A module's farewell, cleanly dispatched,
Destroyed() hooks now perfectly matched,
State resets fresh when identity respawns,
Lifecycle flows like morning's dawn,
Lifecycle management hops along! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the two main additions: the GetInitialState hook and the Destroyed lifecycle event introduced for PredictedModule.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Assets/PurrDiction/Runtime/Core/PredictedIdentity.Module.cs`:
- Around line 319-323: TriggerModuleDestroyedEvents currently iterates the live
_modules list and calls each module's TriggerDestroyedEvent, which can mutate
_modules (modules disposing themselves) and cause later modules to be skipped;
fix by iterating a stable snapshot of the collection (e.g., copy _modules to an
array or List first) and invoke TriggerDestroyedEvent on the snapshot so
mutations during callbacks do not affect which modules get destroyed. Ensure you
reference TriggerModuleDestroyedEvents, _modules, and TriggerDestroyedEvent when
applying the change.

In `@Assets/PurrDiction/Runtime/Core/PredictedModuleState.cs`:
- Around line 40-44: ResetStateToInitialState currently overwrites
fullPredictedState.state without disposing the existing state, risking resource
leaks; before assigning fullPredictedState.state = GetInitialState() (and
similarly in the other reset method around the same area), call Dispose() on the
existing fullPredictedState.state if it implements IDisposable (or cast to
IDisposable) to release resources, then assign the new initial state; ensure
state types implementing IPredictedData also implement IDisposable and
IPackedAuto as required by codegen.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 78544d3d-c178-4423-8483-e4043d819e96

📥 Commits

Reviewing files that changed from the base of the PR and between 09f2b5a and 617b9e2.

📒 Files selected for processing (4)
  • Assets/PurrDiction/Runtime/Core/PredictedIdentity.Module.cs
  • Assets/PurrDiction/Runtime/Core/PredictedIdentity.cs
  • Assets/PurrDiction/Runtime/Core/PredictedModule.cs
  • Assets/PurrDiction/Runtime/Core/PredictedModuleState.cs

Comment on lines +319 to +323
private void TriggerModuleDestroyedEvents()
{
for (int i = 0; i < _modules.Count; i++)
_modules[i].TriggerDestroyedEvent();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Avoid iterating the live _modules list during destroyed callbacks.

If a module disposes itself inside Destroyed(), _modules is 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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private void TriggerModuleDestroyedEvents()
{
for (int i = 0; i < _modules.Count; i++)
_modules[i].TriggerDestroyedEvent();
}
private void TriggerModuleDestroyedEvents()
{
var modulesSnapshot = new List<PredictedModule>(_modules);
for (int i = 0; i < modulesSnapshot.Count; i++)
modulesSnapshot[i].TriggerDestroyedEvent();
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Assets/PurrDiction/Runtime/Core/PredictedIdentity.Module.cs` around lines 319
- 323, TriggerModuleDestroyedEvents currently iterates the live _modules list
and calls each module's TriggerDestroyedEvent, which can mutate _modules
(modules disposing themselves) and cause later modules to be skipped; fix by
iterating a stable snapshot of the collection (e.g., copy _modules to an array
or List first) and invoke TriggerDestroyedEvent on the snapshot so mutations
during callbacks do not affect which modules get destroyed. Ensure you reference
TriggerModuleDestroyedEvents, _modules, and TriggerDestroyedEvent when applying
the change.

Comment on lines +40 to +44
private void ResetStateToInitialState()
{
fullPredictedState.prediction.wasOnSimulationStartCalled = false;
fullPredictedState.state = GetInitialState();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Dispose the previous full state before resetting to a new initial state.

ResetStateToInitialState() overwrites fullPredictedState.state during setup without disposing the prior value, which can leak resources across respawns.

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 Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Assets/PurrDiction/Runtime/Core/PredictedModuleState.cs` around lines 40 -
44, ResetStateToInitialState currently overwrites fullPredictedState.state
without disposing the existing state, risking resource leaks; before assigning
fullPredictedState.state = GetInitialState() (and similarly in the other reset
method around the same area), call Dispose() on the existing
fullPredictedState.state if it implements IDisposable (or cast to IDisposable)
to release resources, then assign the new initial state; ensure state types
implementing IPredictedData also implement IDisposable and IPackedAuto as
required by codegen.

Source: Coding guidelines

@BlenMiner
BlenMiner merged commit c590ae1 into PurrNet:dev Jun 11, 2026
1 of 3 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.3.0-beta.36 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.3.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants