When working with custom K2 nodes in Unreal Engine, it's important to understand the context in which certain functions are called and what data is available at those times.
In your implementation of UCustomK2Node::PinConnectionListChanged, you're attempting to access the actual data (specifically, the non-empty Path array) from a pin of type FScenarioPath. However, during the PinConnectionListChanged event, the actual data values of connected pins are generally not accessible, especially if the pin is connected to another node's output.
Here's why:
-
Editor-Time vs. Runtime Data: The PinConnectionListChanged function is called at editor time, during the blueprint editing process. At this point, the blueprint graph represents the connections and types but doesn't contain the actual runtime data that will be processed when the game is running.
-
Default Values: If a pin is not connected to any other node, and it represents a literal value, its default value can be accessed via Pin->DefaultValue or Pin->DefaultObject. For struct pins, the default value is often stored as a string that you can parse to reconstruct the struct.
-
Connected Pins: If a pin is connected to another node, the DefaultValue is typically empty because the value will be provided during execution by the connected node.
Given these constraints, here's what you can and cannot do:
What You Can Do:
-
Access Default Values for Unconnected Pins: If the pin is not connected and has a default value, you can parse that default value to access the struct and its array.
if (Pin->LinkedTo.Num() == 0 && !Pin->DefaultValue.IsEmpty())
{
UScriptStruct* StructType = Cast<UScriptStruct>(Pin->PinType.PinSubCategoryObject.Get());
if (StructType)
{
FStructOnScope StructData(StructType);
const TCHAR* ImportTextPtr = *Pin->DefaultValue;
StructType->ImportText(ImportTextPtr, StructData.GetStructMemory(), nullptr, PPF_None, GError, StructType->GetName());
// Access the struct data
FScenarioPath* ScenarioPath = reinterpret_cast<FScenarioPath*>(StructData.GetStructMemory());
// Now you can access ScenarioPath->Path
}
}
-
Get Type Information: You can retrieve information about the struct type and its properties, as you are already doing. This can be useful for validation or for setting up node behavior based on the type.
-
Prepare for Runtime Access: Since you can't get the actual data at editor time when the pin is connected, you should design your node to handle data access at runtime. This involves generating the appropriate node logic so that, when the blueprint is executed, it correctly processes the data flowing through the pins.
What You Cannot Do:
- Access Actual Data from Connected Pins in
PinConnectionListChanged: You cannot access the non-empty Path array from connected pins during the PinConnectionListChanged event. The data isn't available at this point because the blueprint editor only knows about the connections and types, not the runtime values.
Recommended Approach:
To work with the data that will be provided at runtime, you should focus on generating the correct behavior in your node's ExpandNode method (if you're working with macro-style nodes) or in a custom UK2Node implementation.
Here's how you might proceed:
-
Override ExpandNode: This function is called during blueprint compilation and allows you to modify the generated nodes. You can insert nodes to handle data operations at runtime.
void UCustomK2Node::ExpandNode(FKismetCompilerContext& CompilerContext, UEdGraph* SourceGraph)
{
Super::ExpandNode(CompilerContext, SourceGraph);
// Generate the necessary nodes to handle the Path array at runtime
// For example, you might get a reference to the variable or call functions on it
}
-
Use Blueprint Callable Functions: If you need to manipulate or access the Path array, consider creating a BlueprintCallable function in C++ that performs the necessary operations on FScenarioPath. Then, in your node, you can call this function.
-
Handle Data Flow at Runtime: Design your node so that it correctly handles data at runtime. This might involve ensuring that the inputs and outputs of your node are correctly typed and that any necessary processing happens when the blueprint is executed, not during editing.
Summary
-
Accessing Runtime Data at Edit Time: Directly accessing the runtime data of connected pins during the PinConnectionListChanged event isn't feasible because the data isn't available at that time.
-
Focus on Node Behavior: Instead of trying to get the data during the pin connection event, focus on ensuring your node behaves correctly at runtime by appropriately handling inputs and outputs and generating the necessary logic in the ExpandNode function.
-
Use Default Values When Applicable: If the pin isn't connected and has a default value, you can parse and use that default value at editor time.
By adjusting your approach to work within the constraints of the Unreal Engine blueprint system, you'll be able to effectively manipulate the Path array when it's actually available—at runtime.