UnrealSharp
  • Home
  • FAQ
  • Getting Started
    • Setup
    • Your First Script
    • Debugging
    • Packaging
  • Documentation
    • Classes
      • Properties
        • C++ Functions As C# Properties
        • C++ Properties with Getters/Setters
      • Functions
        • Flags
        • MetaData
      • Default Actor Components
      • Helper Methods
    • Structs
    • Enums
    • Interfaces
    • Delegates
    • Collections
      • TArray
      • TNativeArray
      • TSet
      • TMap
    • Multiplayer
      • Replicated Properties
      • RPCs (Remote Procedure Calls)
      • Replicated UObjects
    • Primary Data Assets
      • Loading Primary Data Assets
    • Loading Soft References
    • Trace Channels
    • Static Variables
      • FWorldStaticVar<T>
      • FGameStaticVar<T>
    • Logging
    • Async
    • Gameplay Tags
      • Gameplay Tag Container
    • Extension / Mixin Methods
    • Subsystems
    • Module Lifecycle
  • Links
    • Github Repository
    • Roadmap
    • Discord
Powered by GitBook
On this page
  1. Documentation
  2. Classes
  3. Properties

C++ Functions As C# Properties

UnrealSharp automatically generates properties for any C++ function with a Get or Set prefix, provided the function either returns a value or accepts a single parameter.

This includes Get functions that has an out parameter or just a WorldContextObjectas parameter, like shown below.

/** Returns the game instance object  */
UFUNCTION(BlueprintPure, Category="Game", meta=(WorldContext="WorldContextObject"))
static ENGINE_API class UGameInstance* GetGameInstance(const UObject* WorldContextObject);

The generator will attempt to pair corresponding getter and setter functions into a single C# property.

The following functions:

UFUNCTION(BlueprintCallable)
void SetTestValue(int32 Value);

UFUNCTION(BlueprintCallable)
int32 GetTestValue();

turns into a property in C#:

public int TestValue
{
    get
    {
        unsafe
        {
            byte* ParamsBufferAllocation = stackalloc byte[GetTestValue_ParamsSize];
            nint ParamsBuffer = (nint) ParamsBufferAllocation;
            UStructExporter.CallInitializeStruct(GetTestValue_NativeFunction, ParamsBuffer);
            
            UObjectExporter.CallInvokeNativeFunction(NativeObject, GetTestValue_NativeFunction, ParamsBuffer);
            
            int returnValue;
            returnValue = BlittableMarshaller<int>.FromNative(IntPtr.Add(ParamsBuffer, GetTestValue_ReturnValue_Offset), 0);
            
            return returnValue;
        }
    }
    set
    {
        unsafe
        {
            byte* ParamsBufferAllocation = stackalloc byte[SetTestValue_ParamsSize];
            nint ParamsBuffer = (nint) ParamsBufferAllocation;
            UStructExporter.CallInitializeStruct(SetTestValue_NativeFunction, ParamsBuffer);
            BlittableMarshaller<int>.ToNative(IntPtr.Add(ParamsBuffer, SetTestValue_Value_Offset), 0, value);
            
            UObjectExporter.CallInvokeNativeFunction(NativeObject, SetTestValue_NativeFunction, ParamsBuffer);
            
        }
    }
}
PreviousPropertiesNextC++ Properties with Getters/Setters

Last updated 5 months ago