Actor->ProcessEvent with dynamic list of arguments

Hello,

I’m trying to call Actor->ProcessEvent(UFunction * Function, void * Parms).
I have the UFunction* pointer and i’m trying to create the void* Params like so:

    unsigned char *buf = (unsigned char*) FMemory::Malloc(memSize);
    int index = 0;
    for (int i = 0; i < nrOfParameters; i++)
    {
        if (param is int)
        {
            FMemory::Memcpy(buf + index, &someInt, sizeof(int));
            index += sizeof(int);
        }
        if (param is float)
        {
            FMemory::Memcpy(buf + index, &someFloat, sizeof(float));
            index += sizeof(float);
        }
        if (param is vector)
        {
            FMemory::Memcpy(buf + index, &someVector, sizeof(FVector));
            index += sizeof(FVector);
        }
    }

    Actor->ProcessEvent(theFunction, buf);
FMemory::Free(buf);

This works great. The problem is when one of the parameters is string. I don’t know what to do in that case.
What I have now is this:

FString someString = "TestString"
int size = (someString .Len() + 1) * sizeof(TCHAR);

FMemory::Memcpy(buf + index, &someString , size);
index += size;

When I call ProcessEvent with a buf that includes a FString (added like so), it crashes. Can anyone help pls ? How do you store a FString inside a unsigned char * buffer ?

I may be 4 years late, but I struggled with this and figured it out.

First of all, instead of keeping up with an buffer cursor manually, you could be letting the reflection system do that for you using TFieldIterator::ContainerPtrToValePtr. The other part of this is that FString does not hold the actual string on the stack - it basically comes down to being a glorified const wchar_t * depending on what macros are active. Given that, you are going in the wrong direction by using FMemory::Memcpy at all - you can just assign the string to the right place in the buffer directly.

#Example:

I had figured this out targeting a function that took two FString parameters:

void MyObject::ArgTest(const FString& arg1, const FString& arg2)

Just assume I already have an array of two defined FStrings ready, the execution looked like this:

// this has the two strings I want to pass
// (FYI I could just cast this to void* and pass it as 
// the ProcessEvent args but to demonstrate I'll do the whole process)
FString strings[2];

UFunction* targetFunction = FString(TEXT("ArgTest"));
if (targetFunction)
{

	void* buf = FMemory_Alloca(targetFunction->ParmsSize);
	FMemory::Memzero(buf, targetFunction->ParmsSize);
	int i = 0;
	for (TFieldIterator<FProperty> PropertyIterator(targetFunction); PropertyIterator; ++PropertyIterator, i++)
	{

		FString* p = PropertyIterator->ContainerPtrToValuePtr<FString>(buf);
		*p = strings[i];
	}
	
	ProcessEvent(targetFunction, (void*)buf);

}

This functioned properly with many different sized strings.