Adventure Creator Wikia
(created article. Alverik)
Tags: apiedit Visual edit
 
Tag: Visual edit
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  +
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.
Note: This is a simple way to create a Global timer behavior to use in your AC game.
 
   
  +
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.
You can create a Global integer Variable to store the number of e.g. seconds that have passed since your game begins.
 
   
  +
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.
Create a new ActionList that's set to '''Run In Background''', and have it run a '''Variable: Set''' Action to increase the integer's value by 1, then use an '''Engine: Wait''' Action to wait 1 second, then connect this action back to the first Action (to create a loop).  If you then assign this as your ActionList on start game (in the Settings Manager), it'll track how many seconds have passed, which you can use to calculate the time.
 
  +
  +
VariableTimer.cs:<syntaxhighlight lang="csharp">
  +
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 ();
  +
}
  +
}
  +
}
  +
}
  +
}
  +
  +
}
  +
</syntaxhighlight>
  +
[[Category:General]]

Latest revision as of 09:57, 10 December 2021

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 ();
					}
				}
			}
		}
	}

}