Game Development Community

TGE compiled with /CLR

by Jody Byrd · in Torque Game Engine · 08/26/2006 (8:41 pm) · 8 replies

The Goals:

1) Compile the TGE 1.4 so that I can call a function inside a custom vb.net dll

2) Make sure I can use the debugger in both TGE and my DLL at the same time.


The Reasons:

1) I don't have a game to play this weekend.

2) I don't want to code on any of my games this weekend.

3) Prove to myself I still know something about C++ (I've haven't used C++ since VS2002)

4) I can't stand the fact TorqueScript doesn't have a editor. (I like a single IDE environment when I code)


The Approach:

1) I'm using the TGE 1.4 I got from my account download. No upgrades from the HEAD.

2) I'm using VS 2005 Pro. with the free May 2006 MSDN library.

3) Actually write down what I did to make it work

4) Post it on the forums for my own personal record, and so others could benefit.

#1
08/26/2006 (8:45 pm)
//The solution setup

Step 1: Copied the whole torque directory to a new folder.

Step 2: Downloaded the VS2005 project patch. Applied that.

Step 3: Removed all the Tool projects from the Solution

Step 4: Renamed TorqueDemo to TorqueEngine. That way I know its my exe.

Sanity compile. Rebuild everything. Look for TorqueEngine.exe in my Examples folder.
#2
08/26/2006 (9:04 pm)
//Changing the Project Complie Properties

Step 5: Switch solution to Debug Mode.

Step 6: Went to "Properties" of TorqueEngine project, under "Configuration Properties", "General", switched
"Common Language Runtime Support" to "/clr"

Note: /clr:pure and /clr:safe have issues that might need to be address, however I just ignored them for this build.

(snip: long story of trial and error compiling)

Step 7: (Still on the "Properties" window) Under "Debugging", set the "Working Directory" to "../example"

Step 8: (Still on the "Properties" window) Under "C++", "General", set "Resolve #using References" to "../example"
this sets the path for the "#using" statements.

Step 9: (Still on the "Properties" window) Under "C++", "Code Generation", set "Runtime Library" to "/MDd"
.Net requires the dynamic version (/MD) of the libraries, not the static (/MT)

Click "Ok" to save the changes
#3
08/26/2006 (9:12 pm)
//Creating my VB.Net dll

Step 10: Add new vb.net class library to my solution, I called it VB8Dll

Step 11: Add a new Class (or rename the default Class1), I called mine JodysClass.vb

Step 12: Add a function that returns a int32.

The code:

Public Class JodysClass

    Public Function TheFunction() As Int32

        Return 123 

    End Function

End Class
Step 13: Back to the "Properties" of TorqueEngine,
..under "Common Properties", "References",
..click"Add New Reference"
..click "Project"
..select your custom dll project (Mine was VB8Dll)
..click "OK"
..click "OK" to save changes

The IDE will now copy VB8dll.dll into the "../examples" folder for you.
#4
08/26/2006 (9:19 pm)
//Creating the managed extension file

Step 14: Back in TorqueEngine, add a new cpp file, I called my JodysCalls.cpp

Step 15: Add
#pragma managed

This tells VS2005 that this file should be compiled under managed code,

Note: "#pragma unmanaged" might be needed in all the native only files, but I skipped that step.

Step 16: Add
#using <mscorlib.dll>
#using <system.dll>

required for managed code.

Step 17: Add
#using "VB8dll.dll"

The reference to our file.

Step 18: Add

using namespace System;
using namespace VB8dll;

Step 19: Add the interface function
int MyCall()
{

JodysClass d;

int i;

i=d.TheFunction();

return i;

}
#5
08/26/2006 (9:26 pm)
//Calling the Interface function

Note: I chose to add this where the Console is processed. The theory being that I would call my vb.net code right after the Torquescript is processed. However, this is only a concept demo, and I'm not doing anything important but returning a int value, so this code could be anywhere in the TGE.

Step 20: We need a reference to our interface function in the JodysCalls.cpp file.

In winWindow.cc

..Find "void Platform::process()"

..just before the that line, add

extern int MyCall();

Step 21: Inside the "process()" function, add

int i;

i=MyCall();

This calls the interface function.
#6
08/26/2006 (9:32 pm)
Now, set some break points and run the TorqueEngine in debug mode.

::process() calls MyCall()

MyCall() calls TheFunction() inside vb.net

returns 123

All inside the same IDE.



Thanks for reading,
Jody
#7
06/22/2009 (12:35 am)
Greate resource!
#8
06/22/2009 (11:23 am)
great!, some time ago, ill try to make the same thing but using c#, the engine compile but when i try to call the c# function the engine crash.