HasReceivedEvent.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. namespace BehaviorDesigner.Runtime.Tasks
  2. {
  3. [TaskDescription("Returns success as soon as the event specified by eventName has been received.")]
  4. [TaskIcon("{SkinColor}HasReceivedEventIcon.png")]
  5. public class HasReceivedEvent : Conditional
  6. {
  7. [Tooltip("The name of the event to receive")]
  8. public SharedString eventName = "";
  9. [Tooltip("Optionally store the first sent argument")]
  10. [SharedRequired]
  11. public SharedVariable storedValue1;
  12. [Tooltip("Optionally store the second sent argument")]
  13. [SharedRequired]
  14. public SharedVariable storedValue2;
  15. [Tooltip("Optionally store the third sent argument")]
  16. [SharedRequired]
  17. public SharedVariable storedValue3;
  18. private bool eventReceived = false;
  19. private bool registered = false;
  20. public override void OnStart()
  21. {
  22. // Let the behavior tree know that we are interested in receiving the event specified
  23. if (!registered) {
  24. Owner.RegisterEvent(eventName.Value, ReceivedEvent);
  25. Owner.RegisterEvent<object>(eventName.Value, ReceivedEvent);
  26. Owner.RegisterEvent<object, object>(eventName.Value, ReceivedEvent);
  27. Owner.RegisterEvent<object, object, object>(eventName.Value, ReceivedEvent);
  28. registered = true;
  29. }
  30. }
  31. public override TaskStatus OnUpdate()
  32. {
  33. return eventReceived ? TaskStatus.Success : TaskStatus.Failure;
  34. }
  35. public override void OnEnd()
  36. {
  37. if (eventReceived) {
  38. Owner.UnregisterEvent(eventName.Value, ReceivedEvent);
  39. Owner.UnregisterEvent<object>(eventName.Value, ReceivedEvent);
  40. Owner.UnregisterEvent<object, object>(eventName.Value, ReceivedEvent);
  41. Owner.UnregisterEvent<object, object, object>(eventName.Value, ReceivedEvent);
  42. registered = false;
  43. }
  44. eventReceived = false;
  45. }
  46. private void ReceivedEvent()
  47. {
  48. eventReceived = true;
  49. }
  50. private void ReceivedEvent(object arg1)
  51. {
  52. ReceivedEvent();
  53. if (storedValue1 != null && !storedValue1.IsNone) {
  54. storedValue1.SetValue(arg1);
  55. }
  56. }
  57. private void ReceivedEvent(object arg1, object arg2)
  58. {
  59. ReceivedEvent();
  60. if (storedValue1 != null && !storedValue1.IsNone) {
  61. storedValue1.SetValue(arg1);
  62. }
  63. if (storedValue2 != null && !storedValue2.IsNone) {
  64. storedValue2.SetValue(arg2);
  65. }
  66. }
  67. private void ReceivedEvent(object arg1, object arg2, object arg3)
  68. {
  69. ReceivedEvent();
  70. if (storedValue1 != null && !storedValue1.IsNone) {
  71. storedValue1.SetValue(arg1);
  72. }
  73. if (storedValue2 != null && !storedValue2.IsNone) {
  74. storedValue2.SetValue(arg2);
  75. }
  76. if (storedValue3 != null && !storedValue3.IsNone) {
  77. storedValue3.SetValue(arg3);
  78. }
  79. }
  80. public override void OnBehaviorComplete()
  81. {
  82. // Stop receiving the event when the behavior tree is complete
  83. Owner.UnregisterEvent(eventName.Value, ReceivedEvent);
  84. Owner.UnregisterEvent<object>(eventName.Value, ReceivedEvent);
  85. Owner.UnregisterEvent<object, object>(eventName.Value, ReceivedEvent);
  86. Owner.UnregisterEvent<object, object, object>(eventName.Value, ReceivedEvent);
  87. eventReceived = false;
  88. registered = false;
  89. }
  90. public override void OnReset()
  91. {
  92. // Reset the properties back to their original values
  93. eventName = "";
  94. }
  95. }
  96. }