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. Collections

TNativeArray

A more optimized approach to unreals arrays for doing fast copying as well as being able to share data between managed and native without marshalling costs.

[UClass]
public class AActorTest : ACharacter
{
    [UProperty(PropertyFlags.EditAnywhere)]
    public TNativeArray<int> NativeArrayTest { get; set; }

    [UProperty(PropertyFlags.EditAnywhere)]
    public TNativeArray<int> NativeArrayTest2 { get; set; }

    protected override void BeginPlay()
    {
        base.BeginPlay();

        Span<int> test = [5, 6, 7, 8];
        NativeArrayTest.CopyFrom(test);

        Span<int> test2 = stackalloc int[NativeArrayTest.Length];
        NativeArrayTest.CopyTo(test2);

        NativeArrayTest2.CopyFrom(test2);
    }
}

You can also use NativeArray without any marshalling costs

public override void Tick(float deltaSeconds)
{
    base.Tick(deltaSeconds);

    ref int MyNumber = ref NativeArrayTest.AsSpan()[0];
    MyNumber++;
}

TNativeArray can also be represented as an ReadOnlySpan inside of a UFunction

[UFunction(FunctionFlags.BlueprintCallable)]
public void TestSpanCall(ReadOnlySpan<byte> span)
{
    PrintString($"Span Length: {span.Length}");
}

Supported Types

Currently right now NativeArray only supports numerical primitive types

PreviousTArrayNextTSet

Last updated 8 months ago