www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D, DLLs, UnrealScript and UDK (Sort of a complex question)

reply lsc9x <lscarrozzi hotmail.com> writes:
Dear Friends,

I have a pretty technical question about D that I might be making more or less
complex based on my lack of knowledge about C++.

Any help would be appreciated!

I'm a novice programmer who is more into the design end of game design, but I
want to learn programming.  I know some very basic programming concepts, but
only enough code to be dangerous.  Epic games has given their UDK game
creation toolset away for free, and it's an amazing complication of game
related creation tools. (www.udk.com) One of the features of the package is a
visual scripting language called Kismet (which compiles to C++ code when
building the game .exe) and the other tool is a mid level "c++ style"
scripting language called UnrealScript.

Since UnrealScript is "C++ like" I figured the best way to learn UScript would
be to learn C++ since they are similar.  Fair enough.  Unfortunately, UScript
has some disadvantages.  It is a higher level language that is (according to
Epic games) about 20 times slower than native C++ "engine" code.  The reasons
are obscure to a novice like me, but "slow" in game terminology means bad
frame rates, and bad frame rates mean an unplayable game.  This is especially
true if a ton of computations need to be made at runtime through slow code.

A solution to this problem is to either:

1) Spend a ton of money to get UE3 license which gives full access to the C++
source code for UE3

OR

2) Write DLL files in C++ and then call functions from those DLL files from
UScript.

I think idea #1 is a bad idea for me not only because it costs a lot of money,
but because I am not competent enough programming to mess around with the game
engine code, NOR DO I WANT TO!  I do, however, need to make my code as fast as
possible and as easy as possible for me to write.

So, I was poking around looking at C++ books and I wondered "Gee, I know there
were languages A, C, C, C++ and C#, I wonder if there is a "D" language.

Surprise!  There is!

Not only that, but D sounds like a better language for me to learn than C++
(for MY needs) because it has a lot of features - like garbage collection, it
runs fast, it has a C interface, and just looking at the code as a total
novice, the examples in D are MUCH easier to read than the corresponding C++.
 Other enticing nuggets are things like "your code won't compile unless it's
written correctly"...

So, my question is THIS:

Can I write a "windows" DLL file in D that would have functions that can be
accessible from a compiled C++ program?  (Actually, in this case, it's
UnrealScript that is compiled into a C++ exe.)

Epic gives this example to show how to hook up a windows dll to unrealscript:

Uscript:

class TestDLLPlayerController extends PlayerController
   DLLBind(TestDLL);

dllimport final function CallDLL1(out string s);
dllimport final function vector CallDLL2(float x, float y, float z);
dllimport final function bool CallDLL3(string s, int i[2], out float f, out
vector v);

DLL Code:

extern "C"
{
   struct FVector
   {
      float x,y,z;
   };

   __declspec(dllexport) void CallDLL1(wchar_t* s)
   {
      MessageBox(0, s, L"Inside the DLL: CallDLL1", MB_OK);
      // reverse the out parameter string
      int len = wcslen(s);
      for(int i=0; i<len>>1;i++)
      {
         wchar_t temp = s[i];
         s[i] = s[len-i-1];
         s[len-i-1] = temp;
      }
   }

   __declspec(dllexport) FVector* CallDLL2(float x, float y, float z)
   {
      static FVector result;   // declared static so that the struct's memory
is still valid after the function returns.
      result.x = x;
      result.y = y;
      result.z = z;
      return &result;
   }

   __declspec(dllexport) bool CallDLL3(wchar_t* s, int i[2], float* f, FVector*
V)
   {
      wchar_t temp[1024];
      swprintf_s(temp, 1024, L"string: %s, i: {%d,%d}, float: %f, V was
(%f,%f,%f)", s, i[0], i[1], *f, V->x, V->y, V->z);
      V->x = (float)i[0];
      V->y = (float)i[1];
      V->z = (*f);
      return (MessageBox(0, temp, L"Inside the DLL: CallDLL3", MB_OKCANCEL) ==
IDOK);
   }
}

I know I am a noob at code, but the first line "extern "C" seems to be saying
that the DLL is binding to an external "C" application. (not C++ per se) and
while "D" is not really cross compatible with C++, I read somewhere that it
CAN be used with "C".

Additional technical details can be found on this webpage:

http://udn.epicgames.com/Three/DLLBind.html

NOTE: UDK now supports 64bit code and 64bit DLLs.

So, my final question is this:

Do you think it would be worthwhile for me to both learn and use "D" for this
specific type of application?  (noting the programming notes on the linked
page).

Thanks!  =)
Sep 18 2011
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
lsc9x:

 So, I was poking around looking at C++ books and I wondered "Gee, I know there
 were languages A, C, C, C++ and C#, I wonder if there is a "D" language.
 
 Surprise!  There is!
 
 Not only that, but D sounds like a better language for me to learn than C++

The best language of the world will be Z, with a working compiler released about in 2150 dC :-)
 Can I write a "windows" DLL file in D that would have functions that can be
 accessible from a compiled C++ program?

That interface looks C. I think you have to create two little D functions to manage those UFT16 3-memeber-struct strings. I think you don't need to learn the whole D to try to do what you want to do. It's a small experiment, that I think is worth doing. But I think eventually you will have to learn C++ too, it's hard to program games today without C++ knowledge. Bye, bearophile
Sep 19 2011
prev sibling parent reply Trass3r <un known.com> writes:
 So, my question is THIS:

 Can I write a "windows" DLL file in D that would have functions that can  
 be accessible from a compiled C++ program?  (Actually, in this case, it's
 UnrealScript that is compiled into a C++ exe.)

It's perfectly possible to create DLLs with D, also see http://www.d-programming-language.org/dll.html I already successfully used a D dll with C interface to create Matlab plugins as well as a D dll exposing a COM interface to hook DirectDraw.
Sep 19 2011
parent lsc9x <lscarrozzi hotmail.com> writes:
Excellent!

Thanks so much for your feedback!.  I am excited to get into C++ and D now.
Sep 19 2011