Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Action Queue: I need help on the finer details...

Discussion in 'Scripting' started by Bunzaga, Mar 14, 2013.

  1. Bunzaga

    Bunzaga

    Joined:
    Jan 9, 2009
    Posts:
    195
    So after watching Sean Middleditches video about Action Queues, I started to implement my own version of them, but I am running into some snags, and I am hoping people could shed some light on how to iron it all out.

    First, my GAction object class:
    Code (csharp):
    1.  
    2. #pragma strict
    3. public class GAction extends MonoBehaviour{
    4.     public var previous:GAction = null;
    5.     public var next:GAction = null;
    6.    
    7.     public var aList:GActionList = null;
    8.    
    9.     public var blocking:boolean = false;
    10.    
    11.     public var paused:boolean = true;
    12.    
    13.     public var complete:boolean = false;
    14.    
    15.     public var trans:Transform;
    16.    
    17.     public function Init( _go:GameObject ){}
    18.    
    19.     public function OnUpdate( _dt:float ){}
    20.     public function OnFixedUpdate( _fdt:float ){}
    21.    
    22.     public function OnBlocked(){}
    23.     public function OnUnblock(){}
    24.    
    25.     function OnComplete(){}
    26.     function Destroy(){
    27.         aList.Remove(this);
    28.     }
    29. }
    30.  
    Here is the GActionList
    Code (csharp):
    1.  
    2. #pragma strict
    3. public class GActionList extends MonoBehaviour{
    4.     public var first:GAction = null;
    5.     public var last:GAction = null;
    6.     public var go:GameObject = null;
    7.     public function Start(){
    8.         go = gameObject;
    9.     }
    10.     public function Add( _a:GAction ){
    11.         if( null == first ){
    12.             first = last = _a;
    13.             _a.next = _a.previous = null;
    14.         }
    15.         else{
    16.             last.next = _a;
    17.             _a.previous = last;
    18.             _a.next = null;
    19.             last = _a;
    20.         }
    21.         _a.aList = this;
    22.     }
    23.     public function Remove( _a:GAction ){
    24.         if( first == _a ){
    25.             first = first.next;
    26.         }
    27.         if( last == _a){
    28.             last = last.previous;
    29.         }
    30.         if( _a.previous != null ){
    31.             _a.previous.next = _a.next;
    32.         }
    33.         if( _a.next != null ){
    34.             _a.next.previous = _a.previous;
    35.         }
    36.         _a.trans.gameObject.Destroy(_a);
    37.     }
    38.     public function RemoveAll(){
    39.         while( first ){
    40.             var action:GAction = first;
    41.             action.trans.gameObject.Destroy(action);
    42.             first = action.next;
    43.             action.previous = null;
    44.             action.next = null;
    45.         }
    46.         last = null;
    47.     }
    48.     public function Update(){
    49.         var action:GAction = first;
    50.         while( action != null ){
    51.             if(false == action.paused){
    52.                 action.OnUpdate(Time.deltaTime);
    53.                 if(true == action.complete){
    54.                     Remove(action);
    55.                     action.OnComplete();
    56.                 }
    57.                 if(true == action.blocking){
    58.                     break;
    59.                 }
    60.             }
    61.             action = action.next;
    62.         }
    63.     }
    64.     public function FixedUpdate(){
    65.         var action:GAction = first;
    66.         while( action != null ){
    67.             if(false == action.paused){
    68.                 action.OnFixedUpdate(Time.fixedDeltaTime);
    69.                 if(true == action.complete){
    70.                     Remove(action);
    71.                     action.OnComplete();
    72.                 }
    73.                 if( true == action.blocking ){
    74.                     break;
    75.                 }
    76.             }
    77.             action = action.next;
    78.         }
    79.     }
    80. }
    81.  
    So basically I add this GActionList to a game object, and then I can add arbitrary 'GAction' objects to it through script. It all works fairly well, no complaints.

    The problem is, how do I manage this when I add multiple types of actions, which are different in implementation, but similar in functionality?

    For example, the default 'MoveTo' action just takes a position, and moves the user to the position (point and click). Now say I want to implement a 'ChargeAtTarget' action, which should remove any previous 'MoveTo' actions. The 'ChargeAt'Target action would have a cool down timer, take an actual GameObject instead of a position, etc.

    Now say the user used the ChargeAtTarget action, but then tapped elsewhere on the screen, interrupting or cancelling out the Action. How do I setup these conditions and consequences?

    I also am implementing a String based EventSystem, so I am able to Add and Remove event listening along with this...

    Does anyone have any reading material, or examples, even pseudo code, on how to work with a system like this? Essentially, every action the user can do is one or more GAction, such as RotateTowardPosition, MoveToPosition, ChargeToTarget, Axe2HAttack1, etc.
     
    Last edited: Mar 14, 2013
unityunity