Recent

Author Topic: [SOLVED] Add new unit to a Lazarus project from an external tool  (Read 1247 times)

Lulu

  • Sr. Member
  • ****
  • Posts: 347
[SOLVED] Add new unit to a Lazarus project from an external tool
« on: November 15, 2025, 07:56:12 am »
Hi, in OGLCScene (game library), there is a tool called Game Helper to help to create sprites , levels and other things.

The tool can generate Pascal unit from a design of a sprite or game level, but I'm not sure how to add the unit created to the target lazarus project from the tool.

Does:
 - adding the unit name in the USES clause in the LPR project file
 - Adding an XLM entry with the name of the unit in the lpi file
is sufficient ?

Thanks
« Last Edit: November 18, 2025, 08:03:31 pm by Lulu »
wishing you a nice life!
GitHub repositories https://github.com/Lulu04

cdbc

  • Hero Member
  • *****
  • Posts: 2531
    • http://www.cdbc.dk
Re: Add new unit to a Lazarus project from an external tool
« Reply #1 on: November 15, 2025, 08:11:09 am »
Hi
Yup, those two and then also in every unit that'll use it...
...and you'll have to figure out, if it's enough, to put it in the implementation-part or it has to go in the interface-part.
That should do it  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Lulu

  • Sr. Member
  • ****
  • Posts: 347
Re: Add new unit to a Lazarus project from an external tool
« Reply #2 on: November 15, 2025, 02:58:02 pm »
Thank you benny for your answer.
Ok, first I have to start with https://wiki.freepascal.org/XML_Tutorial  :D
wishing you a nice life!
GitHub repositories https://github.com/Lulu04

n7800

  • Hero Member
  • *****
  • Posts: 587
  • Lazarus IDE contributor
    • GitLab profile
Re: Add new unit to a Lazarus project from an external tool
« Reply #3 on: November 15, 2025, 11:50:01 pm »
Am I correct in assuming you have a unit file and just want to use it in your project?

Then simply place it in the project folder and specify its name in the "uses" section of the units you're using. This will work.

Adding the unit to the LPR file (open it via "Menu > Project > View Project Source") is optional if it doesn't have an initialization section.

Making the file part of the project (LPI) isn't actually necessary either, but you can open "Menu > Project > Project Inspector" and click the "Add" button in the toolbar.



Or did I misunderstand, and you want to write some kind of tool to automatically add units? A quick search for "OGLCScene Game Helper" didn't yield many results, so I'm not sure what you're talking about.

Lulu

  • Sr. Member
  • ****
  • Posts: 347
Re: Add new unit to a Lazarus project from an external tool
« Reply #4 on: November 16, 2025, 06:34:27 pm »
Hi n7800, sorry my explanation was not good enough.
yes I'm writing a tool named Game Helper that allow to graphically design sprites and game levels for OGLCScene library.

Inside the tool, when user create a new project game, an associated Lazarus project is also created from a template. Let's call it the target Lazarus project.

Always inside the tool, when the user design and save a sprite or a level, the tool automatically generate a corresponding Pascal unit, saved inside the target Lazarus project.

Quote
you can open "Menu > Project > Project Inspector" and click the "Add" button in the toolbar

If possible I would like also the tool to do that automatically.

For now, Game Helper can add or remove an entry into  the clause unit of the LPR file (target Lazarus project)
wishing you a nice life!
GitHub repositories https://github.com/Lulu04

Lulu

  • Sr. Member
  • ****
  • Posts: 347
Re: Add new unit to a Lazarus project from an external tool
« Reply #5 on: November 17, 2025, 09:46:01 pm »
About LPI file:
Looking at the structure of the LPI file (inside some of my old/new project) I've encountered two different structure of the UNIT section in the XML structure:
one without number (in project with less units)
Code: Pascal  [Select][+][-]
  1.     <Units>
  2.       <Unit>
  3.         <Filename Value="project1.lpr"/>
  4.         <IsPartOfProject Value="True"/>
  5.       </Unit>
  6.       <Unit>
  7.         <Filename Value="unit1.pas"/>
  8.         <IsPartOfProject Value="True"/>
  9.         <ComponentName Value="Form1"/>
  10.         <ResourceBaseClass Value="Form"/>
  11.         <UnitName Value="Unit1"/>
  12.       </Unit>
  13.     </Units>
  14.  
