BeardedManStudios / ForgeNetworkingRemastered

See various forks, also please join the Forge Community fork of Forge Alloy! -> https://github.com/ForgeAlloyCommunity/ForgeAlloy
https://twitter.com/FarrisFaulds
Apache License 2.0
1.49k stars 309 forks source link

Added the option to register rpc using the same approach as the NCW #341

Closed tommohawkaction closed 4 years ago

tommohawkaction commented 4 years ago

An example of this is in the SomeNetworkObject.cs used for additive scenes There is a new function which you can override in a NetworkBehaviour called RegisterCustomRPCs(NetworkObject)

This is called before NetworkStart so the behaviour.networkObject will still be null at this time hence why it has a argument NetworkObject which is a valid NetworkObject

Inside the RegisterCustomRPCs you can register RPCs like you normally would except you have can register the RPC and you'll get the id back

for example

` private byte RPC_SERVER_TEST_RPC;

protected override void RegisterCustomRPCs(NetworkObject networkObject)
{
    base.RegisterCustomRPCs(networkObject);

    RPC_SERVER_TEST_RPC = networkObject.RegisterRpc("ServerTestRPC", ServerTestRPC, typeof(int), typeof(string));
}

`

then like then you can use the RPC like you normally would

Aside from that you can also register rpcs from another MonoBehaviour its completely the same as hooking into the

networkBehaviour.networkStarted event but instead we have a custom one for registering rpcs which is networkBehaviour.registerCustomRPCs its completely the same as the parent one and is invoked just after RegisterCustomRPCs(NetworkObject networkObject)


For as testing goes, I use this in Wobbly Life and works for very complicated NetworkBehaviours with multiple Monobehaviours registering rpcs

For testing in the current branch, I've added some example code in SomeNetworkObject.cs