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

void UCustomK2Node::PinConnectionListChanged(UEdGraphPin* Pin) {…

  1. User

    void UCustomK2Node::PinConnectionListChanged(UEdGraphPin* Pin) { Super::PinConnectionListChanged(Pin);

    UScriptStruct* StructType = Cast<UScriptStruct>(Pin->PinType.PinSubCategoryObject.Get());
    
    if ( StructType )
    {
        void* StructMemory = FMemory::Malloc(StructType->GetStructureSize());
    
        if ( StructMemory )
        {
            // Инициализируем структуру (это вызовет конструктор и установит значения по умолчанию)
            StructType->InitializeStruct(StructMemory);
    
            FStructOnScope DefaultStruct(StructType);
    
            // Если вы знаете точный тип структуры, вы можете привести указатель
            FPathToScenarioObject* MyStructInstance = static_cast<FPathToScenarioObject*>(StructMemory);
    

    } как мне скопировать данные StructType в MyStructInstance?

  2. Assistant

    To copy data from StructType to MyStructInstance, you can use the CopyScriptStruct method provided by the UScriptStruct class. This method allows you to copy the contents of one struct instance to another. Here's how you can modify your code to achieve this:

    void UCustomK2Node::PinConnectionListChanged(UEdGraphPin* Pin)
    {
        Super::PinConnectionListChanged(Pin);
    
        UScriptStruct* StructType = Cast<UScriptStruct>(Pin->PinType.PinSubCategoryObject.Get());
    
        if (StructType)
        {
            void* StructMemory = FMemory::Malloc(StructType->GetStructureSize());
    
            if (StructMemory)
            {
                // Initialize the struct (this calls the constructor and sets default values)
                StructType->InitializeStruct(StructMemory);
    
                // Create a default instance of the struct on the scope
                FStructOnScope DefaultStruct(StructType);
    
                // Copy the default values from the default struct to your allocated memory
                StructType->CopyScriptStruct(StructMemory, DefaultStruct.GetStructMemory());
    
                // If you know the exact type of the struct, you can cast the pointer
                FPathToScenarioObject* MyStructInstance = static_cast<FPathToScenarioObject*>(StructMemory);
    
                // Now MyStructInstance contains the copied data from StructType
            }
        }
    }
    

    Explanation:

    1. Initialize the Struct Memory:

      • You allocate memory for the struct using FMemory::Malloc.
      • You initialize the struct at that memory location using InitializeStruct, which sets up the struct with default values.
    2. Create a Default Struct Instance:

      • FStructOnScope is used to manage the lifetime of a struct instance. It allocates memory and ensures proper initialization and destruction.
      • By creating a FStructOnScope DefaultStruct(StructType);, you get a default-initialized instance of your struct.
    3. Copy the Struct Data:

      • Use CopyScriptStruct to copy the data from the default struct instance to your allocated memory.
      • StructType->CopyScriptStruct(StructMemory, DefaultStruct.GetStructMemory()); copies the contents from the source (DefaultStruct.GetStructMemory()) to the destination (StructMemory).
    4. Accessing the Struct Instance:

      • After copying, you can cast StructMemory to your specific struct type (FPathToScenarioObject*) to access its members.

    Note: Remember to properly free the allocated memory when it's no longer needed to prevent memory leaks. You should also call StructType->DestroyStruct(StructMemory); before freeing the memory if the struct contains complex members that need proper destruction.

    Example of Cleaning Up:

    // When done with the struct
    StructType->DestroyStruct(StructMemory);
    FMemory::Free(StructMemory);
    

    By following these steps, you can successfully copy data from StructType to MyStructInstance.

    Reference:

    Use StructType->CopyScriptStruct(StructMemory, DefaultStruct.GetStructMemory()); to copy the data from StructType into MyStructInstance.