PlayMaker Updates & Downloads > Share New Actions

Countdown Timer

(1/3) > >>

djaydino:
hi, i made a countdown timer action for one of my projects and i will share it here :)
i don't know if it exist already but i could not find 1 so here it is :


--- Code: ---// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.
/*--- __ECO__ __ACTION__ ---*/

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Time)]
[Tooltip("Countdown from a certain time to a certain time and possible to display time left")]
public class CountdownTimer  : FsmStateAction
{
[RequiredField]
[Tooltip("Time To Countdown from")]
public FsmFloat time;
[Tooltip("Stop timer when this time is reached. Value must be lower than time")]
public FsmFloat stopOn;
public FsmEvent finishEvent;
public bool realTime;
public FsmFloat storeValue;

private float startTime;
private float timer;

public override void Reset()
{
time = 1f;
stopOn = 0f;
finishEvent = null;
realTime = false;
storeValue = null;
}

public override void OnEnter()
{
if (time.Value <= stopOn.Value)
{
Fsm.Event(finishEvent);
Finish();
return;
}

startTime = FsmTime.RealtimeSinceStartup;
timer = 0f;
}

public override void OnUpdate()
{
if (realTime)
{
timer = FsmTime.RealtimeSinceStartup - startTime;
storeValue.Value = time.Value - timer;
}
else
{
timer += Time.deltaTime;
storeValue.Value = time.Value - timer;
}

if (storeValue.Value <= stopOn.Value)
{
Finish();
if (finishEvent != null)
{
Fsm.Event(finishEvent);
}
}
}

}
}

--- End code ---

you can let it countdown to any value and you can store the time left value to convert it to a string.

and should be ready to be added to the Ecosystem.

greets djaydino

Edit : Countdown Timer is now Available on the Ecosystem.

jeanfabre:
Hi,

 Nice, can't wait for you to start contributing to the ecosystem ( sent you a pm, as I need your github username actually, not your email).
 
Bye,

 Jean

djaydino:
thx, i just added this on the Ecosystem and seems to work :)

jeanfabre:
Hi,

 yes, perfect :)

 Bye,

 Jean

djaydino:
Hi,
i made 2 different version for testing, requested by Krillan87

He had an error using this action, when using photon.

--- Code: ---"Get_realtimeSinceStartup is not allowed to be called during serialization, call it from Awake or Start instead"
--- End code ---

In CountdownTimer2 i moved "Get_realtimeSinceStartup" into update with a bool check, but when i use the action on a start state, there is a very small delay :

Original action gets 0, CountdownTimer2 gets 0.003+
Which should be fine in most cases.

I also made a version without the realtime option (CountdownTimerNoRealTime), so you can use that if you don't need realtime.

Realtime is mainly useful like for example, when you use timescale 0 (pausing game)  and you still want the Countdown to continue.

Navigation

[0] Message Index

[#] Next page

Go to full version