D scripting (c++ integration)

Pragma ericanderton at yahoo.removeme.com
Mon Oct 2 07:23:20 PDT 2006


Johan Granberg wrote:
> bubu wrote:
>> Hiya,
>>
>> im a game programmer. I want to use some D-scripted objects in my game 
>> which
>> is programmed in C++. Can I embed a D scripting host like I can do 
>> with Lua,
>> Phyton or Ruby? thx
> 
> This newsgroup is not used sou much anymore try digitalmars.D instead.
> 
> To answer your question.
> 
> I don't know of any way to embed D as a script language but if you want 
> you could require script writers to compile their scripts using some D 
> compiler. I think DDL (dsource.org) can be of use here, alternativly 
> just build the scripts into a .dll/.so file.
> 
> Hope that helped :)

Johan, thanks for the plug. :)

bubu,

DDL is going to work, but only if you have a D program to start.  You 
might be able to roll your own scripting host as a .dll or something 
similar, provided it's in D.  However, there's no embeded compiler 
product available, so users will still need the D compiler installed.

With regards to DDL, using D for scripting is the reason why I hatched 
the project in the first place.  If you go back and read my journal 
entries in the DSP forum (also on DSource), you'll get an idea of what 
is ahead of you for dynamic binding on windows, without such a 
technology.  Depending on how you proceed, you may or may not need to 
worry about the things I did.

FWIW, the following pattern can be used to make D flow a bit more like 
live scripting environments:

1) Launch the D compiler from within your app directly, to compile the 
"script" you want to run - std.process works well.
2) Use DDL to bind the .obj file from the compiler output.  The linker 
will return a DynamicLibrary object to use.
3) Use the DynamicLibrary to bind any functions, classes, or static 
fields that you know to exist in the compiled script.

That last point is kind of tricky.  While you can simply force script 
coders to adhere to a particular interface, I found it easier to wrap 
the whole script in a function (call that step #0) before compilation. 
This ensures that the starting point of the script was always known, at 
the cost of disallowing some D constructs (bang-line, imports, module 
static init, etc).  It also allows the script author to start from line 
1, much like one can in PHP or JavaScript.

-- 
- EricAnderton at yahoo



More information about the Digitalmars-d mailing list