unreal engine c++ у меня есть моя нода uk2node, которая принимает пин структуры fscenarioPath, внутри этой структуры есть массив Path и я хочу его получить, но не знаю как. Есть идеи?
void UCustomK2Node::PinConnectionListChanged(UEdGraphPin* Pin)
{
Super::PinConnectionListChanged(Pin);
To access the array Path inside your structure FScenarioPath from your custom K2 node, you can follow these steps:
Determine if the Pin Has a Default Value or Is Connected:
If the pin has a default value (i.e., it's not connected to another node), you can parse this default value to initialize an instance of your struct.
If the pin is connected to another node, you may need to traverse the connections to find the actual value at compile time.
Create an Instance of the Struct and Initialize It:
Allocate memory for an instance of your struct.
Initialize the struct with the default value from the pin, if available.
Access the Array Property:
Use the reflection system to find the property named Path within your struct.
Ensure that this property is indeed an array (FArrayProperty).
Use FScriptArrayHelper to iterate over the elements of the array.
Here’s how you can implement this in your code:
cpp
void UCustomK2Node::PinConnectionListChanged(UEdGraphPin* Pin)
{
Super::PinConnectionListChanged(Pin);
// Get the struct type from the pin
UStruct* StructType = Cast<UScriptStruct>(Pin->PinType.PinSubCategoryObject.Get());
if (StructType)
{
// Allocate memory for an instance of the struct
FStructOnScope StructOnScope(StructType);
void* StructData = StructOnScope.GetStructMemory();
// Initialize the struct instance with the default value from the pin
if (!Pin->DefaultValue.IsEmpty())
{
// Use ImportText to initialize the struct from the DefaultValue string
StructType->ImportText(*Pin->DefaultValue, StructData, nullptr, PPF_None, GLog, Pin->GetName());
// Note: Ensure that your struct supports ImportText/ExportText operations
}
// Find the 'Path' property within the struct
FProperty* Prop = StructType->FindPropertyByName(FName("Path"));
if (Prop && Prop->IsA<FArrayProperty>())
{
FArrayProperty* ArrayProp = CastField<FArrayProperty>(Prop);
// Access the array data
FScriptArrayHelper ArrayHelper(ArrayProp, ArrayProp->ContainerPtrToValuePtr<void>(StructData));
int32 NumElements = ArrayHelper.Num();
for (int32 Index = 0; Index < NumElements; ++Index)
{
// Get a pointer to each element in the array
void* ElementPtr = ArrayHelper.GetRawPtr(Index);
// Do something with ElementPtr
// For example, if the elements are of type FString:
FStrProperty* StringProp = CastField<FStrProperty>(ArrayProp->Inner);
if (StringProp)
{
FString* StringValue = reinterpret_cast<FString*>(ElementPtr);
UE_LOG(LogTemp, Log, TEXT("Element %d: %s"), Index, **StringValue);
}
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Property 'Path' not found or is not an array."));
}
// Clean up is handled by FStructOnScope
}
}
Important Notes:
Struct Initialization:
The struct must support ImportText/ExportText operations for ImportText to work. This means all properties within the struct should be simple types or structs that also support these operations.
If ImportText doesn't work out of the box, you might need to implement custom serialization for your struct.
Pin Default Value:
For complex structures, the DefaultValue string can be empty or not properly formatted. In such cases, you may need to handle the initialization manually or ensure that the default value is correctly set in the pin.
Connected Pins:
If the pin is connected to another node, the value isn't directly available at editor time because it might be computed at runtime.
You may need to analyze the connected nodes to determine the value, which can be complex and might not be feasible in all cases.
Editor vs. Runtime:
Remember that during editor scripting, you might not have access to runtime values. Operations are limited to design-time data.
Logging and Debugging:
Use UE_LOG to output debug information to help trace through your code and ensure that you are accessing the data correctly.
Additional Suggestions:
Review Your Struct Definition:
Ensure that the FScenarioPath struct is properly defined and all properties are accessible.
If Path is a user-defined type, make sure it supports necessary reflection and serialization mechanisms.
Consider the Context:
Depending on what you want to achieve, consider if parsing the default value is the best approach.
If you need to generate code or modify the behavior of your node based on the contents of Path, you might need to consider alternative designs.
Accessing array data within a struct from a pin in a custom K2 node involves using the Unreal Reflection System to inspect and manipulate property values. By creating an instance of your struct and properly initializing it, you can use FArrayProperty and FScriptArrayHelper to iterate over the array elements.
If you encounter difficulties or need further assistance, please provide more details about your FScenarioPath struct and what you intend to accomplish, so I can offer more targeted help.