1
\$\begingroup\$

What I am trying to do is check if a value has changed from the last frame, while using events. Typically I could use threading to my advantage and have a check at the bottom of Update() to see if a value that is typically changed higher up in Update() was changed or not.

However, I am programming with Events, and I don't rely on Update() in several classes to change data. When an event is published, it makes method calls to all methods subscribing to that event. CameraUpButton() is called by the event manager, not by Update(). ClearUp() is called from Update().

I want to see how long an event has been constantly published for, essentially seeing how long the up button has been held. Checking how long it has been held is the easy part, clearing it once the button is no longer held is the hard part since I do not know how to see when an event stopped publishing (I don't know if that is even possible?). So I'm resorting to making some logic to figure it out for me.

In this case:

 private int up;
 private void CameraUpButton()
 {
     up++;
     Vector3 cameraPos = Camera.main.transform.position;
     cameraPos.y += 0.01f;
     Camera.main.transform.position = cameraPos;
 }

 private void ClearUp()
 {
     some logic to determine
     if up has changed since the last frame
 }

Edit: I can make a Input.GetButtonUp("") event, however I want to see if this can be solved without relying on an event triggered by the button coming up. I would prefer if it can be solved without that reliance.

\$\endgroup\$
5
  • \$\begingroup\$ i don't quite understand how, checking if the button is no longer held down, is hard. you should have 2 booleans, PreviousPressed/Pressed, when PreviousPressed was true and Pressed is false, then the button is released, are you trying to do something different ? \$\endgroup\$
    – dimitris93
    Jan 18 '15 at 23:19
  • \$\begingroup\$ I can set the booleans, but to set pressed to false when the button is not down without having a 2nd event is what I do not know how to do. \$\endgroup\$ Jan 19 '15 at 1:44
  • \$\begingroup\$ now i think i understood, you need a ButtonUp event yea, but why are you programming like this ? this is making everything so much harder \$\endgroup\$
    – dimitris93
    Jan 19 '15 at 1:47
  • \$\begingroup\$ It makes it marginally harder from the start, only if I don't know how to do these things (which is why this is hard). But in the end it makes the game easier to work on and modify later, since no methods in another class or object unnecessarily know about a method or field in a different class or object. For user input, I'm handling it all with events. So I only publish one event and then all subscribing methods are called. Rather than checking for a certain condition and making calls to and from each method. I'm not a pro at this though, so I am trying the best I can. \$\endgroup\$ Jan 19 '15 at 1:51
  • \$\begingroup\$ i see what you are thinking, but if my understanding that you are a beginner is correct i'd recommend you stick with the standard way of writing code... if you think modifying games is hard its probably because you haven't written your classes in a good manner yet. just an opinion \$\endgroup\$
    – dimitris93
    Jan 19 '15 at 1:57
0
\$\begingroup\$

If I am understanding you correctly, you have an event which you have created, and you want to know how long a button is held down for after the event has started.

One way is: You can make two events, one for when the event started (aka CameraUpButton), and one for when the event has finished (aka CameraUpButtonComplete). You can save the time when the event was started in a property (such as a DateTime), and use that to compare when the second event (the 'finished' event) is run.

The other way is to use coroutines. Start a coroutine when the event is run and also make a note at the time it was run (in a DateTime property or similar), which listens for 'the end' of that event each frame, and it reports back (perhaps with an event of its own or a callback of some sort) with how long the button was held down for. This way will require the use of Input.Get~~~ to check each frame if the button is still down or not.

Events typically do not 'spin' (run continually) until the event is completed, so there is no good way on the event itself to keep track of how long the event has been 'activated' for.

In either case, when the first event comes in, it should clear any previously used property that is used to keep track of the length of time the button is held for. So if you started it Now (aka; DateTime.Now), then when it is done compare it to Then (aka; DateTime.Now), and you can get the amount of time that has passed.

If you are talking about Unity 4.6 and it's UI revamp, then what you need is an 'event trigger' component. In the trigger component you can have the Pointer Up event, which you can respond to to check how long the pointer was held down for (using the first method listed above).

Hope that helps!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .