예제 #1
0
	public void OnScriptComplete(InteractionScript script,string error){
		lastScriptExecuted = script;
		lastScriptExecutedTime = Time.time;
//Debug.Log (name+" before dequeuing, count is "+scriptQueue.Count);
		
		// error might tell us this was aborted, how should we handle it ? abort the whole stack ? possibly.
		
//		QueuedScript completedScript = 

		if (scriptStack.Count > 0) scriptStack.Pop();
		// if there is anything still on the stack, updates will start giong to it,
		if (scriptStack.Count > 0) return;
		// if the stack is empty, see if there is a queued script waiting to be executed...
//Q		QueuedScript dequeuedScript = scriptQueue.Dequeue();
		
		foreach (object obj in scriptArray){
			if ((obj as QueuedScript).executing){
				currentScript = null;
				script.queueCount--;
				scriptArray.Remove(obj);
				break;
			}
		}
		
		
//		scriptArray.Remove(currentScript); // we could remove this when we start it running instead of here...
		
		if (scriptArray.Count > 0){
			// here is where we get intelligent about what to do next...for all the queued scripts, find the highest priority one which is ready to run right now.
			
			
			// if there isn't one, schedule update to keep checking until there is.
			
			
			//QueuedScript qs = scriptQueue.Peek();
			QueuedScript qs = FindBestScript(); //scriptArray[0] as QueuedScript; // pick the highest priority ready to run script in the queue...
			if (qs != null){
				qs.executing = true;
				ExecuteScript(qs.script,qs.args,qs.obj);
			}
			else {
				checkForScriptReady=true;
			}
		}
	}
예제 #2
0
	public void AbortAllScripts(){

		if (myOI as TaskCharacter != null) { // only do this for task characters
			myOI.actingInScript = null;
			myOI.reservedForScript = null;
			if ((myOI as TaskCharacter).executingScript != null)
				(myOI as TaskCharacter).executingScript.Cancel();
			(myOI as TaskCharacter).executingScript = null;
		}
		// FlushScriptQueue ();
		scriptArray.Clear ();
		while (scriptStack.Count > 0)
			scriptStack.Pop ();
		if (currentScript != null) // there is an abort method on the script but it doesnt do very much...
			currentScript.script.OnScriptComplete ("abort");
		currentScript = null;

	}
예제 #3
0
	public void QueueScript(InteractionScript script, string args, GameObject obj, int priority=0){
		
		if (script == null){
			Debug.LogError("Tried to queue null script - ignored");
			return;
		}		
		QueuedScript qs = new QueuedScript();
		qs.script = script;
		qs.args = args;
		qs.obj = obj;
		qs.priority = priority;
//Q		scriptQueue.Enqueue(qs);
		scriptArray.Add (qs);
		qs.script.queueCount++;
//Debug.Log (name+" queued "+qs.script.name+" after queuing, count is "+scriptArray.Count);
//		if (scriptArray.Count == 1){
		if (myOI.actingInScript == null && scriptArray.Count == 1){
			if (script.isReadyToRun(obj.GetComponent<BaseObject>(),true)){
				qs.executing=true;
				ExecuteScript(script,args,obj,priority);
			}
			else
			{
				// verbalize an acknowledgement that the order has been recieved 

				checkForScriptReady=true;
			}
		}
		else
		{
			// verbalize an acknowledgement that the order has been recieved 
			
			
			// if the currently running script is cancellable, and its priority is lower than ours,
			// send it a cancel
			if (scriptStack.Count > 0){
				QueuedScript qScr = scriptStack.Peek();
				if (qScr != null && 
					qScr.script.cancellable &&
					qScr.priority < priority){
					qScr.script.Cancel();
				}
			}
		}
	}
예제 #4
0
	public void ExecuteScript( InteractionScript script, string args, GameObject obj, int priority=0){ // this can be called from a scriptedInteraction
		if (script == null){
			Debug.LogError("Tried to execute null script - ignored");
			return;
		}
		QueuedScript qs = new QueuedScript();
		qs.script = script;
		qs.args = args;
		qs.obj = obj;
		qs.priority = priority;
		scriptStack.Push(qs);
		currentScript = qs;

		if (scriptStack.Count > 1)
			Debug.Log (name+" stacked "+qs.script.name+" after stacking, count is "+scriptStack.Count);
		
		script.Execute(this,args,obj); // we always start a script when this is called
	}
예제 #5
0
    public void PutMessage( GameMsg msg ) 
    {
		InteractMsg imsg = msg as InteractMsg;
		
		if (imsg != null) // handle special messages to abort or flush script queue
		{
			if (imsg.map != null){
				if (imsg.map.item == "SCRIPT:ABORT"){
					if (scriptStack.Count > 0){
						scriptStack.Peek().script.Abort();
					}
					return;
				}
				if (imsg.map.item == "SCRIPT:QUEUE:FLUSH"){
					if (scriptStack.Count > 0){
						scriptStack.Peek().script.Abort();
					}
					//The OnComplete from this abort is likely to start up any stacked or queued scripts.
					// this really isn't very clean, as there might be stacked scripts which will not get a clean
					// shutdown opportunity, we should probably abort at each level of the stack before clearing all.
					
					// for now, just clear everything after aborting the current script. That should get Zoll going.
					scriptStack.Clear();
					scriptArray.Clear ();
					currentScript = null;
					if ( myOI != null )
						myOI.actingInScript = null;
					return;
				}
			}
			
//UnityEngine.Debug.Log("Scripted Object "+name+" received interact message "+imsg.map.item);
			// see if this message should trigger any of our scripts...
			InteractionScript s = TriggeredScript(imsg.map.item,myOI as BaseObject);
			if (s != null){
				string args = "trigger="+imsg.map.item;
				int priority=s.startPriority;
				if (imsg.map.param != null){
					for (int i=0;i<imsg.map.param.Count; i++){
						if (imsg.map.param[i].Contains("priority")){
							string[] kv = imsg.map.param[i].Split('=');
							int.TryParse(kv[1],out priority);									
						}
						else
						{
							args += " "+imsg.map.param[i];	
						}
					}
				}	
				QueueScript( s,args, gameObject,priority);  	
			}
			return;
		}
		
		InteractStatusMsg ismsg = msg as InteractStatusMsg;
		if (ismsg != null)
		{
			// see if this message should trigger any of our scripts...
			InteractionScript s = TriggeredScript(ismsg.InteractName,null);
			if (s != null){
				string args = "trigger="+ismsg.InteractName;
				int priority=4;
				if (ismsg.Params != null){ 
					foreach (string p in ismsg.Params){
						if (p.Contains("priority")){
							string[] kv = p.Split('=');
							int.TryParse(kv[1],out priority);									
						}
						else
						{
							args += " "+p;	
						}	
					}
				}
				QueueScript( s,args, gameObject,priority); 	
			}
//UnityEngine.Debug.Log("Scripted Object "+name+" received interact status message "+ismsg.InteractName);
		}
    }