Adventure Creator Wikia
Advertisement

The following script can be used to automatically reduce a Global Integer variable's value by 1 every second, and run an ActionList asset when it hits zero.

To use it, paste the following code into a C# script named VariableTimer.cs, and add the Variable Timer component to your scene. Configure its Inspector to create a new timer by assigning the ID number of the variable you wish to countdown.

To have the timer begin, either check "Run On Awake", or use either the "Object: Call event" or "Object: Send message" Actions to call the component's "Begin" function.

VariableTimer.cs:

using UnityEngine;
using AC;

public class VariableTimer : MonoBehaviour
{

	public int globalIntegerVariableID;
	public Cutscene cutsceneOnHitZero = null;
	public ActionListAsset actionListOnHitZero = null;
	public bool runOnAwake;

	private float ticker = 1f;
	private int i;
	private GVar variable;
	private bool doCountdown;


	private void Awake ()
	{
		doCountdown = runOnAwake;
	}


	public void Begin ()
	{
		doCountdown = true;
	}


	public void End ()
	{
		doCountdown = false;
	}


	private void Update ()
	{
		ticker -= Time.deltaTime;
		if (ticker <= 0f)
		{
			ticker = 1f;

			if (variable == null)
			{
				variable = GlobalVariables.GetVariable (globalIntegerVariableID);
			}
			if (variable == null)
			{
				Debug.LogWarning ("AC Global Variable with ID=" + globalIntegerVariableID + " not found!");
			}

			if (doCountdown && variable != null && variable.IntegerValue > 0)
			{
				variable.IntegerValue --;
				if (variable.IntegerValue == 0)
				{
					doCountdown = false;

					if (actionListOnHitZero)
					{
						actionListOnHitZero.Interact ();
					}
					if (cutsceneOnHitZero)
					{
						cutsceneOnHitZero.Interact ();
					}
				}
			}
		}
	}

}
Advertisement