Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

unreal engine 5 c++ у меня есть моя нода uk2node, которая принимает в качестве Pin…

  1. User

    unreal engine 5 c++ у меня есть моя нода uk2node, которая принимает в качестве Pin структуру, внутри этой структуры есть массив Path и мне его нужно получить, когда к ноде присоединяют объект структуры не во время выполнения! Массив не пустой, там есть данные, которые мне нужны. void UCustomK2Node::PinConnectionListChanged(UEdGraphPin* Pin) { Super::PinConnectionListChanged(Pin);

    UScriptStruct* StructType = Cast<UScriptStruct>(Pin->PinType.PinSubCategoryObject.Get());
    
    if ( StructType )
    {
    
        FProperty* Prop = StructType->FindPropertyByName(FName("Path"));}
    
  2. Assistant

    To access the data inside the Path array of the struct connected to your node's pin at edit-time (not runtime), you need to traverse the connected nodes and extract the default values where possible. Here's how you can do it:

    1. Check if the Pin Has Connections: First, verify if the pin is connected to another node. If it's connected, you can attempt to retrieve the value from the connected node.

      void UCustomK2Node::PinConnectionListChanged(UEdGraphPin* Pin)
      {
          Super::PinConnectionListChanged(Pin);
      
          // Check if the pin is connected to another pin
          if (Pin->LinkedTo.Num() > 0)
          {
              UEdGraphPin* ConnectedPin = Pin->LinkedTo[0]; // Assuming single connection
              UEdGraphNode* ConnectedNode = ConnectedPin->GetOwningNode();
      
              // Proceed based on the type of the connected node
          }
          else
          {
              // Handle case where the pin is not connected
          }
      }
      
    2. Identify the Connected Node Type: Determine what type of node is connected to your pin. If it's a UK2Node_MakeStruct, you can extract the default values from it.

      if (UK2Node_MakeStruct* MakeStructNode = Cast<UK2Node_MakeStruct>(ConnectedNode))
      {
          // Proceed to extract the struct data
      }
      else
      {
          // Handle other node types or notify that the value cannot be retrieved at edit-time
      }
      
    3. Extract Struct Data from UK2Node_MakeStruct: If the connected node is a MakeStruct node, you can extract the default values assigned within it.

      if (MakeStructNode)
      {
          UScriptStruct* StructType = MakeStructNode->StructType;
      
          // Allocate memory for the struct and initialize it
          void* StructMemory = FMemory::Malloc(StructType->GetStructureSize());
          StructType->InitializeStruct(StructMemory);
      
          // Populate the struct with values from the node's pins
          for (UEdGraphPin* StructPin : MakeStructNode->Pins)
          {
              if (StructPin->Direction == EGPD_Input && StructPin->PinName != UEdGraphSchema_K2::PN_StructOut)
              {
                  // Find the property corresponding to this pin
                  FProperty* StructProperty = StructType->FindPropertyByName(StructPin->PinName);
                  if (StructProperty)
                  {
                      // Set the property value based on the pin's default value
                      const FString& DefaultValue = StructPin->DefaultValue;
                      StructProperty->ImportText(*DefaultValue, StructProperty->ContainerPtrToValuePtr<void>(StructMemory), PPF_None, nullptr);
                  }
              }
          }
      
          // Access the 'Path' array property
          FArrayProperty* ArrayProp = FindFProperty<FArrayProperty>(StructType, FName("Path"));
          if (ArrayProp)
          {
              FScriptArrayHelper ArrayHelper(ArrayProp, ArrayProp->ContainerPtrToValuePtr<void>(StructMemory));
      
              // Iterate over the array elements
              for (int32 Index = 0; Index < ArrayHelper.Num(); ++Index)
              {
                  void* ElementPtr = ArrayHelper.GetRawPtr(Index);
                  // Cast ElementPtr to the appropriate type and use the data
              }
          }
      
          // Clean up
          StructType->DestroyStruct(StructMemory);
          FMemory::Free(StructMemory);
      }
      
    4. Handle Variables and Other Node Types: If the connected node represents a variable or another node type, accessing its value at edit-time might not be feasible since the actual value is only determined at runtime.

      else if (UK2Node_VariableGet* VariableGetNode = Cast<UK2Node_VariableGet>(ConnectedNode))
      {
          // Attempt to get the default value of the variable
          FName VariableName = VariableGetNode->GetVarName();
      
          // Access the Blueprint's class default object (CDO)
          if (UBlueprint* Blueprint = FBlueprintEditorUtils::FindBlueprintForNodeChecked(VariableGetNode))
          {
              UObject* CDO = Blueprint->GeneratedClass->GetDefaultObject();
      
              // Find the property and get its value
              FProperty* VariableProperty = Blueprint->GeneratedClass->FindPropertyByName(VariableName);
              if (VariableProperty)
              {
                  void* PropertyValue = VariableProperty->ContainerPtrToValuePtr<void>(CDO);
      
                  // If it's the struct we're interested in
                  if (UStructProperty* StructProp = Cast<UStructProperty>(VariableProperty))
                  {
                      // Access the 'Path' array within the struct
                  }
              }
          }
      }
      
    5. Limitations: Remember that not all values can be obtained at edit-time. If the variable's value is set during gameplay or through complex Blueprint logic, you won't be able to access it in the editor. In such cases, you might need to redesign your approach or provide editor-time defaults.

    6. Alternative Approach: If accessing the data at edit-time is critical and the above methods are insufficient, consider implementing a custom editor utility or tool that serializes required data during design time.


    Note: The code snippets above are illustrative and may require adjustments to fit your specific implementation and Unreal Engine version. Always ensure you handle memory management and pointer operations carefully to avoid crashes.


    By traversing the connected nodes and extracting default values where possible, you can access the data inside the Path array of the struct connected to your node's pin at edit-time.