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. |
||
⚫ | |||
− | 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. |
||
⚫ | |||
+ | VariableTimer.cs:<syntaxhighlight lang="csharp"> |
||
− | VariableTimers.cs: |
||
− | + | using UnityEngine; |
|
⚫ | |||
− | using System.Collections.Generic;<br> |
||
+ | |||
− | using UnityEngine;<br> |
||
⚫ | |||
⚫ | |||
+ | { |
||
− | <br> |
||
+ | |||
⚫ | |||
⚫ | |||
− | {<br> |
||
+ | public Cutscene cutsceneOnHitZero = null; |
||
− | <br> |
||
⚫ | |||
− | public VariableTimer[] timers;<br> |
||
+ | public bool runOnAwake; |
||
⚫ | |||
+ | |||
⚫ | |||
− | + | private float ticker = 1f; |
|
⚫ | |||
− | <br> |
||
⚫ | |||
− | <br> |
||
− | + | private bool doCountdown; |
|
+ | |||
− | {<br> |
||
+ | |||
− | numTimers = timers.Length;<br> |
||
⚫ | |||
− | }<br> |
||
⚫ | |||
− | <br> |
||
+ | doCountdown = runOnAwake; |
||
− | <br> |
||
+ | } |
||
⚫ | |||
+ | |||
− | {<br> |
||
+ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | { |
||
− | {<br> |
||
− | + | doCountdown = true; |
|
+ | } |
||
− | <br> |
||
+ | |||
− | for (i=0; i<numTimers; i++)<br> |
||
+ | |||
⚫ | |||
+ | public void End () |
||
− | timers[i].Update ();<br> |
||
+ | { |
||
⚫ | |||
⚫ | |||
− | }<br> |
||
+ | } |
||
− | }<br> |
||
+ | |||
− | <br> |
||
+ | |||
− | <br> |
||
+ | private void Update () |
||
− | [System.Serializable]<br> |
||
+ | { |
||
− | public class VariableTimer<br> |
||
⚫ | |||
− | {<br> |
||
⚫ | |||
− | <br> |
||
+ | { |
||
⚫ | |||
⚫ | |||
⚫ | |||
+ | |||
⚫ | |||
⚫ | |||
− | <br> |
||
⚫ | |||
− | <br> |
||
⚫ | |||
⚫ | |||
− | + | } |
|
− | + | if (variable == null) |
|
− | + | { |
|
⚫ | |||
⚫ | |||
⚫ | |||
− | if (Variable.IntegerValue == 0 && actionListOnHitZero != null)<br> |
||
+ | |||
⚫ | |||
+ | if (doCountdown && variable != null && variable.IntegerValue > 0) |
||
⚫ | |||
− | + | { |
|
⚫ | |||
⚫ | |||
+ | if (variable.IntegerValue == 0) |
||
− | }<br> |
||
⚫ | |||
− | <br> |
||
+ | doCountdown = false; |
||
− | <br> |
||
+ | |||
⚫ | |||
+ | if (actionListOnHitZero) |
||
− | private GVar Variable<br> |
||
− | + | { |
|
⚫ | |||
− | get<br> |
||
− | + | } |
|
− | + | if (cutsceneOnHitZero) |
|
− | + | { |
|
+ | cutsceneOnHitZero.Interact (); |
||
⚫ | |||
− | + | } |
|
⚫ | |||
⚫ | |||
− | + | } |
|
+ | } |
||
⚫ | |||
+ | } |
||
− | }<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 ();
}
}
}
}
}
}