Skip to content

refactor!: NetworkBehaviour and NetworkObject NetworkManager instance can now be overriden #762

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

Merged
merged 9 commits into from
Apr 27, 2021

Conversation

TwoTenPvP
Copy link
Contributor

BREAKING CHANGE: Static lifecycle properties in NetworkBehaviour are no longer static

… can now be overriden

BREAKING CHANGE: Static lifecycle properties in NetworkBehaviour are no longer static
Copy link
Collaborator

@NoelStephensUnity NoelStephensUnity left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment on lines 44 to 46
public NetworkManager NetworkManager => NetworkManagerTestOverride != null ? NetworkManagerTestOverride : NetworkManager.Singleton;

internal NetworkManager NetworkManagerTestOverride;
Copy link
Contributor

Choose a reason for hiding this comment

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

we should have #if UNITY_EDITOR || DEVELOPMENT_BUILD surrounding this.
(we wouldn't have NetworkManagerTestOverride in non-Editor, non-Development builds)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not too sure I agree tbh. This is internal behaviour which we might want exposed eventually. I generally prefer not to conditional compile things if we don't have to.

Perhaps renaming it and removing the word "Test" from the field name?

Copy link
Contributor

Choose a reason for hiding this comment

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

we're not there yet.
let's keep this more focused on testing for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're not there yet, but we want to move there as quickly as possible. Part of this epic is to prepare for that future when we are there. IFDefing is going against of the goal outlined in the brief.

Copy link
Contributor

Choose a reason for hiding this comment

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

we're not there yet, today.
we're adding a very small overhead and extra member into release builds but in reality, it doesn't actually have to be there.

when the use-case and implementations in other places started to become more generic, then we can roll it out to other places as well.

Suggested change
public NetworkManager NetworkManager => NetworkManagerTestOverride != null ? NetworkManagerTestOverride : NetworkManager.Singleton;
internal NetworkManager NetworkManagerTestOverride;
#if UNITY_EDITOR || DEVELOPMENT_BUILD
public NetworkManager NetworkManager => NetworkManagerTestOverride != null ? NetworkManagerTestOverride : NetworkManager.Singleton;
internal NetworkManager NetworkManagerTestOverride;
#else
public NetworkManager NetworkManager => NetworkManager.Singleton;
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm looking at the internal gdoc proposal, where did you outline upcoming changes you're referring to? are they somewhere else?

This part:

This partial step is also a clear path forward to remove Singleton dependency fully from the library and allow users to also run multiple NetworkManagers at once.

is what this is doing. Preparing the library for that stage.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, I see. let's have a chat next week about this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've been bitten by #ifdef blocked code enough times to err on the side of not #ifdef gating things when not necessary.

And I would wonder, can we count on DEVELOPMENT_BUILD being set when we're doing testing? What if we're testing a fully production build?

But I would stipulate we decorate this variable however we name it with a comment, something to the effect of "in normal use, NetworkObject's all share the one-and-only-one networking manager. For testing and some anticipated future uses, we allow this networking manager to be overridden so that objects can belong to multiple different NetworkManagers"

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure why this conversation marked as resolved. I'm still not entirely happy about rolling something out over runtime without proper planning, especially if something was targeting testing/debugging use-cases only somehow started to effect runtime code.
please organize an internal meeting and let's try to align on our future plans together.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolving after internal alignment!

Copy link
Contributor

@0xFA11 0xFA11 left a comment

Choose a reason for hiding this comment

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

almost there, only needs minor adjustments :)

}

if (clientId == NetworkManager.Singleton.ServerClientId)
NetworkManager networkManager = networkObjects[0].NetworkManager;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as above, can this not be (down in the loop):

Suggested change
NetworkManager networkManager = networkObjects[0].NetworkManager;
NetworkManager networkManager = networkObjects[i].NetworkManager;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, because of how things gets packed. What we do is:

  1. Get the first NetworkManager
  2. Iterate all NetworkObjects in a safety-check loop to ensure they are all spawned and such. We also check that they all belong to the NetworManager we previouly grabbed.
  3. Send off the packet

// Set override
networkObject.NetworkManagerTestOverride = networkManager;

Debug.Assert(networkObject.NetworkManager == networkManager);
Copy link
Contributor

Choose a reason for hiding this comment

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

Especially if you agree with my above comment, a good test would be to create a list of objects that belong to different NM's and then call NetworkHide / NetworkShow and make sure it works.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not meant to work :)

If you call the static versions of NetworkShow and NetworkHide on objects with different NetworkManagers it should throw an exception.

Copy link
Contributor

@mattwalsh-unity mattwalsh-unity left a comment

Choose a reason for hiding this comment

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

  • Would like the comment in NetworkManagerTestOverride variable
  • per comment, would like to change networkhide / show to not look at the 0th item, then include a test for them

@TwoTenPvP
Copy link
Contributor Author

* Would like the comment in `NetworkManagerTestOverride` variable

* per comment, would like to change networkhide / show to not look at the 0th item, then include a test for them

Comment added to NetworkManagerOwner.

Copy link
Contributor

@0xFA11 0xFA11 left a comment

Choose a reason for hiding this comment

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

🚀

@TwoTenPvP TwoTenPvP force-pushed the singleton-removal-network-object branch from 730fc4d to d77f930 Compare April 27, 2021 11:10
@TwoTenPvP TwoTenPvP force-pushed the singleton-removal-network-object branch from d77f930 to 84cf1c1 Compare April 27, 2021 12:23
@TwoTenPvP TwoTenPvP merged commit ae70d51 into develop Apr 27, 2021
@TwoTenPvP TwoTenPvP deleted the singleton-removal-network-object branch April 27, 2021 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants