playMaker

Author Topic: Countdown Timer  (Read 10470 times)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Countdown Timer
« on: November 24, 2014, 02:27:03 AM »
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: [Select]
// (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);
}
}
}

}
}

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.
« Last Edit: December 02, 2014, 08:15:16 AM by djaydino »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Countdown Timer
« Reply #1 on: November 24, 2014, 03:05:02 AM »
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

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Re: Countdown Timer
« Reply #2 on: November 24, 2014, 03:35:58 AM »
thx, i just added this on the Ecosystem and seems to work :)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Countdown Timer
« Reply #3 on: November 26, 2014, 02:28:54 AM »
Hi,

 yes, perfect :)

 Bye,

 Jean

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Re: Countdown Timer
« Reply #4 on: March 01, 2017, 04:28:09 AM »
Hi,
i made 2 different version for testing, requested by Krillan87

He had an error using this action, when using photon.
Code: [Select]
"Get_realtimeSinceStartup is not allowed to be called during serialization, call it from Awake or Start instead"
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.

Krillan87

  • Beta Group
  • Full Member
  • *
  • Posts: 185
Re: Countdown Timer
« Reply #5 on: March 01, 2017, 05:13:49 AM »
Hi,
i made 2 different version for testing, requested by Krillan87

He had an error using this action, when using photon.
Code: [Select]
"Get_realtimeSinceStartup is not allowed to be called during serialization, call it from Awake or Start instead"
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.

Thank you so much, I really appreciate your effort. The weird thing here is that I still get the error, even when I use the action without realtime.

For a second I thought that maybe there is another action on the game object that is using the "get realtime" so it is not your action that is causing the issue... but I can't seem to find it.

I do not really know how to check where exactly the issue is coming from, the only information unity is giving me is:

"get_realtimeSinceStartup is not allowed to be called during serialization, call it from Awake or Start instead. Called from MonoBehaviour 'PlayMakerFSM' on game object 'PlayerBrain'. See 'Script Serialization' page in the Unity Manual for further details."

When I playtest and instantiate my player the game pauses and this error text appears, the thing is I can just unpause the game and keep playing so maybe this is not a big issue? My coding skills are next to nothing so I'am really know the significance of this error text haha

Once again, thank you for your help.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Re: Countdown Timer
« Reply #6 on: March 01, 2017, 02:05:37 PM »
Hi,
Can you make a video showing this and the complete error in the console.?

On the player you got Fsm's try adding some breakpoints for testing (right click on a state and select set breakpoint
So you can pinpoint when it happens

Krillan87

  • Beta Group
  • Full Member
  • *
  • Posts: 185
Re: Countdown Timer
« Reply #7 on: March 01, 2017, 02:32:27 PM »
Hi,
Can you make a video showing this and the complete error in the console.?

On the player you got Fsm's try adding some breakpoints for testing (right click on a state and select set breakpoint
So you can pinpoint when it happens

Okey I will do that. I have actually never understood what breakpoints are and how to use them. So I will just do a quick read up on it. Iam still investigating that it really is your action that is causing the issue, but it really seems to be the case.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Re: Countdown Timer
« Reply #8 on: March 01, 2017, 06:53:58 PM »
Hi,
Breakpoints will pause when it reaches that state, but the state it pauses on did not run yet when you unpause it will continue

Did you try also without the countdown actions?

Krillan87

  • Beta Group
  • Full Member
  • *
  • Posts: 185
Re: Countdown Timer
« Reply #9 on: March 03, 2017, 07:54:10 AM »
Hi,
Breakpoints will pause when it reaches that state, but the state it pauses on did not run yet when you unpause it will continue

Did you try also without the countdown actions?

hi!
Sorry for no reply, I have had a lot to do these days trying to get photon to work so the test with the countdowntimer has dropped in priority right now because the game is playable even with this error.

I will try find some time to test with breakpoints and examine this issue more in detail, maybe beginning next week okey?

Thanks for you help and enthusiasm :)

Krillan87

  • Beta Group
  • Full Member
  • *
  • Posts: 185
Re: Countdown Timer
« Reply #10 on: March 03, 2017, 09:01:26 AM »
Hi,
Breakpoints will pause when it reaches that state, but the state it pauses on did not run yet when you unpause it will continue

Did you try also without the countdown actions?

Okey I finally found what caused the issue! And I am very embarrassed that I have to admit that it had nothing to do with your actions....  :-\

In the latest state (It was never even active)  there where a "set bool value" on a global variable that does not exist anymore. (I got rid of all my global variables when I realize that I overused them a lot for no good reason.)

This global variable was one of those that gets added automatically by Playmaker when you copy and paste actions around.

Anyway... I so very sorry that I thought it was your action that caused the issue, my coding skills are next to nothing so I did not know how to check all actions before message you.... but I am every so grateful that you took yourself time and tried to help me, and even created new actions for this! <3

Ps. thanks for explaining how the breakpoints work, they help me a lot in narrowing down where the issue where!

« Last Edit: March 03, 2017, 09:03:20 AM by Krillan87 »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Re: Countdown Timer
« Reply #11 on: March 03, 2017, 11:16:22 AM »
Np, Happy to help

Dakk

  • Beta Group
  • Playmaker Newbie
  • *
  • Posts: 21
Re: Countdown Timer
« Reply #12 on: February 19, 2018, 02:40:49 PM »
Thanks for sharing Krillan87, helped me out... 
I have been banging my head with the same issue.  Somehow there was a phantom var populating a field, but that variable didn't exist at all in the FSM or project.  It was probably caused by copying an action.  Pretty sneaky bug though- and the error messages didn't help to figure it out at all and still seem completely unrelated.