how to tell the frame in which a boolean becomes true?

Ok, here’s my problem.
I’m building a custom input management class to substitute/integrate unity’s built-in Input Manager.

I’m stuck at a point when i’m trying to mimick unity’s GetButtonDown method, that returns true in the only frame in which GetButton becomes true.

I tried to do something like this (C#):

[...]
bool last = false;
	bool TestFunction(bool truth)
	{
		bool result = !last  truth;
		last = truth;
		return result;
	}
[...]

But this has some disadvantages:

  • It only works if you call it in a Update method
  • It requires constant input polling
  • If more than one call per frame is made the result gets messed up.

Do you have any better ideas for implementing this?

I would use properties instead of messing with an ugly perma polling solution

How would it be any different using a property instead of a method?

bump …

I think the idea of properties is in relation to having an input change call a specific function. This would work internally quite well, but would likely fall apart if you were doing some sort of input system.

The basic idea is

bool myVal;

public bool MyVal
{
Set
{
//Call function here that needs to only happen on a state change
myVal = value;
}
}

You could possibly use that to set a justPressed bool to true that is reset to false at the beginning of each frame, although you may have to fight with update order.