Solved Easy Timer Module C#

xandeturf

Moderator
Moderator
Nov 4, 2015
132
25
28
35
This timer system I made and simply easy to use, if there are bugs or bad written code inform for the correct corrections.
C#:
using System;
using System.Collections.Generic;
using UnityEngine;

namespace TimerEdit
{
    public class TimerEditStart {

        //timer
        private static GameObject LoadTime = new GameObject();

        public static void Init() {
            LoadTime.AddComponent<TimerEvento>();
            UnityEngine.Object.DontDestroyOnLoad(LoadTime);
        }

    }

    public class TimerEvento : MonoBehaviour
    {
        private static Dictionary<float, TimerAction> listTimers = new Dictionary<float, TimerAction>();
        private static List<float> ListCleatTimer = new List<float>();
        public float TimerAtual;
        public float antiCrach = 0;
        public static float timerSync = 0;

        public class TimerAction {
            public Action acao;
            public int repeat;
            public float time;

            public TimerAction(float time_, int repeat_, Action acao_){
                time = time_;
                repeat = repeat_;
                acao = acao_;
            }
        }

        private void Update()
        {
          
            if (listTimers.Count > 0 && UnityEngine.Time.realtimeSinceStartup - antiCrach >= 1)
            {
                //antiCrach - Reduces the milliseconds update rate to seconds
                antiCrach = UnityEngine.Time.realtimeSinceStartup;

                ListCleatTimer.Clear();

                //Validates the timer to activate
                foreach (var checkTime in listTimers) {
                    if (checkTime.Key < UnityEngine.Time.realtimeSinceStartup) {
                        checkTime.Value.acao();
                        ListCleatTimer.Add(checkTime.Key);
                    }
                }

                // clear timers old's
                foreach (var item in ListCleatTimer) {
                    if (listTimers[item].repeat == -1)
                    {
                        AntiExistente(listTimers[item]);
                    }
                    else if (listTimers[item].repeat > 0)
                    {
                        listTimers[item].repeat--;
                        AntiExistente(listTimers[item]);
                    }
                    listTimers.Remove(item);
                }
               
            }

        }

        //Responsible for adding the timers without repeating Key
        public static void AntiExistente(TimerAction callback) {
            timerSync = UnityEngine.Time.realtimeSinceStartup + callback.time;
            if (listTimers.ContainsKey(timerSync))
            {
                callback.time += 0.05f;
                AntiExistente(callback);
            }
            else {
                //debug    UnityEngine.Debug.Log("timer current: " + UnityEngine.Time.realtimeSinceStartup.ToString() + "  timer event: " + timerSync.ToString());
                listTimers.Add(timerSync, callback);
            }
        }

        //Create timer  dalay=seconds  callback=action/function
        public static void Once(float delay, Action callback) {
            AntiExistente(new TimerAction(delay, 0, callback));
        }

        //Create timer with repetitions  dalay=seconds  reps=0(infinit)  callback=action/function
        public static void Repeat(float delay, int reps, Action callback) {
            if (reps == 0) {
                reps = -1;//infinit
            }
            AntiExistente(new TimerAction(delay, reps, callback));
        }

    }
}
how to use
active:
C#:
//active timer in your module
TimerEditStart.Init();
create a timer :
C#:
//Makes an Airdrop fall after 60 seconds
TimerEvento.Once(60f, () => {
      ConsoleSystem.Run("airdrop.drop");
});
create a timer repeat:
C#:
//It does an Airdrop drop after 60 seconds, activating the next automatic timer to drop another airdrop in 60 seconds infinitely.
TimerEvento.Repeat(60f, 0, () => {
      ConsoleSystem.Run("airdrop.drop");
});
 

xandeturf

