Can someone show my how to use FInstancedStruct please

i’ve been trying in c++ but it doesn’t seem to be recognized, although i think my Visual Studios is bugged because it also doesnt recognize GENERATED_BODY anymore when it did earlier with no changes.

is no one using this?
its a new feature so not much documentation but seems extremely powerful

FInstancedStruct is just a wrapper of a struct type and data that points to a struct of that type

In C++, you can easily use it like so:

// Create a new instanced struct that contains a vector
FInstancedStruct TestStruct = FInstancedStruct::Make(FVector::UpVector);

// Read an instanced struct as a vector, if you are 100% sure it contains a vector
const FVector& TestVector = TestStruct.Get<FVector>();

// Test if an instanced struct is a vector
if (const FVector* TestVector = TestStruct.GetPtr<FVector>())
{
   // It contains a vector
}

// This would fail in this example, since Test contains an FVector
if (const FVector* TestVector = TestStruct.GetPtr<FRotator>())
{
   // Would not go in here
}


// You can also use the mutable versions if you want to modify the content in the instanced struct
Test.GetMutablePtr<FVector>();
Test.GetMutable<FVector>();

For Blueprints, you can define an instanced struct like so:

UPROPERTY(EditAnywhere, BlueprintReadWrite)
FInstancedStruct MyInstancedStruct;

You can use the BaseStruct meta specifier to limit the drop down menu in the editor, for example:

USTRUCT(BlueprintType)
struct FMyStruct
{
   GENERATED_BODY()

   UPROPERTY(EditAnywhere, BlueprintReadWrite)
   int MyInt = 0;
}

// This will show a drop down in the editor, containing only MyStruct and its children structs
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (BaseStruct = "/Script/MyModule.MyStruct"))
FInstancedStruct MyInstancedStruct;

There are blueprint functions as well to handle instanced structs:
SetInstancedStructValue and GetInstancedStructValue

Those will have a wildcard pin, to which you can connect a Break or Make struct nodes (or any other struct properties from other nodes). The GetInstancedStructValue node has 2 execution paths, for success and failure. If the passed Instanced struct is not of the connected struct type, then the Fail path will be executed

You can use Instanced structs as function parameters as well if you wish, and convert the struct to your expected type inside the function

Instanced structs also support replication out of the box

6 Likes

thanks, when i did it, it kept saying FInstancedStruct was an unknown type i had included instancedstruct.h am i missing anything else?

You need to add StructUtils to your build.cs file and include InstancedStruct.h wherever you use instanced structs, but that should be all

2 Likes

thanks for your help, got it working, had to include StructUtils.h and InstancedStruct.h

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.