# Adding Delay to Event

**URL:** https://forum.castle-engine.io/t/adding-delay-to-event/359
**Category:** Uncategorized
**Created:** [June 27, 2021, 9:37am UTC](https://forum.castle-engine.io/t/adding-delay-to-event/359 "2021-06-27T09:37:43Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)
#### Post date: [June 27, 2021, 9:37am UTC](https://forum.castle-engine.io/t/adding-delay-to-event/359/1 "2021-06-27T09:37:43Z")

</div>

Hi!

How can I add methods to DesignedComponents after loading them in code?  
For instance  
Button1 := DesignedComponent (‘Button1’) as TCastleButton;  
I would like to add a small delay so when I click a button it takes for instance 2 seconds before the action is performed.  
The idea is that when I give a command to a transform (scene sprite) there will be a small delay before it actually responds.

---

<div class="post-metadata">

### Author: ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)
#### Post date: [July 2, 2021, 1:39pm UTC](https://forum.castle-engine.io/t/adding-delay-to-event/359/2 "2021-07-02T13:39:45Z")

</div>

To register events on components, follow engine examples/templates like “3D Model Viewer” template:

```auto
procedure TStateMain.Start;
begin
  inherited;

  { Find components, by name, that we need to access from code }
  ButtonStopAnimation := DesignedComponent('ButtonStopAnimation') as TCastleButton;

  { Assign OnClick handler to buttons }
  ButtonStopAnimation.OnClick := @ClickStopAnimation;
end;

procedure TStateMain.ClickStopAnimation(Sender: TObject);
begin
  ....
end;

```

To run something with delay, use a variable controlling the timeout. That is,

- add a variable like `TimeToDoSomething: TFloatTime`

- when user clicks a button, do `TimeToDoSomething := 2.0` (delay in 2 seconds)

- in state `Update` method decrease it , and watch when it drops to zero, like

```auto
procedure TStateMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
begin
  inherited;
  if TimeToDoSomething > 0 then
  begin
    TimeToDoSomething := TimeToDoSomething - SecondsPassed;
    if TimeToDoSomething <= 0 then
    begin
      // do something ...
    end;
  end;
end;

```

---

<div class="post-metadata">

### Author: ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)
#### Post date: [July 3, 2021, 8:01am UTC](https://forum.castle-engine.io/t/adding-delay-to-event/359/3 "2021-07-03T08:01:49Z")

</div>

Thanks!  
And if I want to run a sequence of events and only execute the next event when the previous one is finished?  
For instance: a character sprite is walking off screen and the screen has to fade out before the next location and sprites are loaded?

---

<div class="post-metadata">

### Author: ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)
#### Post date: [July 4, 2021, 7:31am UTC](https://forum.castle-engine.io/t/adding-delay-to-event/359/4 "2021-07-04T07:31:05Z")

</div>

> And if I want to run a sequence of events and only execute the next event when the previous one is finished?

Depends. If you want to do that in a simple way - just create two timeouts. E.g. adding to @michalis example:

```Pascal
procedure TStateMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
begin
  inherited;
  if TimeToDoSomething > 0 then
  begin
    TimeToDoSomething := TimeToDoSomething - SecondsPassed;
    if TimeToDoSomething <= 0 then
    begin
      StartSomeAnimation; // it should check if the animation started already not to start it twice
      TimeToDoSomethingNext := TimeToDoSomethingNext - SecondsPassed;
      if TimeToDoSomethingNext <= 0 then
      begin
         ChangeLocation;
    end;
  end;
end;

```

Note, that while simple, this is not the best solution for a larger scale. There you’ll have to create some event manager otherwise you’ll drown in timeouts and ifs. But this is not an easy task, so if you don’t plan to significantly extend the usecase, I’d go with the simple solution.

---

<div class="post-metadata">

### Author: ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)
#### Post date: [July 4, 2021, 3:51pm UTC](https://forum.castle-engine.io/t/adding-delay-to-event/359/5 "2021-07-04T15:51:02Z")

</div>

Thanks.  
I don’t want to make this too difficult ( I have problems enough already with the screen coordination) 😉 so this is a fine approach.  
For an action following fading out a screen I think I should use a routine that has no countdown but executes after all screen colors are faded to black?

---

<div class="post-metadata">

### Author: ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)
#### Post date: [July 4, 2021, 4:22pm UTC](https://forum.castle-engine.io/t/adding-delay-to-event/359/6 "2021-07-04T16:22:39Z")

</div>

> [@Carring](#):
>
> For an action following fading out a screen I think I should use a routine that has no countdown but executes after all screen colors are faded to black?

I’m not sure if I follow. If you have “fade out” animation and loading starts at its end, then indeed, you can do without the second timer (sorry, I think I’ve misunderstood your first post). As in:

```Pascal
procedure TStateMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
begin
  inherited;
  if TimeToDoSomething > 0 then
  begin
    if SceneIsFadingOut then
    begin
      TimeToFadeOut := TimeToFadeOut - SecondsPassed;
      SetFadedColors(1 - TimeToFadeOut / FadeOutTimeout);
      if TimeToDoSomething <= 0 then
        StartLoading;
  end;
end;

```

---

<div class="post-metadata">

### Author: ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)
#### Post date: [July 4, 2021, 7:34pm UTC](https://forum.castle-engine.io/t/adding-delay-to-event/359/7 "2021-07-04T19:34:48Z")

</div>

Thanks.  
I will try this out as soon I have finally sorted out the screen coordination problems.  
🙂

---

<div class="post-metadata">

### Author: ![EvSpecialProjects](https://forum.castle-engine.io/letter_avatar_proxy/v4/letter/e/258eb7/32.png) [@EvSpecialProjects](https://forum.castle-engine.io/u/EvSpecialProjects)
#### Post date: [June 27, 2025, 2:12pm UTC](https://forum.castle-engine.io/t/adding-delay-to-event/359/8 "2025-06-27T14:12:16Z")

</div>

Hello! I have tried to reproduce your example.  
And I have that error:  
D:\Castle game engine\Castle\_projects\Project11\buttonProject.\code\gamevieworiginstart0.pas(56,16)  
Error: Identifier not found “TFloatTime”.  
What am I missing?

---

<div class="post-metadata">

### Author: ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)
#### Post date: [June 27, 2025, 3:59pm UTC](https://forum.castle-engine.io/t/adding-delay-to-event/359/9 "2025-06-27T15:59:04Z")

</div>

That’s a rather old thread, from 2021 🙂

That being said, the general answer to such questions is to

- search our [API documentation](https://castle-engine.io/apidoc/html/) for the identifier you’re looking for.

- This will show [search results](https://castle-engine.io/apidoc/html/tipue_results.html?q=TFloatTime),

- first result is often the right one, and points to the [documentation of the identifier you’re looking for, in this case TFloatTime](https://castle-engine.io/apidoc/html/CastleTimeUtils.html#TFloatTime).

- If you scroll up on this page, you will see it talks about Unit `CastleTimeUtils`.

- so, add `CastleTimeUtils` to your uses clause 🙂

---

<div class="post-metadata">

### Author: ![EvSpecialProjects](https://forum.castle-engine.io/letter_avatar_proxy/v4/letter/e/258eb7/32.png) [@EvSpecialProjects](https://forum.castle-engine.io/u/EvSpecialProjects)
#### Post date: [June 27, 2025, 5:32pm UTC](https://forum.castle-engine.io/t/adding-delay-to-event/359/10 "2025-06-27T17:32:20Z")

</div>

Thank you for your quick response!  
I accidentlally find TFloatTime in CastleSceneCore and had tried to use that class
