@@ -104,16 +104,40 @@ public virtual void OnLostOwnership()
104
104
105
105
}
106
106
107
- protected int RegisterMessageHandler ( string name , Action < int , byte [ ] > action )
107
+ protected void RegisterMessageHandler ( string name , Action < int , byte [ ] > action )
108
108
{
109
+ if ( ! MessageManager . messageTypes . ContainsKey ( name ) )
110
+ {
111
+ Debug . LogWarning ( "MLAPI: The messageType " + name + " is not registered" ) ;
112
+ return ;
113
+ }
114
+ ushort messageType = MessageManager . messageTypes [ name ] ;
115
+ ushort behaviourOrder = networkedObject . GetOrderIndex ( this ) ;
116
+
117
+ if ( ! networkedObject . targetMessageActions . ContainsKey ( behaviourOrder ) )
118
+ networkedObject . targetMessageActions . Add ( behaviourOrder , new Dictionary < ushort , Action < int , byte [ ] > > ( ) ) ;
119
+ if ( networkedObject . targetMessageActions [ behaviourOrder ] . ContainsKey ( messageType ) )
120
+ {
121
+ Debug . LogWarning ( "MLAPI: Each NetworkedBehaviour can only register one callback per instance per message type" ) ;
122
+ return ;
123
+ }
124
+
125
+ networkedObject . targetMessageActions [ behaviourOrder ] . Add ( messageType , action ) ;
109
126
int counter = MessageManager . AddIncomingMessageHandler ( name , action , networkId ) ;
110
127
registeredMessageHandlers . Add ( name , counter ) ;
111
- return counter ;
112
128
}
113
129
114
130
protected void DeregisterMessageHandler ( string name , int counter )
115
131
{
116
132
MessageManager . RemoveIncomingMessageHandler ( name , counter , networkId ) ;
133
+ ushort messageType = MessageManager . messageTypes [ name ] ;
134
+ ushort behaviourOrder = networkedObject . GetOrderIndex ( this ) ;
135
+
136
+ if ( networkedObject . targetMessageActions . ContainsKey ( behaviourOrder ) &&
137
+ networkedObject . targetMessageActions [ behaviourOrder ] . ContainsKey ( messageType ) )
138
+ {
139
+ networkedObject . targetMessageActions [ behaviourOrder ] . Remove ( messageType ) ;
140
+ }
117
141
}
118
142
119
143
private void OnDisable ( )
@@ -619,7 +643,7 @@ protected void SendToServerTarget(string messageType, string channelName, byte[]
619
643
Debug . LogWarning ( "MLAPI: Server can not send messages to server." ) ;
620
644
return ;
621
645
}
622
- NetworkingManager . singleton . Send ( NetworkingManager . singleton . serverClientId , messageType , channelName , data , networkId ) ;
646
+ NetworkingManager . singleton . Send ( NetworkingManager . singleton . serverClientId , messageType , channelName , data , networkId , networkedObject . GetOrderIndex ( this ) ) ;
623
647
}
624
648
625
649
protected void SendToLocalClient ( string messageType , string channelName , byte [ ] data )
@@ -649,7 +673,7 @@ protected void SendToLocalClientTarget(string messageType, string channelName, b
649
673
Debug . LogWarning ( "MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType" ) ;
650
674
return ;
651
675
}
652
- NetworkingManager . singleton . Send ( ownerClientId , messageType , channelName , data , networkId ) ;
676
+ NetworkingManager . singleton . Send ( ownerClientId , messageType , channelName , data , networkId , networkedObject . GetOrderIndex ( this ) ) ;
653
677
}
654
678
655
679
protected void SendToNonLocalClients ( string messageType , string channelName , byte [ ] data )
@@ -664,7 +688,7 @@ protected void SendToNonLocalClients(string messageType, string channelName, byt
664
688
Debug . LogWarning ( "MLAPI: Sending messages from client to other clients is not yet supported" ) ;
665
689
return ;
666
690
}
667
- NetworkingManager . singleton . Send ( messageType , channelName , data , ownerClientId , null ) ;
691
+ NetworkingManager . singleton . Send ( messageType , channelName , data , ownerClientId ) ;
668
692
}
669
693
670
694
protected void SendToNonLocalClientsTarget ( string messageType , string channelName , byte [ ] data )
@@ -679,7 +703,7 @@ protected void SendToNonLocalClientsTarget(string messageType, string channelNam
679
703
Debug . LogWarning ( "MLAPI: Sending messages from client to other clients is not yet supported" ) ;
680
704
return ;
681
705
}
682
- NetworkingManager . singleton . Send ( messageType , channelName , data , ownerClientId , networkId ) ;
706
+ NetworkingManager . singleton . Send ( messageType , channelName , data , ownerClientId , networkId , networkedObject . GetOrderIndex ( this ) ) ;
683
707
}
684
708
685
709
protected void SendToClient ( int clientId , string messageType , string channelName , byte [ ] data )
@@ -709,7 +733,7 @@ protected void SendToClientTarget(int clientId, string messageType, string chann
709
733
Debug . LogWarning ( "MLAPI: Invalid Passthrough send. Ensure AllowPassthroughMessages are turned on and that the MessageType " + messageType + " is registered as a passthroughMessageType" ) ;
710
734
return ;
711
735
}
712
- NetworkingManager . singleton . Send ( clientId , messageType , channelName , data , networkId ) ;
736
+ NetworkingManager . singleton . Send ( clientId , messageType , channelName , data , networkId , networkedObject . GetOrderIndex ( this ) ) ;
713
737
}
714
738
715
739
protected void SendToClients ( int [ ] clientIds , string messageType , string channelName , byte [ ] data )
@@ -739,7 +763,7 @@ protected void SendToClientsTarget(int[] clientIds, string messageType, string c
739
763
Debug . LogWarning ( "MLAPI: Sending messages from client to other clients is not yet supported" ) ;
740
764
return ;
741
765
}
742
- NetworkingManager . singleton . Send ( clientIds , messageType , channelName , data , networkId ) ;
766
+ NetworkingManager . singleton . Send ( clientIds , messageType , channelName , data , networkId , networkedObject . GetOrderIndex ( this ) ) ;
743
767
}
744
768
745
769
protected void SendToClients ( List < int > clientIds , string messageType , string channelName , byte [ ] data )
@@ -769,7 +793,7 @@ protected void SendToClientsTarget(List<int> clientIds, string messageType, stri
769
793
Debug . LogWarning ( "MLAPI: Sending messages from client to other clients is not yet supported" ) ;
770
794
return ;
771
795
}
772
- NetworkingManager . singleton . Send ( clientIds , messageType , channelName , data , networkId ) ;
796
+ NetworkingManager . singleton . Send ( clientIds , messageType , channelName , data , networkId , networkedObject . GetOrderIndex ( this ) ) ;
773
797
}
774
798
775
799
protected void SendToClients ( string messageType , string channelName , byte [ ] data )
@@ -799,7 +823,7 @@ protected void SendToClientsTarget(string messageType, string channelName, byte[
799
823
Debug . LogWarning ( "MLAPI: Sending messages from client to other clients is not yet supported" ) ;
800
824
return ;
801
825
}
802
- NetworkingManager . singleton . Send ( messageType , channelName , data , networkId ) ;
826
+ NetworkingManager . singleton . Send ( messageType , channelName , data , networkId , networkedObject . GetOrderIndex ( this ) ) ;
803
827
}
804
828
805
829
protected NetworkedObject GetNetworkedObject ( uint networkId )
0 commit comments