# Trying to build a class inherit form TcastleTransform

**URL:** https://forum.castle-engine.io/t/trying-to-build-a-class-inherit-form-tcastletransform/1863
**Category:** Game Design
**Created:** [April 13, 2025, 7:23pm UTC](https://forum.castle-engine.io/t/trying-to-build-a-class-inherit-form-tcastletransform/1863 "2025-04-13T19:23:37Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![BlueIcaro](https://forum.castle-engine.io/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@BlueIcaro](https://forum.castle-engine.io/u/BlueIcaro)
#### Post date: [April 13, 2025, 7:23pm UTC](https://forum.castle-engine.io/t/trying-to-build-a-class-inherit-form-tcastletransform/1863/1 "2025-04-13T19:23:37Z")

</div>

Hello, as part of my to learn CGE, I started to program a clone of FlappyBird game.  
I made a “small” demo, that you can find here:

[https://github.com/Blueicaro/FlappyBird-in-Castle-Game-Engine](https://github.com/Blueicaro/FlappyBird-in-Castle-Game-Engine)

You can run it, Press space bar to move the bird, and press enter to generate the pipes.

I wrote this code to generate the pipes (Tuberia is a pipe in spanish):

> Tuberia := TuberiaFactory.ComponentLoad(FreeAtStop) as TCastleTransform;  
> Tuberias.Add(Tuberia);  
> Tuberia.Translation :=Vector3(0,0,0);

Now I want to move the pipes up down. So I thought in make a a class inherit form TCastleTransform, and in this class put a timer and a speed to move up/down the pipe.

I create the class that inherited from TCastleTransform, but the constructor has other parameters, so I don’t know to continue.  
Where can I find a demo or code, how to do this.  
Thanks  
/BlueIcaro

---

<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: [April 14, 2025, 2:33pm UTC](https://forum.castle-engine.io/t/trying-to-build-a-class-inherit-form-tcastletransform/1863/2 "2025-04-14T14:33:46Z")

</div>

The `TCastleTransform` is a descendant from the standard `TComponent`, so it has an additional “AOwner: TComponent” parameter. In your descendant classes, you need to override it:

```delphi
type
  TMyDescendant = class(TCastleTransform)
  public
    MyNewField: Integer;
    constructor Create(AOwner: TComponent); override;
  end;

constructor TMyDescendant.Create(AOwner: TComponent);
begin
  inherited;
  MyNewField := 123;
  MyInitialization;
end;

```

An alternative is to define a constructor with new parameters, and use `reintroduce`, but then be aware that it will not be executed when deserializing the component or when using it through a class reference. See e.g. [castle-engine/examples/physics/physics\_asteroids/code/gameviewmain.pas at 68edc4699002d3999acf1c5b0a3405084a4488ef · castle-engine/castle-engine · GitHub](https://github.com/castle-engine/castle-engine/blob/68edc4699002d3999acf1c5b0a3405084a4488ef/examples/physics/physics_asteroids/code/gameviewmain.pas#L29) . Ignore this if it sounds like black magic 🙂

As a general advise, note that **extending functionality by making `TCastleTransform` descendants is often not the best way**.

- While you can of course do this, but sometimes it results in inheritance that prevents you from expressing what you need.

- Using “composition” is often a better choice, an in particular developing new “behaviors” ( [Behaviors | Manual | Castle Game Engine](https://castle-engine.io/behaviors) ) and adding them as children to TCastleTransform is often a better choice. This is great to “attach a logic”, like “this pipe is moving”.

---

<div class="post-metadata">

### Author: ![BlueIcaro](https://forum.castle-engine.io/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@BlueIcaro](https://forum.castle-engine.io/u/BlueIcaro)
#### Post date: [April 15, 2025, 7:59am UTC](https://forum.castle-engine.io/t/trying-to-build-a-class-inherit-form-tcastletransform/1863/3 "2025-04-15T07:59:25Z")

</div>

Hello, thanks you very much for detailed answers.

Rewrite the constructor has many unwanted consequences.  
I going to work with Behaviors as you say is better choice. I think the code is more clean.  
I going study the links that you mentioned. I hope back with my flappyBird game finished.  
/BlueIcaro

---

<div class="post-metadata">

### Author: ![Noscow](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/noscow/32/964_2.png) [@Noscow](https://forum.castle-engine.io/u/Noscow)
#### Post date: [April 15, 2025, 8:37am UTC](https://forum.castle-engine.io/t/trying-to-build-a-class-inherit-form-tcastletransform/1863/4 "2025-04-15T08:37:30Z")

</div>

When I need more parameters for the standard class `constructor`, I sometimes just use additional procedure. Something like this:

```auto
procedure CustomTransform.AfterConstruct(Position : TVector3);
begin
  Translation := Position;
end;

```

and:

```auto
NewTransform := CustomTransform.Create(Owner);
NewTransform.AfterConstruct(PositionVector);

```

It separate setting logic from creation logic. A dumb but working way 🔨  
Maybe this way not so elegant, but I can be sure `constructor` works as he must. Not busy about things, that somebody already made fine 🙃  
But I don’t know, is it good for use with the `Factory`.

---

<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: [April 15, 2025, 1:06pm UTC](https://forum.castle-engine.io/t/trying-to-build-a-class-inherit-form-tcastletransform/1863/5 "2025-04-15T13:06:10Z")

</div>

> [@Noscow](#):
>
> It separate setting logic from creation logic. A dumb but working way 🔨  
> Maybe this way not so elegant, but I can be sure `constructor` works as he must. Not busy about things, that somebody already made fine 🙃  
> But I don’t know, is it good for use with the `Factory`.

I think what you show - is a completely normal approach 🙂 , so I would not call it “dumb”.

As my wife says: _“if something is dumb but works, then it’s not dumb”_ 🙂

When the component is constructed by deserialization (from `.castle-user-interface` or such files) or using `TCastleComponentFactory` then naturally only the standard constructor (`Create(SomeOwner)`) is called, and you have to make sure to call `AfterConstruct` (because it needs some parameters that cannot be known at construction) on all instances yourself. Which is OK.

( You can also add more published properties and design your component visually, registering it in the editor, [Custom Components | Manual | Castle Game Engine](https://castle-engine.io/custom_components) . But this is not supposed to be a “solution for everything” 🙂, i.e. it’s not strictly an “alternative to constructor with parameters”. If you really need to call some additional initialization, with some custom parameters, then a method like `AfterConstruct` makes total sense IMHO. )
