[Solved]How to Embed TCastleTiledMapControl in Form1?

#1

IDE:Lazarus-2.0.2

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

#2

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);
var
  MapControl: TCastleTiledMapControl;
begin
  MapControl := TCastleTiledMapControl.Create(CastleControl1);
  MapControl.URL := 'castle-data:/maps/desert.tmx';
  CastleControl1.Controls.InsertFront(MapControl);
end;
2 Likes
#3

thanks to eugeneloza.


IDE:Lazarus-2.02
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.

#4

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.

2 Likes
#5

Thanks to eugeneloza.
It Works now.

2 Likes
#6

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
#7

IDE:Lazarus-2.0.2
Castle-Engine:6.5

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

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

20190502144937

1 Like
#8

@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).

2 Likes
#9

Thanks to michalis,I Love :heart_eyes:you!