Search code examples
c#.netunrealscript

Using .NET with UnrealScript


UDK uses .NET. So maybe it is possible somehow to use .NET from UnrealScript?
It is really great to use C# with UnrealScript.

One certainly could build C++ layer to interact between .NET and UnrealScript which will use dllimport, but it is not the subject for this question.


Solution

  • So there seems to be no way to access .NET libraries directly from UnrealScript, but one can combine [DllExport] extension for C# and UnrealScript interop system to interact with .NET without intermediate C++ wrapper.

    Lets look at simple example of exchanging with int, string, structure and filling UnrealScript String in C#.

    1 Create C# class

    using System;
    using System.Runtime.InteropServices;
    using RGiesecke.DllExport;
    namespace UDKManagedTestDLL
    {
    
        struct TestStruct
        {
            public int Value;
        }
    
        public static class UnmanagedExports
        {
            // Get string from C#
            // returned strings are copied by UnrealScript interop system so one
            // shouldn't worry about allocation\deallocation problem
            [DllExport("GetString", CallingConvention = CallingConvention.StdCall]
            [return: MarshalAs(UnmanagedType.LPWStr)]
            static string GetString()
            {
                return "Hello UnrealScript from C#!";
            }
    
            //This function takes int, squares it and return a structure
            [DllExport("GetStructure", CallingConvention = CallingConvention.StdCall]
            static TestStructure GetStructure(int x)
            {
                 return new TestStructure{Value=x*x};
            }
    
            //This function fills UnrealScript string
            //(!) warning (!) the string should be initialized (memory allocated) in UnrealScript
            // see example of usage below            
            [DllExport("FillString", CallingConvention = CallingConvention.StdCall]
            static void FillString([MarshalAs(UnmanagedType.LPWStr)] StringBuilder str)
            {
                str.Clear();    //set position to the beginning of the string
                str.Append("ha ha ha");
            }
        }
    }
    

    2 Compile the C# code, say, as UDKManagedTest.dll and place to [\Binaries\Win32\UserCode] (or Win64)

    3 On UnrealScript side one should place declarations of the functions:

    class TestManagedDLL extends Object
        DLLBind(UDKManagedTest);
    
    struct TestStruct
    {
       int Value;
    }
    
    dllimport final function string GetString();
    dllimport final function TestStruct GetStructure();
    dllimport final function FillString(out string str);
    
    
    DefaultProperties
    {
    }
    

    Then one can use the functions.


    The only trick is filling UDK string like it is shown in FillString method. Since we pass string as fixed length buffer, this string MUST be initialized. The length of initialized string MUST be more or equal to length C# will work with.


    Further reading could be found here.