# How are we storing the time when the door last opened?

I’m having problems understanding how we are storing the time when the door was last opened in the first if statement, and not the time when the world was brought up for play. Any help?

``````
if (PressurePlate->IsOverlappingActor(ActorThatOpens))
// If the ActorThatOpens is in the volume

{
OpenDoor();
LastDoorOpenTime = GetWorld()->GetTimeSeconds();
}

// Check if its time to close the door
if (GetWorld()->GetTimeSeconds() - LastDoorOpenTime > DoorCloseDelay)
{
CloseDoor();
}
}
``````

GetTimeSeconds returns time in seconds since world was brought up for play. So if at 3 seconds into the game (when the world begins) then GetTimeSeconds would return 3.f

But if we are storing the time since the world opened for play here:

``````LastDoorOpenTime = GetWorld()->GetTimeSeconds();
``````

How is the next statement telling the door to close after given delay?

``````if (GetWorld()->GetTimeSeconds() - LastDoorOpenTime > DoorCloseDelay)
{
CloseDoor();
}
``````

Looks to me that we are saying:

If (time since world opened - time since world opened) is greater than DoorCloseDelay

Then close the door

Because `LastDoorOpenTime = GetWorld()->GetTimeSeconds();` isn’t continually being called.

Say someone enters the trigger volume 1 second after the world loads then does nothing. LastDoorOpenTime is now at 1, `if (GetWorld()->GetTimeSeconds() - LastDoorOpenTime > DoorCloseDelay)` is indeed `1 - 1 > DoorCloseDelay` to begin with, but 3 seconds later it’s now `3 - 1 > DoorCloseDelay` since `LastDoorOpenTime = GetWorld()->GetTimeSeconds();` was never called again.

You know, I actually understand it now. The time in seconds is being stored as a static number the moment the door opens. So the new if statement is actually saying:

(This moment - That moment > DoorCloseDelay), and is continually doing this until the condition is true, because its in the TickComponent.

Thank you so much! I can see a lot of interesting things being done with such a simple if-statement.

Still doesn’t make any logical sense though. How is it actually delaying to close the door? Where is the actual delay?

All the if statement says is that if the time you leave is greater than 1.0f, then close the door. No where in the actual code does it say to delay the door closing.

I’ve been stuck trying to figure out where the actual delay is for hours. The CloseDoor(); method doesn’t have a delay in it, just an angle to close at. So where is the actual delay? How does it know to delay closing the door?

In Blueprints, you can set a simple Delay node easily. But I don’t see where the actual delay code is for c++. All I see is that the time is > the other time, so close the door. There’s no actual delay in that though.

At this point in the course the delay variable was declared and defined in the header file. Later we moved the whole thing into blueprint

i wrote a little different:

if (GetWorld()->GetTimeSeconds() - LasDoorOpenTime == DoorCloseDelay)
but it didn’t work. why?

Think about what that code is doing and when it’s being called

case is the same and instead of last if statement I wrote:
if (GetWorld()->GetTimeSeconds() - LasDoorOpenTime == DoorCloseDelay)
but it is not work??

Because the time is continually going to go up, when `GetWorld()->GetTimeSeconds - LastDoorTime` is equal to DoorCloseDelay it will be for an instant

`4.97 - 2 == 3` (false)
`4.98 - 2 == 3` (false)
`4.99 - 2 == 3` (false)
`5.0 - 2 == 3` (true)
`5.01 - 2 == 3` (false)

Yes it will be for an instant as you say 5.0 - 2 == 3 (true) and instant is not enough for working if
statement ?

No because this is happening every tick, it will instantly be false again.

outcome your opinion length of one tick is bigger than 1 second therefor this time is not enough for if statement. is not it?

it’s shorter than 1 second. You can see this by logging `GetWorld()->GetTimeSeconds` in the tick function

``````UE_LOG(LogTemp, Warning, TEXT("%f"), GetWorld()->GetTimeSeconds)
``````

When dealing with floats you never want to use `==` anyway as it will be unlikely that they are equal `1.00000001 == 1.000002` for example

thank you DanM