Moderator
Moderator
Nov 4, 2015
132
25
28
35
Update code.
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace TimerEdit
{
    public class TimerEditStart {

        //timer
        private static GameObject LoadTime = new GameObject();

        //active
        public static void Init() {
            LoadTime.AddComponent<TimerEvento>();
            UnityEngine.Object.DontDestroyOnLoad(LoadTime);
        }

        //desactive
        public static void DeInitialize()
        {
            GameObject.Destroy(LoadTime);
        }
    }

    public class TimerEvento : MonoBehaviour
    {
        private static Dictionary<float, TimerAction> listTimers = new Dictionary<float, TimerAction>();
        private static List<float> ListCleatTimer = new List<float>();
        public float TimerAtual;
        public float antiCrach = 0;
        public static float timerSync = 0;

        public class TimerAction {
            public Action acao;
            public int repeat;
            public float time;

            public TimerAction(float time_, int repeat_, Action acao_){
                time = time_;
                repeat = repeat_;
                acao = acao_;
            }
        }

        private void Update()
        {

            if (listTimers.Count > 0 && UnityEngine.Time.realtimeSinceStartup - antiCrach >= 1)
            {
                //antiCrach - Reduces the milliseconds update rate to seconds
                antiCrach = UnityEngine.Time.realtimeSinceStartup;

                ListCleatTimer.Clear();

                //Validates the timer to activate
                for (int i = 0; i < listTimers.Count; i++)
                {
                    var time = listTimers.ElementAt(i);
                    if (time.Key < UnityEngine.Time.realtimeSinceStartup) {
                        time.Value.acao();

                        if (time.Value.repeat == -1)
                        {
                            AntiExistente(time.Value);
                        }
                        else if (time.Value.repeat > 0)
                        {
                            time.Value.repeat--;
                            AntiExistente(time.Value);
                        }

                        listTimers.Remove(time.Key);
                    }

                }

            }

        }

        //Responsible for adding the timers without repeating Key
        public static void AntiExistente(TimerAction callback) {
            timerSync = UnityEngine.Time.realtimeSinceStartup + callback.time;
            if (listTimers.ContainsKey(timerSync))
            {
                callback.time += 0.05f;
                AntiExistente(callback);
            }
            else {
                //debug    UnityEngine.Debug.Log("timer current: " + UnityEngine.Time.realtimeSinceStartup.ToString() + "  timer event: " + timerSync.ToString());
                listTimers.Add(timerSync, callback);
            }
        }

        //Create timer  dalay=seconds  callback=action/function
        public static void Once(float delay, Action callback) {
            AntiExistente(new TimerAction(delay, 0, callback));
        }

        //Create timer with repetitions  dalay=seconds  reps=0(infinit)  callback=action/function
        public static void Repeat(float delay, int reps, Action callback) {
            if (reps == 0) {
                reps = -1;//infinit
            }
            AntiExistente(new TimerAction(delay, reps, callback));
        }

    }
}
 
  • Like
Reactions: Jakkee

xandeturf

Moderator
Moderator
Nov 4, 2015
132
25
28
35
Update Code.
C#:
using Fougerite;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace TimerEdit
{
    public class TimerEditStart {

        //timer
        private static GameObject LoadTime = new GameObject();

        //active
        public static void Init() {
           
            // clear olds timers actives
            TimerEvento[] timersAtual = UnityEngine.Object.FindObjectsOfType<TimerEvento>();
            foreach (TimerEvento obj2 in timersAtual)
            {
                GameObject.Destroy(obj2);
            }

            LoadTime.AddComponent<TimerEvento>();
            UnityEngine.Object.DontDestroyOnLoad(LoadTime);
        }

        //desactive
        public static void DeInitialize()
        {
            GameObject.Destroy(LoadTime);
        }
    }

    public class TimerEvento : MonoBehaviour
    {
        private static Dictionary<float, TimerAction> listTimers = new Dictionary<float, TimerAction>();
        private static List<float> ListCleatTimer = new List<float>();
        public float TimerAtual;
        public float antiCrach = 0;
        public static float timerSync = 0;

        public class TimerAction {
            public Action acao;
            public int repeat;
            public float time;

            public TimerAction(float time_, int repeat_, Action acao_){
                time = time_;
                repeat = repeat_;
                acao = acao_;
            }
        }

        private void Update()
        {

            if (listTimers.Count > 0 && UnityEngine.Time.realtimeSinceStartup - antiCrach >= 1)
            {
                try
                {
                    //antiCrach - Reduces the milliseconds update rate to seconds
                    antiCrach = UnityEngine.Time.realtimeSinceStartup;

                    ListCleatTimer.Clear();

                    //Validates the timer to activate
                    for (int i = 0; i < listTimers.Count; i++)
                    {
                        var time = listTimers.ElementAt(i);
                        if (time.Key < UnityEngine.Time.realtimeSinceStartup)
                        {
                            time.Value.acao();

                            if (time.Value.repeat == -1)
                            {
                                AntiExistente(time.Value);
                            }
                            else if (time.Value.repeat > 0)
                            {
                                time.Value.repeat--;
                                AntiExistente(time.Value);
                            }

                            listTimers.Remove(time.Key);
                        }

                    }
                }
                catch (Exception ex)
                {
                    Logger.LogDebug("[TimerEvento] Error Update! " + ex);
                }

            }

        }

        //Responsible for adding the timers without repeating Key
        public static void AntiExistente(TimerAction callback) {
            try
            {
                timerSync = UnityEngine.Time.realtimeSinceStartup + callback.time;
                if (listTimers.ContainsKey(timerSync))
                {
                    callback.time += 0.05f;
                    AntiExistente(callback);
                }
                else
                {
                    //debug    UnityEngine.Debug.Log("timer current: " + UnityEngine.Time.realtimeSinceStartup.ToString() + "  timer event: " + timerSync.ToString());
                    listTimers.Add(timerSync, callback);
                }
            }
            catch (Exception ex)
            {
                Logger.LogDebug("[TimerEvento] Error AntiExistente! " + ex);
            }

        }

        //Create timer  dalay=seconds  callback=action/function
        public static void Once(float delay, Action callback) {
            try
            {
                AntiExistente(new TimerAction(delay, 0, callback));
            }
            catch (Exception ex)
            {
                Logger.LogDebug("[TimerEvento] Error Once! " + ex);
            }

        }

        //Create timer with repetitions  dalay=seconds  reps=0(infinit)  callback=action/function
        public static void Repeat(float delay, int reps, Action callback) {
            try
            {
                if (reps == 0)
                {
                    reps = -1;//infinit
                }
                AntiExistente(new TimerAction(delay, reps, callback));
            }
            catch (Exception ex)
            {
                Logger.LogDebug("[TimerEvento] Error Repeat! " + ex);
            }

        }

    }
}
 

