Advertisement
IsntCo

Untitled

May 24th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. // (c) Jean Fabre, 2011-2013 All rights reserved.
  2. // http://www.fabrejean.net
  3.  
  4. // INSTRUCTIONS
  5. // Drop a PlayMakerArrayList script onto a GameObject, and define a unique name for reference if several PlayMakerArrayList coexists on that GameObject.
  6. // In this Action interface, link that GameObject in "arrayListObject" and input the reference name if defined.
  7. // Note: You can directly reference that GameObject or store it in an Fsm variable or global Fsm variable
  8.  
  9. using UnityEngine;
  10.  
  11. namespace HutongGames.PlayMaker.Actions
  12. {
  13. [ActionCategory("ArrayMaker/ArrayList")]
  14. [Tooltip("Each time this action is called it gets the next item from a PlayMaker ArrayList Proxy component. \n" +
  15. "This lets you quickly loop through all the children of an object to perform actions on them.\n" +
  16. "NOTE: To get to specific item use ArrayListGet instead.")]
  17. public class ArrayListGetNext : ArrayListActions
  18. {
  19. [ActionSection("Set up")]
  20.  
  21. [RequiredField]
  22. [Tooltip("The gameObject with the PlayMaker ArrayList Proxy component")]
  23. [CheckForComponent(typeof(PlayMakerArrayListProxy))]
  24. public FsmOwnerDefault gameObject;
  25.  
  26. [Tooltip("Author defined Reference of the PlayMaker ArrayList Proxy component ( necessary if several component coexists on the same GameObject")]
  27. public FsmString reference;
  28.  
  29. [Tooltip("Set to true to force iterating from the first item. This variable will be set to false as it carries on iterating, force it back to true if you want to renter this action back to the first item.")]
  30. [UIHint(UIHint.Variable)]
  31. public FsmBool reset;
  32.  
  33.  
  34. [Tooltip("From where to start iteration, leave to 0 to start from the beginning")]
  35. public FsmInt startIndex;
  36.  
  37. [Tooltip("When to end iteration, leave to 0 to iterate until the end")]
  38. public FsmInt endIndex;
  39.  
  40. [Tooltip("Event to send to get the next item.")]
  41. public FsmEvent loopEvent;
  42.  
  43. [Tooltip("Event to send when there are no more items.")]
  44. public FsmEvent finishedEvent;
  45.  
  46. [UIHint(UIHint.FsmEvent)]
  47. [Tooltip("The event to trigger if the action fails ( likely and index is out of range exception)")]
  48. public FsmEvent failureEvent;
  49.  
  50.  
  51. [ActionSection("Result")]
  52.  
  53. [UIHint(UIHint.Variable)]
  54. [Tooltip("The current index.")]
  55. public FsmInt currentIndex;
  56.  
  57. [UIHint(UIHint.Variable)]
  58. [Tooltip("The value for the current index.")]
  59. public FsmVar result;
  60.  
  61.  
  62.  
  63. // increment that index as we loop through item
  64. private int nextItemIndex = 0;
  65.  
  66.  
  67. public override void Reset()
  68. {
  69.  
  70. gameObject = null;
  71. reference = null;
  72. startIndex = null;
  73. endIndex = null;
  74.  
  75. reset = null;
  76.  
  77. loopEvent = null;
  78. finishedEvent = null;
  79.  
  80. failureEvent = null;
  81.  
  82. result = null;
  83. currentIndex = null;
  84.  
  85. }
  86.  
  87.  
  88. public override void OnEnter()
  89. {
  90. if (reset.Value)
  91. {
  92. reset.Value = false;
  93. nextItemIndex = 0;
  94. }
  95.  
  96. if (nextItemIndex == 0)
  97. {
  98. if ( ! SetUpArrayListProxyPointer(Fsm.GetOwnerDefaultTarget(gameObject),reference.Value) )
  99. {
  100. Fsm.Event(failureEvent);
  101.  
  102. Finish();
  103. }
  104.  
  105. if (startIndex.Value>0)
  106. {
  107. nextItemIndex = startIndex.Value;
  108. }
  109. }
  110.  
  111. DoGetNextItem();
  112.  
  113. Finish();
  114. }
  115.  
  116.  
  117. void DoGetNextItem()
  118. {
  119.  
  120. // no more children?
  121. // check first to avoid errors.
  122.  
  123. if (nextItemIndex >= proxy.arrayList.Count)
  124. {
  125. nextItemIndex = 0;
  126. Fsm.Event(finishedEvent);
  127. return;
  128. }
  129.  
  130. // get next item
  131.  
  132. GetItemAtIndex();
  133.  
  134.  
  135. // no more items?
  136. // check a second time to avoid process lock and possible infinite loop if the action is called again.
  137. // Practically, this enabled calling again this state and it will start again iterating from the first child.
  138. if (nextItemIndex >= proxy.arrayList.Count)
  139. {
  140. nextItemIndex = 0;
  141. Fsm.Event(finishedEvent);
  142. return;
  143. }
  144.  
  145. if (endIndex.Value>0 && nextItemIndex>= endIndex.Value)
  146. {
  147. nextItemIndex = 0;
  148. Fsm.Event(finishedEvent);
  149. return;
  150. }
  151.  
  152. // iterate the next child
  153. nextItemIndex++;
  154.  
  155. if (loopEvent != null)
  156. {
  157. Fsm.Event(loopEvent);
  158. }
  159. }
  160.  
  161.  
  162. public void GetItemAtIndex(){
  163.  
  164.  
  165. if (! isProxyValid())
  166. {
  167. return;
  168. }
  169.  
  170. if (result.IsNone)
  171. {
  172. return;
  173. }
  174.  
  175. object element = null;
  176.  
  177. currentIndex.Value = nextItemIndex;
  178.  
  179. try{
  180. element = proxy.arrayList[nextItemIndex];
  181. }catch(System.Exception e){
  182. Debug.LogError(e.Message);
  183. Fsm.Event(failureEvent);
  184. return;
  185. }
  186.  
  187. PlayMakerUtils.ApplyValueToFsmVar(Fsm,result,element);
  188.  
  189. }
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement