PDA

View Full Version : delphi programming. (Scripting Language)



bekuzofu
17-06-2005, 06:23 PM
i'm currently working on a rpg maker and have finished the tile engine.

I was wondering if anyone knew how to create a scripting language in delphi or can give a sample code to start me off.

The scripting language will help load the tiles, and control npc and stuff.

Thank you if someone can help me. :D

technomage
17-06-2005, 07:33 PM
Hi

If you are after a good pascal scripting lanugage, try te Pascal Script from Rem Objects http://www.remobjects.com/page.asp?id={9A30A672-62C8-4131-BA89-EEBBE7E302E6}

It used to be Innerfuse Pascal Script, but the author went to work for Rem Objects and took the software with him. It's a nice system, allows you to compile scripts and use Delphi objects in your scripts. All in all a good solution.

Dean

WILL
25-06-2005, 05:16 AM
Hmm... is there a Free Pascal capable solution aswell?

technomage
25-06-2005, 07:23 PM
The scripting system works under FPC I've tested it. :D

It does need a few changes here and there to get it compiling but the changes aren't too complicated.

Legolas
25-06-2005, 09:56 PM
It works with freepascal and, with some limitations, I installed it in Lazarus too. Indeed you can install 4 components only (PSScript, PSScriptDebugger, PSDllPlugin, PSScriptExtension), because the others are heavyly based on vcl and a porting is very hard.

technomage
25-06-2005, 10:28 PM
I don't use the components, you can skip that a just go straight to the compiler and run time classes. It's a little more work , but you get around having to convert the components.

it's nice an quick too :)

cairnswm
26-06-2005, 08:22 AM
Can you use the non VCL objects as is or is there any conversion neccessary. I am trying to find a nice pascal scripting colution for my games - currently I'm using DWS (which needs VCL).

If you have some nice examples of registering call back functions and procedures I'd appreciate it (I vahe them nicely packeged in a procedure now).

savage
26-06-2005, 12:31 PM
Another Scripting solution I have looked into is JavaScript ( AKA SpiderMonkey ). There are some Delphi bindings out there ( http://delphi.mozdev.org/ ) and they should work on Linux as well. Almost everyone knows JavaScript due to it being used pervasivly on in WebPage and the Mozilla browser.

Anyway just another option.

technomage
27-06-2005, 11:06 PM
The non VCL components derive from TObject so it can work without the VCL. Note that you do need to mess around with the units to get them to compile under FPC as they haven't quite got the IFDEF's right.

Running scripts is in two stages, first you compile the script , then you execute it. The compile stage can be done as a pre process so you can distribute only compiled scripts with your application.

There is an example of running a compiled script, the script and thje compiler. It assumes that the Pascal Script units are in you library path.

Hope it helps

Dean


Testbed to run the code


program testbed;
{$APPTYPE CONSOLE}
uses
SysUtils,
Classes,
Dialogs,
uPSRuntime,
uPSUtils,
uPSCompiler;



var Exec: TPSExec;
s: TStringStream;
fs: TFileStream;


procedure Debug_LogMessage(const s: string);
begin
Writeln(s);
end;
function Debug_GetInput: string;
begin
Result := InputBox('Debug_GetInput','GetInput','test');
end;

begin
Exec := TPSExec.Create;
s := TStringStream.Create('');
try
Exec.RegisterDelphiFunction(@Debug_LogMessage, 'DEBUG_LOGMESSAGE', cdRegister);
Exec.RegisterDelphiFunction(@Debug_GetInput, 'DEBUG_GETINPUT', cdRegister);

fs := TFileStream.Create(ParamStr(1), fmOpenRead);
try
s.CopyFrom(fs, 0);
finally
fs.Free;
end;
if not Exec.LoadData(s.DataString) then
begin
Writeln(Exec.ExceptionString);
Exit;
end;
Exec.RunScript;
finally
s.Free;
Exec.Free;
Readln;
end;
end.


Here is the test script


var t: string;
i: integer;
begin
t := Debug_GetInput;
for i := 0 to 100 do
begin
Debug_LogMessage(t);
end;
end.


This is the compiler

program compiler;

uses
SysUtils, Dialogs, Classes,
uPSUtils,
uPSCompiler,
uPSPreProcessor,
uPSRuntime;

var Compiler: TPSPascalCompiler;
Data: string;
Script: TStringList;
CompiledScript: TStringStream;
idx: Integer;


function OnUses(Sender: TPSPascalCompiler; const Name: string): Boolean;
begin
Writeln('OnUses');
Result := False;
if Name = 'SYSTEM' then
begin
Sender.AddDelphiFunction('procedure Debug_LogMessage(const s: string)');
Sender.AddDelphiFunction('function Debug_GetInput: string');
Result := True;
end;
end;


function OnExportCheck(Sender: TPSPascalCompiler; Proc: TPSInternalProcedure; const ProcDecl: string): Boolean;
begin
Result := True;
end;

begin
Script := TStringList.Create;
Script.LoadFromFile(ParamStr(1));
Compiler := TPSPascalCompiler.Create;
try
Compiler.OnUses := OnUses;
Compiler.OnExportCheck := OnExportCheck;
Compiler.AllowNoBegin := True;
Compiler.AllowNoEnd := True; // AllowNoBegin and AllowNoEnd allows it that begin and end are not required in a script.
if not Compiler.Compile(Script.Text) then // Compile the Pascal script into bytecode.
begin
// You could raise an exception here.
for idx := 0 to Compiler.MsgCount-1 do
begin
Writeln(Compiler.Msg[idx].MessageToString);
end;
Exit;
end;
Compiler.GetOutput(Data); // Save the output of the compiler in the string Data.
CompiledScript := TStringStream.Create(Data);
try
with TFileStream.Create(ParamStr(2), fmCreate or fmOpenWrite) do
begin
try
CopyFrom(CompiledScript, 0);
finally
Free;
end;
end;
finally
CompiledScript.Free;
end;
finally
Compiler.Free;
Script.Free;
end;
end.

cairnswm
28-06-2005, 05:09 AM
Thanks - this is exactly what I need :) This is one of the last things stopping my move over to FreePascal at home, now if only they can drop the size of the exes!