Turn based battle - pausing inactive characters

I have three playable characters on screen, who each have to wait for their turn based on their waitTurn value. At the moment I’m using yield WaitForSeconds to change each characters state from waiting to active.

The problem I’m having is how to pause other waiting characters once one becomes active. I considered using Time.timeScale = 0 to pause the other characters, but i believe this would pause the entire game so no other action by the active character could be taken.

Is there some way I could achieve this, or a better method to use?

public var waitTime : float;

enum 	LucaState {
		Waiting,
		Engaged,
}

private var state = LucaState.Waiting;

function Waiting( waitTime )
{
	state = LucaState.Waiting;
	
	Debug.Log("Started Waiting");
	
	yield WaitForSeconds ( waitTime );
	
	Debug.Log("Finished Waiting");
	
	state = LucaState.Engaged;
}

function Start()
{
	Waiting ( waitTime ); // run at the start to initiate waiting
}

Your best bet would be created some sort of queue system / class where you can add characters to the end of the queue when the timer is up and grab them off the top of the queue and processes them. Haven’t really done much unity scripting yet, but a simple class with a enqueue and dequeue function would suffice.

Depending on your game you may not even need the timer. For example, a character could be dequeued off the top of your queue and processed, then enqueued back onto the bottom of the queue and the next one dequeued … etc.

If you do need a timer per character, try something like this:

var b_active : Boolean = false;

function Update() {
    if (Time.time > fl_timer_start + fl_wait_time) {
        b_active = true;
    }
}

That’s a REALLY simplified example. Hopefully the variable names make sense. After the character acts, of course, you’ll set b_active = false.

Thanks for the responses.

I’m not sure about adding a queue system, mainly because I wanted other characters to pause once one becomes active. Once that turn is complete, the other characters would continue counting down their wait time. I guess this approach would remove any need for a queue system.

I’ll give the timer in the update function a go. If I have any luck I’ll post it up.

Also do you think having one class to handle the update for all the characters would be the best solution? Or a script for each character with their own update function? Being built for the iPhone I’m not sure if its a good idea to have 2 many update functions running at the same time, though thats just me guessing.

Got it working now! :slight_smile:

enum BattleModeState
{
	AllInactive,
	Activated,
	WaitingForInput,
}

	private var state = BattleModeState.AllInactive;

	private var luca_Active : boolean = false;			
	private var luca_Past_Inactive : boolean = false;	
	private var luca_Carry_Over : float;				
	private var luca_Carry_Over_Store : float; 			
	private var luca_Wait_Time : float;					

	private var phoenix_Active : boolean = false;
	private var phoenix_Past_Inactive : boolean = false;
	private var phoenix_Carry_Over : float;
	private var phoenix_Carry_Over_Store : float;
	private var phoenix_Wait_Time : float;

	private var bridge_Active : boolean = false;
	private var bridge_Past_Inactive : boolean = false;
	private var bridge_Carry_Over : float;
	private var bridge_Carry_Over_Store : float;
	private var bridge_Wait_Time : float;

	private var accumulatingTime : float;
	private var setAccumulatedTime : boolean = true;
	private var accumulatedTime : float;

	private var carryOverTime : float;
	private var activateTime : float;

	function Wait()
	{
		yield WaitForSeconds (3);
	
		switch ( true )
		{
			case ( luca_Active ):		
			luca_Carry_Over = 0.0;
			luca_Carry_Over_Store = 0.0;
			luca_Past_Inactive = false;							
			break;
		
			case ( phoenix_Active ):		
			phoenix_Carry_Over = 0.0;
			phoenix_Carry_Over_Store = 0.0;
			phoenix_Past_Inactive = false;								
			break;
		
			case ( bridge_Active ):		
			bridge_Carry_Over = 0.0;
			bridge_Carry_Over_Store = 0.0;
			bridge_Past_Inactive = false;							
			break;
		}
	
		if ( !luca_Active )
		{
			if ( luca_Past_Inactive )
			{
				luca_Carry_Over_Store = luca_Carry_Over;
				luca_Carry_Over = carryOverTime + luca_Carry_Over_Store;
			}
			else {
				luca_Past_Inactive = true;
				luca_Carry_Over = carryOverTime;
			}
		}
	
		if ( !phoenix_Active )
		{
			if ( phoenix_Past_Inactive )
			{
				phoenix_Carry_Over_Store = phoenix_Carry_Over;
				phoenix_Carry_Over = carryOverTime + phoenix_Carry_Over_Store;
			}
			else {
				phoenix_Past_Inactive = true;
				phoenix_Carry_Over = carryOverTime;
			}
		}
	
		if ( !bridge_Active )
		{
			if ( bridge_Past_Inactive )
			{
				bridge_Carry_Over_Store = bridge_Carry_Over;
				bridge_Carry_Over = carryOverTime + bridge_Carry_Over_Store;
			}
			else {
				bridge_Past_Inactive = true;
				bridge_Carry_Over = carryOverTime;
			}
		}

		luca_Active = false;
		phoenix_Active = false;
		bridge_Active = false;
	
		state = BattleModeState.AllInactive;
	}


	function Update()
	{

		if ( state == BattleModeState.AllInactive )
		{
			accumulatingTime = Time.time;

			Debug.Log (bridge_Carry_Over);
		
			if ( !setAccumulatedTime )
			{
				setAccumulatedTime = true;
				accumulatedTime = accumulatingTime;
			}
		
			if ( !luca_Active  CurrentParty.lucaInParty )
			{
				if ( Time.time > 4.2 + ( accumulatedTime - luca_Carry_Over )) 
					{
						Debug.Log ("Luca Active");
						luca_Active = true;
						state = BattleModeState.Activated;
					}
			}
		
			if ( !phoenix_Active  CurrentParty.phoenixInParty)
			{
				if ( Time.time > 4.9 + ( accumulatedTime - phoenix_Carry_Over ))
					{
						Debug.Log ("Phoenix Active");
						phoenix_Active = true;
						state = BattleModeState.Activated;
					}
			}
		
			if ( !bridge_Active  CurrentParty.bridgeInParty)
			{
				if ( Time.time > 5.4 + ( accumulatedTime - bridge_Carry_Over ))
					{
						Debug.Log ("Bridge Active");
						bridge_Active = true;
						state = BattleModeState.Activated;
					}
			}
		}
		else if ( state == BattleModeState.Activated )
		{

			activateTime = Time.time;
			carryOverTime = activateTime - accumulatedTime;
			setAccumulatedTime = false;
	
			state = BattleModeState.WaitingForInput;
		
			Wait();
		}
	}