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

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

  1. User

    unreal engine c++ у меня есть моя нода uk2node, которая принимает в Pin объект структуры, как мне создать новый объект структуры и скопировать все данные из Pin? В самой структуре мне нужен массив, который не пустой! void UCustomK2Node::PinConnectionListChanged(UEdGraphPin* Pin) { Super::PinConnectionListChanged(Pin);

    UScriptStruct* StructType = Cast(Pin->PinType.PinSubCategoryObject.Get());}

  2. Assistant

    In Unreal Engine's blueprint system, when working with UK2Node and pins that represent struct types, you may need to create an instance of a struct and possibly copy data associated with a pin. However, it's important to note that UEdGraphPin instances themselves do not hold the actual struct data; they represent connections and types in the blueprint graph.

    Here's how you can create a new instance of a struct given a UScriptStruct*, and how you might approach copying data if available.

    Creating a New Instance of a Struct

    Since UScriptStruct represents the type information of a struct at runtime, you can use FStructOnScope to create an instance of the struct dynamically:

    #include "UObject/StructOnScope.h"
    
    void UCustomK2Node::PinConnectionListChanged(UEdGraphPin* Pin)
    {
        Super::PinConnectionListChanged(Pin);
    
        UScriptStruct* StructType = Cast<UScriptStruct>(Pin->PinType.PinSubCategoryObject.Get());
        if (StructType)
        {
            // Create a new instance of the struct
            FStructOnScope StructInstance(StructType);
    
            // Optionally, initialize array members or default values
            void* StructData = StructInstance.GetStructMemory();
            StructType->InitializeStruct(StructData);
    
            // If you have default values or source data, you can copy them here
    
            // Remember to clean up if you're manually managing memory
            // However, FStructOnScope will handle destruction automatically when it goes out of scope
        }
    }
    

    FStructOnScope is a utility that handles allocation and deallocation of struct memory and ensures proper constructor and destructor calls for the struct.

    Copying Data from the Pin

    As mentioned, UEdGraphPin does not contain the actual data for the struct, but it may contain a default value in the form of a string (DefaultValue, DefaultObject, or DefaultText). For complex data like structs, the default value is often stored as a JSON string in DefaultValue.

    However, copying data directly from the pin's default value can be non-trivial, especially for structs with arrays or other nested data structures. Here's how you might approach it:

    if (Pin->DefaultObject)
    {
        // For object references
        UObject* DefaultObject = Pin->DefaultObject;
        // Cast and copy as needed
    }
    else if (!Pin->DefaultValue.IsEmpty())
    {
        // For structs, the DefaultValue might be a string representation
        FString DefaultValueString = Pin->DefaultValue;
    
        // Deserialize the string into the struct instance
        void* StructData = StructInstance.GetStructMemory();
        StructType->ImportText(*DefaultValueString, StructData, nullptr, PPF_None, GWarn);
    }
    else
    {
        // If the pin is connected to another pin, you might need to evaluate the connected node to get the data
        if (Pin->LinkedTo.Num() > 0)
        {
            // Get the connected pin
            UEdGraphPin* ConnectedPin = Pin->LinkedTo[0];
            // Further processing to get data from the connected node
        }
        else
        {
            // Handle the case where no default value or connection exists
        }
    }
    

    Important Notes:

    1. Deserialization: When importing text into the struct, ensure that the format of DefaultValue matches what ImportText() expects for that struct type.

    2. Runtime vs. Design Time: Keep in mind that blueprint pins and their default values are primarily used by the blueprint compiler and editor. If you're trying to work with actual data at runtime, you'd typically do so in an instance of a blueprint or component, not within the UK2Node class.

    3. Arrays and Nested Structures: If your struct contains arrays or nested structs, you need to ensure that they are properly initialized and that any deserialization or copying handles them correctly.

    Alternative Approach

    If your goal is to manipulate or access the data that flows through the blueprint nodes at runtime (e.g., during gameplay), you might need to implement this logic elsewhere, such as within a custom function or component class.

    Example of Handling Struct with Non-Empty Array

    Suppose your struct is defined as:

    USTRUCT(BlueprintType)
    struct FMyStruct
    {
        GENERATED_BODY()
    
        UPROPERTY(EditAnywhere, BlueprintReadWrite)
        TArray<int32> MyArray;
    };
    

    To ensure that MyArray is not empty when you create a new instance:

    if (StructType->IsChildOf(FMyStruct::StaticStruct()))
    {
        FStructOnScope StructInstance(StructType);
        void* StructData = StructInstance.GetStructMemory();
        StructType->InitializeStruct(StructData);
    
        FMyStruct* MyStructInstance = static_cast<FMyStruct*>(StructData);
        // Initialize the array
        MyStructInstance->MyArray = {1, 2, 3}; // Example data
    
        // Continue with your logic
    }
    

    Summary

    • Use FStructOnScope to create and manage an instance of a struct dynamically.
    • Be cautious when attempting to copy data from UEdGraphPin as pins don't hold actual data.
    • For default values, consider deserializing DefaultValue if appropriate.
    • Ensure proper initialization of arrays and nested structures within your struct.
    • Remember that manipulation of actual data typically occurs at runtime, not within the UK2Node itself.

    Additional Resources

    • Struct Reflection: Unreal Engine Documentation - Reflection System
    • FStructOnScope: Look into the source code for FStructOnScope for more details on its usage.
    • Serialization and Deserialization: Learn how to use ExportText() and ImportText() for serializing structs to and from strings.

    If you provide more details about what you're trying to achieve (e.g., where the data is coming from, what the struct contains, and how it should be initialized), I could offer a more precise solution.