x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Modifying the value of an FString in an array with reflection.

I'm using reflection to get a uproperty which is an array of strings using:

 bool UFunctionLibrary::SetStringArrayElementByName(UObject * Target, FName VarName, int32 index, FString NewValue)
 {
 
     if (Target)
     {
         TArray<FString> FoundArray;
         UArrayProperty* StringArray = FindField<UArrayProperty>(Target->GetClass(), VarName);  
         TArray<FString> outArray = *StringArray->ContainerPtrToValuePtr<TArray<FString>>(Target);
         outArray[index] = NewValue;

This doesn't appear to modify the original array, however. What I'm trying to figure out, is how to change the value at index to the value of the string NewValue

I've seen lots of posts about using ContainerPtrToValuePtr, and I've been able to return a modified array as a & parameter, but I can't figure out how to modify the original array property. I'm trying to create a generic string array editor.

I was greatly helped by this: http://shootertutorial.com/2016/03/20/get-set-variables-by-name/

And ultimately, I'm trying to create a new SetStringArrayElementByName UFUNCTION to take an index, target, a uproperty name, and a string value, and make the change.

Product Version: UE 4.19
Tags:
more ▼

asked Sep 21 '18 at 11:59 AM in C++ Programming

avatar image

G_t_Pianoman
3 1 3

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

You should try without dereferencing the pointer to the array into a variable ( it make a copy of it )

  TArray<FString>* outArray = StringArray->ContainerPtrToValuePtr<TArray<FString>>(Target);
 (*outArray)[index] = NewValue;

not 100% sure, but that's how i would do it !

more ▼

answered Sep 21 '18 at 12:39 PM

avatar image

Firefly74
1.3k 13 17 31

avatar image G_t_Pianoman Sep 24 '18 at 02:01 PM

Brilliant! That worked like a charm. Thank you! For anyone up against this in the future, this is the whole code:

 bool UFunctionLibrary::SetStringArrayElementByName(UObject * Target, FName VarName, int32 index, FString NewValue)
 {
 
     if (Target)
     {
         TArray<FString> FoundArray;
         UArrayProperty* StringArray = FindField<UArrayProperty>(Target->GetClass(), VarName);  
         TArray<FString>* outArray = StringArray->ContainerPtrToValuePtr<TArray<FString>>(Target);
         (*outArray)[index] = NewValue;
         return true;
     }
 
     return false;
 }
(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question