官术网_书友最值得收藏!

Wrapping up the messaging system

Congratulations are in order, as we have finally built a fully functional global messaging system that any and all objects can interface with and use it to send messages between one another. A useful feature of this approach is that it is Type-agnostic, meaning that the message senders and listeners do not even need to derive from any particular class in order to interface with the messaging system; it just needs to be a class that provides a message type and a delegate function of the matching function signature, which makes it accessible to both ordinary classes and MonoBehaviours.

As far as benchmarking the MessagingSystem class goes, we will find that it is capable of processing hundreds, if not thousands, of messages in a single frame with minimal CPU overhead (depending on the CPU, of course). The CPU usage is essentially the same, whether one message is being distributed to 100 different listeners or 100 messages are distributed to just one listener. Either way, it costs about the same.

Even if we're predominantly sending messages during UI or gameplay events, this probably has far more power than we need. So, if it does seem to be causing performance problems, then it's far more likely to be caused by what the listener delegates are doing with the message than the messaging system's ability to process those messages.

There are many ways to enhance the messaging system to provide more useful features we may need in the future, as follows:

  • Allow message senders to suggest a delay (in time or frame count) before a message is delivered to its listeners.
  • Allow message listeners to define a priority for how urgently it should receive messages compared to other listeners waiting for the same message type. This is a means for a listener to skip to the front of the queue if it was registered later than other listeners.
  • Implement some safety checks to handle situations where a listener gets added to the list of message listeners for a particular message while a message of that type is still being processed. Currently, C# will throw EnumerationException at us since the delegate list will be changed by AttachListener(), while it is still being iterated through in TriggerEvent().

At this point, we've probably explored the messaging system enough, so these tasks will be left as an academic exercise for you to undertake if you become comfortable using this solution in your games. Let's continue to explore more ways to improve performance through script code.

主站蜘蛛池模板: 白山市| 贵阳市| 三台县| 壤塘县| 通化市| 大连市| 嘉鱼县| 安康市| 永泰县| 城口县| 武强县| 神木县| 南汇区| 綦江县| 卢龙县| 新邵县| 简阳市| 菏泽市| 旺苍县| 安龙县| 浙江省| 儋州市| 聊城市| 万安县| 红安县| 新和县| 文安县| 化德县| 辽宁省| 武义县| 义马市| 闽清县| 临武县| 孙吴县| 韶关市| 南靖县| 临城县| 榆社县| 柯坪县| 岳阳县| 西丰县|