and another with numbers (in project with lot of units)
Code: Pascal  [Select][+][-]
  1.     <Units Count="84">
  2.       <Unit0>
  3.         <Filename Value="GameHelper.lpr"/>
  4.         <IsPartOfProject Value="True"/>
  5.         <WindowIndex Value="1"/>
  6.         <TopLine Value="10"/>
  7.         <CursorPos X="33" Y="37"/>
  8.         <UsageCount Value="200"/>
  9.         <Loaded Value="True"/>
  10.       </Unit0>
  11.       <Unit1>
  12.         <Filename Value="Units\form_main.pas"/>
  13.         <IsPartOfProject Value="True"/>
  14.         <ComponentName Value="FormMain"/>
  15.         <HasResources Value="True"/>
  16.         <ResourceBaseClass Value="Form"/>
  17.         <EditorIndex Value="8"/>
  18.         <TopLine Value="350"/>
  19.         <CursorPos X="12" Y="364"/>
  20.         <UsageCount Value="200"/>
  21.         <Loaded Value="True"/>
  22.         <LoadedDesigner Value="True"/>
  23.       </Unit1>
  24.       <Unit2>
  25.         <Filename Value="Units\windowsscene.pas"/>
  26.         <ComponentName Value="Window_Scene"/>
  27.         <HasResources Value="True"/>
  28.         <ResourceBaseClass Value="Form"/>
  29.         <UnitName Value="WindowsScene"/>
  30.         <EditorIndex Value="-1"/>
  31.         <CursorPos X="2" Y="36"/>
  32.         <UsageCount Value="34"/>
  33.       </Unit2>
  34. .....
  35. .....
  36.       <Unit83>
  37.         <Filename Value="Units\frame_viewlevelbank.pas"/>
  38.         <IsPartOfProject Value="True"/>
  39.         <ComponentName Value="FrameViewLevelBank"/>
  40.         <ResourceBaseClass Value="Frame"/>
  41.         <IsVisibleTab Value="True"/>
  42.         <EditorIndex Value="7"/>
  43.         <WindowIndex Value="1"/>
  44.         <TopLine Value="99"/>
  45.         <CursorPos X="9" Y="103"/>
  46.         <UsageCount Value="69"/>
  47.         <Bookmarks Count="2">
  48.           <Item0 X="3" Y="287"/>
  49.           <Item1 X="3" Y="297" ID="1"/>
  50.         </Bookmarks>
  51.         <Loaded Value="True"/>
  52.         <LoadedDesigner Value="True"/>
  53.       </Unit83>
  54.     </Units>
  55.  

Which of these two structures should I use to insert/delete an entry from the Game Helper tool?
wishing you a nice life!
GitHub repositories https://github.com/Lulu04

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4653
  • I like bugs.
Re: Add new unit to a Lazarus project from an external tool
« Reply #6 on: November 18, 2025, 07:16:16 am »
Quote
Which of these two structures should I use to insert/delete an entry from the Game Helper tool?
You should use the one without numbers. It is the current format, easier to change and creates less diff in a revision control.
Lazarus IDE still supports the old format. See Project Options -> Miscellaneous -> Maximize compatibility of project files (LPI and LPS).
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Lulu

  • Sr. Member
  • ****
  • Posts: 347
Re: Add new unit to a Lazarus project from an external tool
« Reply #7 on: November 18, 2025, 10:47:56 am »
@JuhaManninen
Thank you! With this information I can now fully add or remove a unit from a project from the tool.

Regards
wishing you a nice life!
GitHub repositories https://github.com/Lulu04

n7800

  • Hero Member
  • *****
  • Posts: 587
  • Lazarus IDE contributor
    • GitLab profile
Re: [SOLVED] Add new unit to a Lazarus project from an external tool
« Reply #8 on: November 19, 2025, 11:41:18 pm »
If you're only editing a file, not creating one, you should continue using numbers if they exist. And don't forget to update the "Count" attribute. Otherwise, it will break the format's compatibility with older versions.

In general, "manually" editing files is dangerous, as they may change again in future versions. As I said, adding files to LPI is usually not necessary, the project should still compile.

n7800

  • Hero Member
  • *****
  • Posts: 587
  • Lazarus IDE contributor
    • GitLab profile
Re: [SOLVED] Add new unit to a Lazarus project from an external tool
« Reply #9 on: November 19, 2025, 11:53:34 pm »
The correct way to edit projects is through code, from within an IDE package. But that's not really suitable, since you have a standalone application. I even opened it and tried it, but after a quick test, I still couldn't create anything ))

However, I don't know how far you want to go with development. As far as I understand, this is just a level/sprite editor, and game development is being done in Lazarus? In that case, it could be made into a design package. Editors can be separate windows, added to menus, and much more...

Lulu

  • Sr. Member
  • ****
  • Posts: 347
Re: [SOLVED] Add new unit to a Lazarus project from an external tool
« Reply #10 on: November 20, 2025, 06:02:52 pm »
If you're only editing a file, not creating one, you should continue using numbers if they exist. And don't forget to update the "Count" attribute. Otherwise, it will break the format's compatibility with older versions.
The Lazarus project is created with the options "Maximize compatibility of LPI/LPS file" disabled.

