void UCustomK2Node::PinConnectionListChanged(UEdGraphPin* Pin) {…
- 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?
- Assistant
To copy data from
StructTypetoMyStructInstance, you can use theCopyScriptStructmethod provided by theUScriptStructclass. 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:
-
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.
- You allocate memory for the struct using
-
Create a Default Struct Instance:
FStructOnScopeis 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.
-
Copy the Struct Data:
- Use
CopyScriptStructto 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).
- Use
-
Accessing the Struct Instance:
- After copying, you can cast
StructMemoryto your specific struct type (FPathToScenarioObject*) to access its members.
- After copying, you can cast
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
StructTypetoMyStructInstance.Reference:
UScriptStruct::CopyScriptStruct: Copies the value of one script struct instance to another.
Use
StructType->CopyScriptStruct(StructMemory, DefaultStruct.GetStructMemory());to copy the data fromStructTypeintoMyStructInstance. -