[Solved]How to Embed TCastleTiledMapControl in Form1?


procedure TForm1.Button1Click(Sender: TObject);
CastleTiledMapControl1.URL := ‘castle-data:/maps/desert.tmx’;

Hi, and welcome!
There are a few issues with your code.

  1. The classes are created like ClassVariable := TClassType.Create(AOwner). You shouldn’t call obsolete SomeObject.Create. You can read more about classes and how they are different from objects in https://castle-engine.io/modern_pascal_introduction.html . I really recommend this book, it’s very simple, yet gives most of the basics you may need.
  2. You have to “first create the control” then change something. You can’t assign URL field before the class was created.
  3. Never call Destroy explicitly. It’s automatically called, when the class is freed by FreeAndNil or ClassVariable.Free. You can read about that in that book above too.
  4. Try to avoid creating something in button click event. This way the class will be created again, when the user presses button, ending up with many instances (i.e. wasting memory and probably all of those will be displayed on screen - the last ones will not be deleted automatically unless you remove them from TCastleControl.SceneManager).
  5. To add an UI control, like TCastleTiledMapControl to TCastleControl you should add those to TCastleControl.SceneManager or TCastleControl.Controls using InsertFront, not to TCastleControl itself.

So, your code should be something like:

procedure TForm1.FormCreate(Sender: TObject);
  MapControl: TCastleTiledMapControl;
  MapControl := TCastleTiledMapControl.Create(CastleControl1);
  MapControl.URL := 'castle-data:/maps/desert.tmx';

thanks to eugeneloza.

Castle Game Engine:6.5
CastleControl1 is black,I don’t know how to fix it.
Castle2DControl1 is black,I don’t know how to fix it.
Thanks to the CGE community.

Ah, I’ve forgotten an important thing. To view the whole map, you have to set MapControl.FullSize := true; after creation, this way it will not be a small piece of map (100x100 default), but the map will take the whole area of CastleControl.
The reason why you had the “control is black” is because the colored map was actually higher than the are covered by MapControl, so it only rendered background piece (i.e. black).
One note about your code: do not insert one MapControl (or anything else) into two different CastleControls (like CastleControl1 and Castle2DControl1 in your code), it may misbehave.


Thanks to eugeneloza.
It Works now.


Believe in the power of the community

Author: a contributor & Google Translate

When faced with a problem,
It took no whole night to find the answer all day long.
Publish the problem to the community,
It will take a few hours,
There are people from unknown countries, unknown genders, strangers who have never met,
Give you the most satisfactory answer.
No need to pay,
No transactions,
No need to pay,
No transactions,
I want to be a devotee like that!

1 Like


Target OS:Win64
Target CPU family:x86_64
It Works!

Target OS:Win32
Target CPU family:i386
It doesn’t work.


1 Like

@edit.games Fixed! Thanks for reporting this.

And thanks for publishing https://github.com/edit-games/EmbedTiledInForm on GitHub – it was trivial to reproduce the problem thanks to this example. Indeed the error was only on win32. I remade the relevant code to use AnsiString all the way, it’s now simpler and the error is gone :slight_smile:

The fixed engine version is immediately available from GitHub, on https://github.com/castle-engine/castle-engine/ . The 6.5 snapshot on https://castle-engine.io/ wil also be automatically updated (but this will take a few hours).


Thanks to michalis,I Love :heart_eyes:you!