Quote
In general, "manually" editing files is dangerous, as they may change again in future versions. As I said, adding files to LPI is usually not necessary, the project should still compile.
You're right. I think to remove the LPI modify feature for that reason, since the project should still compile. Thanks for the advice!

Quote
The correct way to edit projects is through code, from within an IDE package. But that's not really suitable, since you have a standalone application. I even opened it and tried it, but after a quick test, I still couldn't create anything
I've sent last commits today but Game Helper still not yet ready. I will post a project example soon.

Quote
However, I don't know how far you want to go with development. As far as I understand, this is just a level/sprite editor, and game development is being done in Lazarus? In that case, it could be made into a design package. Editors can be separate windows, added to menus, and much more...
I don't know myself how far I want/can to go  :)
Yes, the game development is keeped under Lazarus because the IDE is very good. Game Helper is only a tool to produce Pascal unit that define object (sprite, levels and others) from a graphical editor to avoid the repetitive work each time we need to define an object.

Do you mean it's possible to make a package to integrate the tool into Lazarus even if it render the graphics using OpenGL at design time ? For now, I don't have the knowledge for that... But the idea is nice!
 
wishing you a nice life!
GitHub repositories https://github.com/Lulu04

n7800

  • Hero Member
  • *****
  • Posts: 587
  • Lazarus IDE contributor
    • GitLab profile
Re: [SOLVED] Add new unit to a Lazarus project from an external tool
« Reply #11 on: November 20, 2025, 07:07:22 pm »
I don't know the details of your project, but I think it's very likely. OpenGL shouldn't be a problem, since the IDE is just a regular program. It's possible that launching the IDE could be an issue if the OpenGL libraries are missing - this depends on the implementation of the library bindings.

At first glance, I imagine the user clicking "Create sprite" in the menu, and a modal editor window opens in the IDE. They finish editing, click "OK", and the user returns to the IDE, seeing the new unit in the Code Editor. Of course, this is just an example.

The question is whether this would actually be better. For example, a separate program currently allows you to work even without an IDE. Theoretically, sprites and levels could be created by people other than programmers. So, in some ways, the current solution is even more logical.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4653
  • I like bugs.
Re: [SOLVED] Add new unit to a Lazarus project from an external tool
« Reply #12 on: November 20, 2025, 07:19:56 pm »
An IDE plugin package would allow adding files to a project using the API.
Something like: LazProject1.AddFile().
Maybe you find also other features that would benefit from the integration.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Lulu

  • Sr. Member
  • ****
  • Posts: 347
Re: [SOLVED] Add new unit to a Lazarus project from an external tool
« Reply #13 on: November 20, 2025, 09:03:20 pm »
Thank you n7800 and JuhaManninen for your answers and interest.
I think stay on the idea to keep Game Helper separated from the IDE. So the modal editor window inside the IDE is not suitable.

For now the OGLCScene package is not a plugin of the IDE.

What you describe is interesting: we can imagine a new package, that install in the IDE some new menus entries/buttons/panels to allow the programmer to launch the game editor.
When the game editor save its internal project and produce a Pascal unit, it warns Lazarus that a new unit has been generated. Then Lazarus add the new unit to the current project, current Code Editor. The programmer can continue to code with.


EDIT: After a night of sleep, things are a little more clear: your idea to incorporate the sprite editor into the IDE (as modal window) is good and avoid many problems.

The problem I have are:
 - I have no idea how to add new menu/button and other stuff to Lazarus from a plugin. May be have you some links to help to start ?

- Starting an external application is ok, but how to wait a respons from it ? The simpliest way I know is to check periodically the presence of a kind of answer file in a specific folder. This file will be generated by the game editor when a Pascal unit is generated or when user quit without creating something. When Lazarus found this file, it load it and do the job according the info find inside. May be there is a better approach?

Edit2: I've take a look to the TODO plugin: it use some call IDE API. I've googled Lazarus IDE PlugIN API but found nothing

Edit3:
Quote
It's possible that launching the IDE could be an issue if the OpenGL libraries are missing - this depends on the implementation of the library bindings
OpenGL is dynamically loaded with LoadLibrary(). If the lib isn't loaded, an error property is set and OGLCScene do nothing in its internal loop. The application can read this property and show a message to prevent the user. (feature not very tested but should work...)
« Last Edit: November 21, 2025, 08:01:16 am by Lulu »
wishing you a nice life!
GitHub repositories https://github.com/Lulu04

Lulu

  • Sr. Member
  • ****
  • Posts: 347
wishing you a nice life!
GitHub repositories https://github.com/Lulu04

 

TinyPortal © 2005-2018