def on_message_received(self, msg: IncomingMessage) -> None:
     """
     Note: We must implement this method of the SideChannel interface to
     receive messages from Unity
     """
     # We simply read a string from the message and print it.
     self.last_msg = msg.read_string()
Example #2
0
 def _parse_side_channel_message(side_channels: Dict[uuid.UUID,
                                                     SideChannel],
                                 data: bytes) -> None:
     offset = 0
     while offset < len(data):
         try:
             channel_id = uuid.UUID(bytes_le=bytes(data[offset:offset +
                                                        16]))
             offset += 16
             message_len, = struct.unpack_from("<i", data, offset)
             offset = offset + 4
             message_data = data[offset:offset + message_len]
             offset = offset + message_len
         except Exception:
             raise UnityEnvironmentException(
                 "There was a problem reading a message in a SideChannel. "
                 "Please make sure the version of MLAgents in Unity is "
                 "compatible with the Python version.")
         if len(message_data) != message_len:
             raise UnityEnvironmentException(
                 "The message received by the side channel {0} was "
                 "unexpectedly short. Make sure your Unity Environment "
                 "sending side channel data properly.".format(channel_id))
         if channel_id in side_channels:
             incoming_message = IncomingMessage(message_data)
             side_channels[channel_id].on_message_received(incoming_message)
         else:
             logger.warning(
                 "Unknown side channel data received. Channel type "
                 ": {0}.".format(channel_id))
Example #3
0
 def on_message_received(self, msg: IncomingMessage):
     print(msg.read_string())