Adventure Creator Wikia
mNo edit summary
Tag: Visual edit
No edit summary
Tag: Visual edit
 
(One intermediate revision by the same user 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.
 
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.
Doing so in this script is more efficient than running an ActionList, as multiple timers can be created in the same component.
 
   
  +
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.
To use it, paste the following code into a C# script named VariableTimers.cs, and add the Variable Timer component to your scene. Configure its Inspector to create new timers by assigning the ID number of the variable you wish to countdown.
 
   
  +
VariableTimer.cs:<syntaxhighlight lang="csharp">
VariableTimers.cs:
 
using System.Collections;<br>
+
using UnityEngine;
 
using AC;
using System.Collections.Generic;<br>
 
  +
using UnityEngine;<br>
 
 
public class VariableTimer : MonoBehaviour
using AC;<br>
 
  +
{
<br>
 
  +
public class VariableTimers : MonoBehaviour<br>
 
 
public int globalIntegerVariableID;
{<br>
 
  +
public Cutscene cutsceneOnHitZero = null;
<br>
 
 
public ActionListAsset actionListOnHitZero = null;
public VariableTimer[] timers;<br>
 
  +
public bool runOnAwake;
private float ticker = 1f;<br>
 
  +
private int numTimers;<br>
 
private int i;<br>
+
private float ticker = 1f;
 
private int i;
<br>
 
 
private GVar variable;
<br>
 
private void Awake ()<br>
+
private bool doCountdown;
  +
{<br>
 
  +
numTimers = timers.Length;<br>
 
 
private void Awake ()
}<br>
 
 
{
<br>
 
  +
doCountdown = runOnAwake;
<br>
 
  +
}
private void Update ()<br>
 
  +
{<br>
 
  +
ticker -= Time.deltaTime;<br>
 
 
public void Begin ()
if (ticker <= 0f)<br>
 
  +
{
{<br>
 
ticker = 1f;<br>
+
doCountdown = true;
  +
}
<br>
 
  +
for (i=0; i<numTimers; i++)<br>
 
  +
{<br>
 
  +
public void End ()
timers[i].Update ();<br>
 
  +
{
}<br>
 
 
doCountdown = false;
}<br>
 
  +
}
}<br>
 
  +
<br>
 
  +
<br>
 
  +
private void Update ()
[System.Serializable]<br>
 
  +
{
public class VariableTimer<br>
 
 
ticker -= Time.deltaTime;
{<br>
 
 
if (ticker <= 0f)
<br>
 
  +
{
public int globalIntegerVariableID;<br>
 
 
ticker = 1f;
public bool doCountdown = true;<br>
 
  +
public ActionListAsset actionListOnHitZero = null;<br>
 
 
if (variable == null)
<br>
 
 
{
<br>
 
 
variable = GlobalVariables.GetVariable (globalIntegerVariableID);
public void Update ()<br>
 
{<br>
+
}
if (doCountdown && Variable != null && Variable.IntegerValue > 0)<br>
+
if (variable == null)
{<br>
+
{
 
Debug.LogWarning ("AC Global Variable with ID=" + globalIntegerVariableID + " not found!");
Variable.IntegerValue --;<br>
 
 
}
if (Variable.IntegerValue == 0 && actionListOnHitZero != null)<br>
 
  +
{<br>
 
  +
if (doCountdown && variable != null && variable.IntegerValue > 0)
actionListOnHitZero.Interact ();<br>
 
}<br>
+
{
 
variable.IntegerValue --;
}<br>
 
  +
if (variable.IntegerValue == 0)
}<br>
 
 
{
<br>
 
  +
doCountdown = false;
<br>
 
  +
private GVar variable;<br>
 
  +
if (actionListOnHitZero)
private GVar Variable<br>
 
{<br>
+
{
 
actionListOnHitZero.Interact ();
get<br>
 
{<br>
+
}
if (variable == null)<br>
+
if (cutsceneOnHitZero)
{<br>
+
{
  +
cutsceneOnHitZero.Interact ();
variable = GlobalVariables.GetVariable (globalIntegerVariableID);<br>
 
}<br>
+
}
 
}
if (variable == null)<br>
 
{<br>
+
}
  +
}
Debug.LogWarning ("AC Global Variable with ID=" + globalIntegerVariableID + " not found!");<br>
 
  +
}
}<br>
 
  +
return variable;<br>
 
  +
}
}<br>
 
  +
</syntaxhighlight>
}<br>
 
<br>
 
}<br>
 
<br>
 
}
 
 
[[Category:General]]
 
[[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 ();
					}
				}
			}
		}
	}

}