how to filter out repetitions for receiving function?

this is probably a more strictly coding logic specific issue, but please bear with me, as i’m not a coder. i’ve often run into a situation where a function or state gets called numerous times in a short space of time. what i would like is the ability to somehow filter repetitions of a state or a function call so only the state change is registered.

let’s say we have some true or false condition we’re monitoring from some fast updating source. could be Update() or something that triggers during an object move. doesn’t matter.

if we monitor the value coming out and stick a T or F in a print statement, we’ll get: TTTTTTTTTTTTTTTTTTTFFFFFFTTTT, or something similar.

if that gets hooked into a function that plays a sound using Play(), the sound will constantly restart every time the state (whether T or F) is triggering the function. what i would like the receiving part to do is get a single T and wait as long as it needs until the F state triggers, then wait as long as it needs until T happens again.

so, assuming that the state does not fluctuate wildly between T or F how do you make this happen? for my current situation i can create workarounds through triggers and such, but i’m often in a situation where i don’t want the function to be triggered on every value update. i’ve tried while(condition == true) and if (condition == true) and those don’t work. they just keep retesting the value.

i’ve tried researching on Answers as it would seem to be basic, but my term (‘fiter out repetitions’) doesn’t seem to show. any help or assistance greatly appreciated.

scott

Simply store the value in a variable

bool prevResult = false, result = false;
void Update(){
	if(Input.GetKeyDown(KeyCode.Space)){
		result = !result;
	}
   	if(result!= prevResult){
          print(result);
   	      prevResult = result;
    }
}

This is pretty simple. You have a result and a previous result. When you press space, result is modified and only if result is not equal to prevResult then you get the printing and prevResult is set to result avoiding teh printing next update

geez - i figured it was something simple like that. that’s very concise. thanks!