- Home /
A code to reset a variable after x time if another button is not pressed. Help!
Basically what I am trying to do is create a system were a certain variable will reset if one of 4 different buttons is not pressed within a certain time frame.
I was trying to do this with co-routines, but I have been unsuccessful without using the stop all coroutines function, which was fine until my I needed other coroutines for my character. I am struggling to figure out a new way to do this, can anyone help??
Below is my current script using the stop all coroutines function.
Thanks,
void Update()
{
if (Input.GetButtonDown ("Fire State") && !onWall)
{
if (morphCount <= morphLimit && !attacking)
{
morphCount += 1;
FireState ();
StopAllCoroutines ();
StartCoroutine (MorphReset());
}
}
if (Input.GetButtonDown ("Water State") && !onWall)
{
if (morphCount <= morphLimit && !attacking)
{
morphCount += 1;
WaterState ();
StopAllCoroutines ();
StartCoroutine (MorphReset());
}
}
if (Input.GetButtonDown ("Earth State") && grounded)
{
if (morphCount <= morphLimit)
{
morphCount += 1;
EarthState ();
StopAllCoroutines ();
StartCoroutine (MorphReset());
}
}
if (Input.GetButtonDown ("Powerless"))
{
Powerless ();
StopAllCoroutines ();
StartCoroutine (MorphReset());
}
}
IEnumerator MorphReset ()
{
yield return new WaitForSeconds (4);
morphCount = 0;
}
you'll want to create a float variable and check if it's count is less than 0. Then create an else statement below it.
public float frameTime = 1.0f;
void Update ()
{
if (frameTime > 0) {
frameTime -= Time.deltaTime;
}
else{
$$anonymous$$orphCount = 0;
}
Answer by Eric5h5 · Aug 01, 2014 at 04:03 AM
Use StopCoroutine, so you can stop a specific coroutine, rather than using StopAllCoroutines. Either that or use Invoke instead of coroutines.
if (condition) {
CancelInvoke ("SomeFunction");
Invoke ("SomeFunction", invokeTime);
}
Answer by Kiwasi · Aug 01, 2014 at 07:46 AM
You can use stop coroutine to stop a specific coroutine. You can only use StopCoroutine on a function you start with the string version
// This works
StartCoroutine("MorphReset");
StopCoroutine("MorophReset");
//This will not work
StartCoroutine(MorphReset());
StopCoroutine(MorophReset());
Another solution using coroutines is as follows. This is a set and forget method. Use this by setting timeRemaining to 4 (or whatever other number) in place of you StartCoroutine.
private float _timeRemaining;
private float timeRemaining {
set {
if (_timeRemaining <= 0){
_timeRemaining = value;
StartCoroutine($$anonymous$$orphReset());
} else {
_timeRemaining = value;
}
}
get {
return _timeRemaining;
}
}
IEnumerator $$anonymous$$orphReset ()
{
while (timeRemaining>0){
timeRemaining -= Time.DeltaTime;
yield return null;
}
timeRemaining = 0;
morphCount = 0;
}
You could go a step further and have this coroutine start in the set method for morphCount. That way every time you change morphCount it will automatically reset itself.
Thanks Bored$$anonymous$$ormon, that looks like it will do what I want. The main issue that I have with just using StopCoroutine is that it seems as if it will only stop a coroutine started in the same statement. So for example:
if(Input.GetButtonDown("Fire State"))
{
if(morphCount <= morphLimit)
{
morphCount += 1;
FireState();
StopCoroutine ($$anonymous$$orphReset());
StartCoroutine ($$anonymous$$orphReset());
}
}
Lets say I have this code under all 4 states, this will not actually stop the coroutine currently running. The coroutines will just stack on top of each other and morphCount is continuously being reset to 0. Am I just missing something? Because I originally thought that this should have worked, with one stopcoroutine being called before each startcoroutine. It should have had only one morphreset running at any given time. However, this was not the case.
To use stop you must call the coroutine via the string.
Edit: Whoops, just realised I didn't make this clear in my answer, will adjust.
Ah, I see now.
So by calling it through the string it can be cancelled and will not stack. I see, thanks.