Example #1
0
        //----------------------------------------------------------------------------------
        // The IsNetworkMessage being optional could make this open to abuse as it allows the consumer to send it.
        // maybe the flags should be made private?
        /// <summary>
        /// Send entity message. The message will get pushed across the network to all slaves if there any available.
        /// </summary>
        /// <param name="szMessageName">Name of message.</param>
        /// <param name="entity">The entity we are sending this message to.</param>
        /// <param name="msgData">Data to sent with the message. (optional)</param>
        /// <param name="IsNetworkMessage">Is this being sent as a network message. (optional)</param>
        /// <returns></returns>
        //----------------------------------------------------------------------------------
        public EntityMsgRslt SendMessage(String szMessageName, Entity entity, object msgData = null, bool IsNetworkMessage = false)
        {
            Rslt rslt = new Rslt();

            //set flags
            rslt |= DynamicStores.Enums.NonAuthoritative;
            rslt |= DynamicStores.Enums.Invalid_Sender;
            rslt |= DynamicStores.Enums.Invalid_Receiver;

            //check entity is valid and authoritative.
            if (IsValid && entity.IsValid && entity.IsAuthoritative())
            {
                //use reflection to get method call.
                MethodInfo methodInfo = null;
                Type       type       = entity.GetType();

                methodInfo = type.GetMethod(szMessageName);

                if (methodInfo == null)
                {
                    //If this method doesn't exist.
                    Console.WriteLine("Message '" + szMessageName + "' does not exist for entity type " + type.ToString());
                    rslt.ClearRslt();
                    rslt |= DynamicStores.Enums.Fail;
                }
                else
                {
                    //pack data into and message header in an object array.
                    Object[] aMsgData = msgData != null ? new Object[2] : new Object[1];
                    aMsgData[0] = this; //This is the sending entity.

                    if (msgData != null)
                    {
                        aMsgData[1] = msgData; //any data sent with it...this can be a struct etc.
                    }
                    methodInfo.Invoke(entity, aMsgData);

                    if (!IsNetworkMessage)
                    {
                        NetworkEngine network = EngineServices.GetSystem <IGameSystems>().Network;
                        //send this through the network.
                        if (network.IsNetworkActive)
                        {
                            //create network packet.
                            NetworkDataPacket packet = new NetworkDataPacket();

                            //set up packet.
                            packet.EntityIDs.SendingEntityID = this.EntityID;
                            packet.EntityIDs.ReceiveEntityID = entity.EntityID;
                            packet.MessageType = MessageType.EntityMessage;
                            packet.SendMethod  = NetworkSendMethod.InOrder;
                            packet.UserData    = msgData;

                            //push message.
                            network.SendMessage(packet);
                        }
                    }

                    rslt.ClearRslt();
                    rslt |= DynamicStores.Enums.Success;
                }
            }

            return(new EntityMsgRslt(this, entity, rslt));
        }