Detect if a 24 hour clock is between a given range

Welcome to our brand new Clickteam Community Hub! We hope you will enjoy using the new features, which we will be further expanding in the coming months.

A few features including Passport are unavailable initially whilst we monitor stability of the new platform, we hope to bring these online very soon. Small issues will crop up following the import from our old system, including some message formatting, translation accuracy and other things.

Thank you for your patience whilst we've worked on this and we look forward to more exciting community developments soon!

Clickteam.
  • I've got a timer that starts at 0 and resets at 24. i.e. a 24 hour clock.

    Now I want some objects to be visible at a given range. For example an object should appear after 8 and hide after 20. This is easy, I can just check if the time is greater than 8 and lower than 20.

    But, if the object should appear after 20 and hide after 8, it becomes a little more complicated. As the timer can be greater than 20, but it is not lower than 8 - not until the clock resets to 0.

    Maybe there is some math formula I can use? Something that returns either a positive or negative value if the time is inside or outside of the range?

  • I just had a thought..

    On a 24 hr clock the hours 20hrs = 8pm so you might need to check your calculations.
    There is no 24th hour as the clock should read 00:00
    Could this be the reason?

    Maybe print out your calculation in a list box or something to see what is happening.
    I don't know if you can use a time calculation based on less than 00:00 as there is nothing smaller than zero but if that doesn't work does 23:59:59 work?

    Regards

    Ross

    Please login to see this link.

  • I've got a timer that starts at 0 and resets at 24. i.e. a 24 hour clock.

    Now I want some objects to be visible at a given range. For example an object should appear after 8 and hide after 20. This is easy, I can just check if the time is greater than 8 and lower than 20.

    But, if the object should appear after 20 and hide after 8, it becomes a little more complicated. As the timer can be greater than 20, but it is not lower than 8 - not until the clock resets to 0.

    Maybe there is some math formula I can use? Something that returns either a positive or negative value if the time is inside or outside of the range?

    If you want to check if the timer (let's call it: x ) is inside or outside the range provided (say 8 -- 20) you can do as follow:

    inside the range: ( x > 8 ) AND ( x < 20 )
    outside the range: ( x < 8 ) OR ( x > 20 )

    I assume you know very well the difference between the logic operators AND and OR, so I apologize if I write now something obvious here: the operator AND requires both conditions to be true, while with the operator OR, you just need one condition to be true.
    That means, x > 8 AND x < 20 requires x greather than 8 AND at the same time x less than 20.
    The condition x < 8 OR x > 20, instead, requires either x less than 8 OR x greather than 20.

    Let's test it. Say we want our object to appear when the timer is inside the range provided. When the timer x is, say, 7, it will indeed be less than 20, but we have an "AND" in our test, that states, that our timer should be also greather than 8. Since the "AND" is not satisfied, our object remains hidden - as wished.
    If the timer is, say, 9, it is greather than 8 AND less than 20, so our object appears - as expected.
    If the timer is, say, 21, it is indeed greather than 8, but it is not less than 20 (condition required from the "AND", thus the object will disappear - as we want.

    The test with the clause "outside the range"works in a similar fashion, but we have an "OR", which makes a big difference, and does exactly what you want:

    Say we want our object appear/desappear outside the range ( 8--20)
    Let's test it.
    When our timer x is, say, 6, it satisfies the rule x < 8 and since the clause in our test is an "OR", the object appears even if x ( in our example x=6 ) is obviously not greather as 20.
    When our timer x is, say, 10, both conditions are not satisfied ( 10 is not less than 8; 10 is not greather than 20 ), so in this case the object disappears - as we want.
    When our timer x is, say, 22, it is not greather than 8 but is greather than 20, ans since our clause is an "OR", our object will reappear, as we want.

    I hope it helps.

    Take care,
    Sergio

  • My suggestion is, just try to separate the test if the timer is in range or outside the range, and set a flag accordingly. Then use that flag in your condition.

    Important: you don't need to use both conditions. Just use the one that you like. Say for example, you use the condition with the "AND", which returns true when the timer is inside the range. Of course, if that condition returns false, means that the timer is outside the range.

    If the condition returns true, set a flag (for example: rangeIsInside) to true. Otherwise set it to false.

    Then use that flag in your logic and you are set.
    I would add two child events to the main event "On Tile Loaded". This way you use that main event once and not twice like in your example.
    One child event checks if the flag rangeIsInside is true, and takes the necessary actions. The other event checks if the flag rangeIsInside is false, and takes the necessary actions.

    I would set an event "Always" before your "On Tile Loaded". In the event "Always" I would check the timer with the provided time range, and set the flag "rangeIsInside" accordingly. The flag "rangeIsInside" can be a flag of an active object, which you could use to store values and flags that are needed in you application (this because global values, as the name suggests, can only store values and not flags).

    After the "Always" event, in the main event "On Tile Loaded" I would use the two child events and check for the flag, as explained above.

    Furthermore, make sure you check two hour values. It seems that currentHour returns, for example, 21, and you check this value against something like "08:00" which could return something wrong. You should check values of the same type - in this case, two integers.

    I hope it makes sense to you :).

    Here is a simplyfied example ( without the "Always" event before the "On Tile Loaded" ) of what I mean. Note the child events of the "On tile loaded" main event.
    Basically what it does:

    On tile loaded
    ----> set the flag timerIsInRange to False
    ---------> child event n. 1
    ------------------> is the time of the tile "in range" ? If true, then set the flag "timerIsInRange" to True.
    ---------> child event n. 2
    -------------------> is the flag timeIsInRange true ?
    ---------------------------------> take the appropriate action. For example, show the tile.
    ---------> child event n. 3
    ---------------------> is the flag timeIsInRange false ?
    ---------------------------------> take the appropriate action. For example, hide the tile.

    Please login to see this attachment.

  • Thanks I forgot I could do child events now ^^

    I don't think I can use an "Always" event, as this check must be run as the map is loaded from an external tile map. Having it run always would probably slow it down by a lot, but maybe I can separate the function as you say. Thanks!

  • I made a simple example here semar Is this what you mean?

    Yes it could work. However:
    as said, you need only one condition check. Simply set the flag to false in a "Always" event, and then make a check if the timer is in the range, and set the flag to true.
    But your version works anyway. For the timer I would use the function "MOD":
    1 MOD 24 returns 1
    2 MOD 24 returns 2
    and so on. But
    24 MOD 24 returns 0.
    This way you don't need the "if time > 24" event. You just set
    counter = ( counter + 1 ) MOD 24

  • Thank you, but not quite sure I understand what the always and MOD would do in this case. If you have time, could you build on the example I added?

    Sure.
    The code is self explanatory. I hope it makes sense for you :)

    P.S.
    One more thing. In your previous attachment, the object2 did not have the flag "ShowObject". Perhaps you have added only to object1, after you cloned it. This way, the flag was only by object1 and not by object2. Check it out !
    If you want to add a flag to more objects at the same time, you have to select them in the frame editor using "shift" or "control + shift". This way you select both objects, and when you add a variable or a flag, it will be added to all the selected objects.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!