Author Topic: Soldat Scripts with Lazarus IDE  (Read 5169 times)

0 Members and 1 Guest are viewing this topic.

Offline tk

  • Soldier
  • **
  • Posts: 235
Soldat Scripts with Lazarus IDE
« on: July 24, 2016, 08:17:57 pm »
Development of Soldat scripts longer than a few hundreds lines may be problematic without aid of a proper IDE. In this topic I show how to develop Soldat scripts with Lazarus - freeware Pascal IDE coming with Free Pascal Compiler (FPC).
Why even bother? Firstly, you gain some basic refactorization and code navigation features offered by Lazarus. Secondly, you don't need to run SoldatServer billions of times to find all your errors in the code, the Pascal compiler will do that automatically. Yes, you actually can compile your script in Lazarus, which doesn't make much sense, except for parsing the script for syntactic correctness. The produced binary is rubbish, obviously.

Project structure
Under linked Github repository, the scriptcore.pas unit with example project are provided.

Your script must be compilable both by SoldatServer and FPC. This is done by maintaining structure of an actual Pascal program and using ScriptCore.pas unit, which consists of empty SC2/SC3 declarations.
In main project file (*.lpr) there must be the main script file included.
All script units must have scriptcore.pas included, so the FPC can recognize Scriptcore references.
Code: [Select]
{$ifdef FPC}

In general, Pascal Script is more liberal than Delphi, although not everything compiled by Delphi will be compiled by Soldat Server. Your current scripts may require some minor changes. I haven't found anything that cannot be fixed by preprocessor.

Problems discovered so far
  • GetArrayLength, SetArrayLength - you may have to replace them with Length, SetLength, because of some variant issues in FPC. The latter version is known both to Delphi and Scriptcore.
  • array of string - try replacing it with TStringArray.
  • if string <> nil - Delphi doesn't take this well, change it to if string <> ''.
  • case string of - This seems not to be allowed in Delphi, therefore you may need to use preprocessor
    to hide it from FPC.
Code: [Select]
{$ifndef FPC}
 code which FPC doesn't like.

If you come across any piece of code that FPC doesn't digest, just exclude it with the preprocessor. It only matters that Scriptcore compiles it after all.

Creating a new project
You may modify the example project from the link. If for some reason you want to make the project from scratch, then go:
File -> New -> Simple program
Project -> Project Options
   Parsing -> Syntax mode -> Delphi
   (optional) Compilation and Linking -> Set optimization level to zero so it compiles faster.
   (optional) Messages -> you may want to disable some warnings about unused parameters and such, otherwise the compilation log might get spammy;

Include the main script unit from the *.lpr file with "uses";
Save the project;
Build (Shift + F9).

The Lazarus IDE
scriptcore.pas with example project on GitHub

Final notes
It's still experimental. The scriptocre.pas is based on scriptcore source files, but it still may be lacking some declarations, or they might be incorrect. We will (probably) be updating it over time.
« Last Edit: July 24, 2016, 08:27:47 pm by tk »

Offline jrgp

  • Administrator
  • Flamebow Warrior
  • *****
  • Posts: 5037
Re: Soldat Scripts with Lazarus IDE
« Reply #1 on: July 24, 2016, 10:15:16 pm »
Nice job. That scriptcore.pas clearly took a lot of work.
There are other worlds than these