Get Time A Bool Has Been True
I have a public bool that I would like to track how long it has been set to "True" Is there a way I can determine from an If statement how long the bool has been active for?
Something like: if (somebool activeTime == ?) Then do something
$$anonymous$$aybe I worded my question wrong, I was looking for some way to track how long the bool had been true for in an if statement I'm using in a coroutine.
The bool changes from false to true based on input from an external input, a motion sensor.
So I was wanting to do something like:
if the bool has been false for something like 20 or 30 seconds then: do something
not sure if that makes more sense?
Answer by srylain · Oct 21, 2016 at 12:49 AM
When you set the bool to true, you can also store the Time.time value at the same time. Time.time is the time in seconds since the game started, so when you need to check how long that bool had been true just compare it to the current Time.time value.
Answer by Itzhak_P · Oct 21, 2016 at 05:28 AM
Hi This is a quick and dirty way ,
private bool _Enabled;
private float TimeFromBoolStart = 0;
public bool yourBool
{
get
{
return _Enabled;
}
set
{
_Enabled = value;
if (_Enabled)
{
TimeFromBoolStart = Time.realtimeSinceStartup;
}
}
}
public float GetTimeSinceBool()
{
if (yourBool)
{
return Time.realtimeSinceStartup - TimeFromBoolStart;
}
else
{
return 0;
}
}
Good Luck Sfc
Answer by Itzhak_P · Oct 21, 2016 at 07:27 AM
For some Reason my older post was Discarded
this is quick and dirty script
private bool _Enabled;
private float TimeFromBoolStart = 0;
public bool yourBool
{
get
{
return _Enabled;
}
set
{
_Enabled = value;
if (_Enabled)
{
TimeFromBoolStart = Time.realtimeSinceStartup;
}
}
}
public float GetTimeSinceBool()
{
if (yourBool)
{
return Time.realtimeSinceStartup - TimeFromBoolStart;
}
else
{
return 0;
}
}
Good luck SFC
Thanks @Itzhak_P, Now would this reset the bool time for when the bool became "false" again?
Answer by Naphier · Oct 21, 2016 at 02:12 AM
This is a great use-case for accessors! These can allow you to hook in other behaviour to when the variable has changed. In this case it would be something like this:
class MyClass
{
private bool _dontSetMeDirectly;
private float setMeDirectlyChangeTime;
public bool setMeDirectly
{
get { return _dontSetMeDirectly;}
set
{
if (value != _dontSetMeDirectly)
setMeDirectlyChangeTime = Time.time;
_dontSetMeDirectly = value;
}
}
void UsageExample()
{
setMeDirectly = true;
Debug.Log("setMeDirectlyChangeTime: " + setMeDirectlyChangeTime);
}
}
Simple don't ever set the value of _dontSetMeDirectly outside of the accessors (get/set). And don't change the setMeDirectlyChangeTime outside of the setter. Play around with accessors (getter/setters) and learn a bit about them, they are awesome.
Thanks @Naphier, This method looks interesting too. How would I access that in something like this where I want to execute the Debug.Log("Looks Like They have left the area") and the rest when the bool has been false for about 20 or 30 seconds? I'm calling my bool: Human$$anonymous$$otionDetected
if ((answerWasGiven == false) && (Human$$anonymous$$otionDetected == false)){
Debug.Log ("Looks like they Have Left the Area.... Lets go back to LOITER");
inIntimidateConvo = false;
yield return new WaitForSeconds (1.0f); //Wait Time
Loiter ();
}
I don't know what Loiter does so I can't get too in depth. Hopefully it resets some states.
So I'd do this like so:
float timeOut = 20f;
bool isCoroutineRunning = false;
void HandleGoToLoiter()
{
if (!answerWasGiven &&
!Human$$anonymous$$otionDetected &&
(Time.time - Human$$anonymous$$otionDetectedTime) > timeOut)
{
if (!isCoroutineRunning)
{
StartCoroutine(GoToLoiter());
}
}
}
IEnumerator GoToLoiter()
{
if (isCoroutineRunning)
yield break;
Debug.Log("Looks like they have left the area, go to loiter in 1 sec");
yield return new WaitForSeconds(1);
Loiter();
isCoroutineRunning = false;
}
Thanks @Naphier, Sorry for the late reply, I was busy as heck working on the voice response part of the project. Um, I'm not sure about the line:
(Time.time - Human$$anonymous$$otionDetectedTime) > timeOut)
$$anonymous$$y Bool For human motion Being detected is: Human$$anonymous$$otionDetected = false;
Should there be a "." between "Human$$anonymous$$otiondetected" and "Time"
Loiter mode is kind of a stand by mode so basically he goes back into loiter if no motion was detected and enough vocal responses were either positive or neutral, I haven't set up a score count for that part yet so I can track how many there have been.
But yeah Loiter does reset some bools back to false so when motion is detected again he goes through the "Query" mode to deter$$anonymous$$e if there is any sort of threat.
Your answer
Follow this Question
Related Questions
how do i say this 1 Answer
Check a bool from a prefab? 0 Answers
How do I check if bool is true from another script? 2 Answers
How to turn off/on a collider using a bool variable from another script 1 Answer
Using Time.deltaTime as a Timer 0 Answers