xandeturf

Moderator
Moderator
Nov 4, 2015
132
25
28
35
Update code.
C#:
using Fougerite;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

/// <summary>
/// System Timer Easy - version 1.1
/// * Timer Once
/// * Timer Repeat
/// </summary>

namespace TimerEdit
{
    public class TimerEditStart {

        //timer
        private static GameObject LoadTime;

        //active
        public static void Init() {
           
            // clear olds timers actives
            TimerEvento[] timersAtual = UnityEngine.Object.FindObjectsOfType<TimerEvento>();
            foreach (TimerEvento obj2 in timersAtual)
            {
                GameObject.Destroy(obj2);
            }

            LoadTime = new GameObject();
            LoadTime.AddComponent<TimerEvento>();
            UnityEngine.Object.DontDestroyOnLoad(LoadTime);
        }

        //desactive
        public static void DeInitialize()
        {
            TimerEvento.ClearTimer();
            GameObject.Destroy(LoadTime);
        }
    }

    public class TimerEvento : MonoBehaviour
    {
        private static bool WarnBug = false;
        private static Dictionary<float, TimerAction> listTimers = new Dictionary<float, TimerAction>();
        public float TimerAtual;
        public float antiCrach = 0;
        public static float timerSync = 0;

        public class TimerAction {
            public Action acao;
            public int repeat;
            public float time;

            public TimerAction(float time_, int repeat_, Action acao_){
                time = time_;
                repeat = repeat_;
                acao = acao_;
            }
        }

        private void Update()
        {

            if (listTimers.Count > 0 && UnityEngine.Time.realtimeSinceStartup - antiCrach >= 1)
            {
                try
                {
                    if (!WarnBug && listTimers.Count > 1000)
                    {
                        WarnBug = true;
                        Logger.LogDebug("[TimerEvento] Error WarnBug!\n[TimerEvento] Error WarnBug!\n[TimerEvento] Error WarnBug!\n");
                    }

                    //antiCrach - Reduces the milliseconds update rate to seconds
                    antiCrach = UnityEngine.Time.realtimeSinceStartup;

                    //Validates the timer to activate
                    for (int i = 0; i < listTimers.Count; i++)
                    {
                        var time = listTimers.ElementAt(i);
                        if (time.Key < UnityEngine.Time.realtimeSinceStartup)
                        {
                            try
                            {
                                time.Value.acao();
                            }
                            catch (Exception ex)
                            {
                                Logger.LogDebug("[TimerEvento] Error in active Action! " + ex);
                            }
                           

                            if (time.Value.repeat == -1)
                            {
                                AntiExistente(time.Value);
                            }
                            else if (time.Value.repeat > 0)
                            {
                                time.Value.repeat--;
                                AntiExistente(time.Value);
                            }

                            listTimers.Remove(time.Key);
                        }

                    }
                }
                catch (Exception ex)
                {
                    Logger.LogDebug("[TimerEvento] Error Update! " + ex);
                }

            }

        }

        //Responsible for adding the timers without repeating Key
        public static void AntiExistente(TimerAction callback) {
            try
            {
                timerSync = UnityEngine.Time.realtimeSinceStartup + callback.time;
                if (listTimers.ContainsKey(timerSync))
                {
                    callback.time += 0.05001f;
                    AntiExistente(callback);
                }
                else
                {
                    //debug    UnityEngine.Debug.Log("timer current: " + UnityEngine.Time.realtimeSinceStartup.ToString() + "  timer event: " + timerSync.ToString());
                    listTimers.Add(timerSync, callback);
                }
            }
            catch (Exception ex)
            {
                Logger.LogDebug("[TimerEvento] Error AntiExistente! " + ex);
            }

        }

        //Create timer  dalay=seconds  callback=action/function
        public static void Once(float delay, Action callback) {
            try
            {
                AntiExistente(new TimerAction(delay, 0, callback));
            }
            catch (Exception ex)
            {
                Logger.LogDebug("[TimerEvento] Error Once! " + ex);
            }

        }

        //Create timer with repetitions  dalay=seconds  reps=0(infinit)  callback=action/function
        public static void Repeat(float delay, int reps, Action callback) {
            try
            {
                if (reps == 0)
                {
                    reps = -1;//infinit
                }
                AntiExistente(new TimerAction(delay, reps, callback));
            }
            catch (Exception ex)
            {
                Logger.LogDebug("[TimerEvento] Error Repeat! " + ex);
            }

        }

        public static void ClearTimer()
        {
            listTimers.Clear();
        }

    }
}
 
  • Like
Reactions: Jakkee and salva