- Unity 2017 Game Optimization(Second Edition)
- Chris Dickinson
- 373字
- 2021-07-02 23:21:09
Registration
The second and third requirements can be achieved by offering some public methods that allow registration with the Messaging System. If we force the listening object to provide us a delegate function to call when the message is broadcast, then this allows listeners to customize which method is called for which message. We can make our codebase very easy to understand if we name the delegate after the message it is intended to process.
In some cases, we might wish to broadcast a general notification message and have all listeners do something in response, such as an Enemy Created message. Other times, we might be sending a message that specifically targets a single listener among a group. For example, we might want to send an Enemy Health Value Changed message that is intended for a specific Health Bar object that is attached to the enemy that was damaged. However, we may have many health bar objects in the Scene, all of which are interested in this message type, but each is only interested in hearing health update messages for the enemy they're providing health information for. So, if we implement a way for the system to stop checking after it has been handled, then we can probably save a good number of CPU cycles when there are many listeners waiting for the same message type.
The delegate we define should, therefore, provide a way to retrieve the message via an argument and return a response that determines whether or not processing for the message should stop if and when the listener is done with it. The decision on whether to stop processing or not can be achieved by returning a simple Boolean, where true implies that this listener has handled the message and the processing for the message must stop, and false implies that this listener has not handled the message and the Messaging System should try the next listener.
Here is the definition for the delegate:
public delegate bool MessageHandlerDelegate(Message message);
Listeners must define a method of this form and pass a delegate reference to the Messaging System during registration, thus providing a means for the Messaging System to tell the listening object when the message is being broadcast.
- JavaScript 從入門到項目實踐(超值版)
- Java異步編程實戰(zhàn)
- LabVIEW入門與實戰(zhàn)開發(fā)100例
- Python 深度學(xué)習(xí)
- Cocos2d-x游戲開發(fā):手把手教你Lua語言的編程方法
- Internet of Things with the Arduino Yún
- 面向?qū)ο蟪绦蛟O(shè)計(Java版)
- OpenShift在企業(yè)中的實踐:PaaS DevOps微服務(wù)(第2版)
- SQL Server 2016數(shù)據(jù)庫應(yīng)用與開發(fā)習(xí)題解答與上機(jī)指導(dǎo)
- 青少年學(xué)Python(第1冊)
- OpenStack Orchestration
- Mastering Unity 2D Game Development(Second Edition)
- ElasticSearch Cookbook(Second Edition)
- C++ Fundamentals
- Mastering Elixir