# \[solved\] Backward compat bug of TCastleScene "structure"

**URL:** <https://forum.castle-engine.io/t/solved-backward-compat-bug-of-tcastlescene-structure/1938>\
**Category:** Bugs\
**Created:** [June 10, 2025, 5:38pm UTC](https://forum.castle-engine.io/t/solved-backward-compat-bug-of-tcastlescene-structure/1938 "2025-06-10T17:38:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phomm](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/phomm/32/2827_2.png) [@phomm](https://forum.castle-engine.io/u/phomm)\
**Post date:** [June 10, 2025, 5:38pm UTC](https://forum.castle-engine.io/t/solved-backward-compat-bug-of-tcastlescene-structure/1938/1 "2025-06-10T17:38:55Z")

</div>

Some time and commits ago (assume like around 50-60 compared to yesterday) I updated the castle code locally and I was working on my game flawlessly. When I pushed the changes to master yesterday, the build started on github (it should be using the latest CGE) and in result I got a build which was failing on lines like this:

```auto
LCell := ACastleTransformDesignField.Items[0] as TCastleImageTransform;

```

and design file of that CastleTransformDesign was containing just basic hierarchy:

```auto
{
  "$$ClassName" : "TCastleImageTransform",
...
  "Url" : "castle-data:/textures/cell.png"
}

```

the error is basically EInvalidCast.  
After some research I updated the code of CGE to latest locally, and I found out that all TCastleScene components (on based on that, like TCastleText, TCastleImageTransform, which I use) started to require additional block Items[0] to get to exact component that I need (usually I load dynamically designs and wire its subcomponents), thus Design contains the image not directly but first some intermediate Transform and that Transform then holds Image.

So I think this is some change in CastleScene which got restructured and broke this flow.  
So regardless of intentions , decisions and so on, I’m here just to report and ask if it stays like this in future or it is really an issue to be fixed.

Disclaimer. I know this is actually my fault as I shouldn’t be using explicit indexing Items and nested Items, but rather names of components, which should be error-prone to such things. Ofc I will try to rework it in future, but also I’ve adjusted code to new “paradigm” and took those risks for now, it is not a big deal 😅

---

<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 11, 2025, 3:01am UTC](https://forum.castle-engine.io/t/solved-backward-compat-bug-of-tcastlescene-structure/1938/2 "2025-06-11T03:01:08Z")

</div>

Thanks for bringing this to my attention – I indeed did a change that is likely responsible for what you observe. Though I didn’t actually realize that I broke compatibility for usage like you show.

Short version of the answer: The change will stay (we need this to fix another problem), but I added in [a commit I did just now](https://github.com/castle-engine/castle-engine/commit/206d31b0a6c173364fedb33b9fe99049c8bcbfab) a feature that will make your life easier, the access will be easier _and_ be invulnerable to such changes in the future: `TCastleTransformDesign.DesignRoot` read-only property. So instead of your own old

```delphi
// don't use this anymore
LCell := ACastleTransformDesignField.Items[0] as TCastleImageTransform;

```

→ you want to do this (if I understand the situation from the description correctly):

```delphi
// use this now, and don't worry how deep is DesignRoot inside ACastleTransformDesignField :)
LCell := ACastleTransformDesignField.DesignRoot;

```

Longer explanation what happened:

Note: We didn’t make any change to `TCastleScene` (which doesn’t create any internal `TCastleTransform` children) or `TCastleImageTransform` (which was and is still creating one internal child, an instance of `TCastleScene`) lately.

We did however change the `TCastleTransformDesign` which I guess explains what you experience: see [TCastleTransformDesign fix: make SetTransient only on FDesignContaine… · castle-engine/castle-engine@b6dc637 · GitHub](https://github.com/castle-engine/castle-engine/commit/b6dc637a035b56194686160beac20e9c6dfc4e65) .

- Previously it added the loaded components as the direct child of `TCastleTransformDesign` – this was causing problems, because it meant that root (and only root) of loaded design has to  
be `SetTransient`. This means that trying to detect ray hits to this root wasn’t working properly.

- In new approach, the `TCastleTransformDesign` creates additional child, `FDesignContainer`, that is `SetTransient`. And only adds newly loaded hierarchy as a child of `FDesignContainer`.

I found this when testing example from [GitHub - castle-engine/conference-delphi-summit-2025: Demo for Delphi Summit 2025 https://delphisummit.com/](https://github.com/castle-engine/conference-delphi-summit-2025) , where we have “crates” thrown with physics. After the fix, the trivial `MainViewport.TransformHit` ([here](https://github.com/castle-engine/conference-delphi-summit-2025/blob/master/walk_3d_game_controllers/code/gameviewmain.pas#L231)) works.

So, this was a deliberate change, I think the new organization is better, the right component is marked “transient”. You should not assume whether the loaded contents are directly or indirectly children of `TCastleTransformDesign`.

Solutions:

1. As you say, you could use names. Like `MyRoot := MyDesign.DesignedComponent('RootInDesign');`
2. But we could also make it easier, and just expose a direct read-only way to get design root. Which I’ve done now → see top of this answer 🙂

Hope this helps!

---

<div class="post-metadata">

**Author:** ![phomm](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/phomm/32/2827_2.png) [@phomm](https://forum.castle-engine.io/u/phomm)\
**Post date:** [June 11, 2025, 10:57am UTC](https://forum.castle-engine.io/t/solved-backward-compat-bug-of-tcastlescene-structure/1938/3 "2025-06-11T10:57:42Z")

</div>

Thanks, @michalis for sorting this out so quickly (and also the other issue I reported on github), I will try this new approach with DesignRoot field and write my findings.

---

<div class="post-metadata">

**Author:** ![phomm](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/phomm/32/2827_2.png) [@phomm](https://forum.castle-engine.io/u/phomm)\
**Post date:** [June 16, 2025, 10:29am UTC](https://forum.castle-engine.io/t/solved-backward-compat-bug-of-tcastlescene-structure/1938/4 "2025-06-16T10:29:19Z")

</div>

Hi, I tried all this during weekend and it works pretty much good, so I changed it everywhere and got rid of crawling through the hierarchy, which is nicer. so I’m happy with this, thank you, @michalis !
