Mirage icon indicating copy to clipboard operation
Mirage copied to clipboard

Adding LateStart events In NetworkIdentity (OnLateStartServer, OnLateStartClient)

Open lumeriith opened this issue 3 years ago • 2 comments

Problem

If a networked object tries to do something dependent on other networked object in NetworkIdentity.OnStart events, this can go wrong when they're either in the same payload, or in the scene together. The other object might not have spawned when accessed.

Specifically what I ran into is calling other networked behaviour's [Server] marked method, which resulted in following error.

MethodInvocationException: [Server] function 'LoadZone' called when server not active. (GameManager calling ZoneManager's LoadZone)

Current Workaround

I'm planning to use a workaround of waiting a frame before doing something that's dependent on other networked component. It's a very hacky workaround.

Feature Proposal

When we try to do something in a component on the beginning of the lifecycle, we usually do it in Awake(). If that something is dependent on other component, we use Start(). We do initialization of our own component in Awake(), and other other-component-dependent stuff in Start(). By following this logic and explicitly separating two parts of code, we can avoid running into order-of-execution related errors. (Calling some other component's method when the component is not yet ready.)

I propose adding something similar to what Awake() and Start() offers, which is LateStart events. By adding those events we can safely do things that are dependent on other networked components in LateStart, without having to worry about which networked object spawns first.

lumeriith avatar Apr 03 '22 19:04 lumeriith

This is interesting, Might be possible to have a LateStart.. event that is invoked after all message for that frame are processed.

James-Frowen avatar Apr 03 '22 19:04 James-Frowen

This issue is stale because it has been open 120 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar Aug 02 '22 05:08 github-actions[bot]