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"

Can I cast FScriptArray to TArray?

Is good this practice casting FScriptArray to TArray if I'm sure that UProperty has array defined as specified TArray template?

 const FScriptArray& script_value = ArrayProp->GetPropertyValue_InContainer(StructData);
 FScriptArray& v = const_cast<FScriptArray&>(script_value);
 TArray<uint8>* value = reinterpret_cast<TArray<uint8>*>( &v );

TArray and FScriptArray are not interconnected by inheritance or something else. But has similar allocation in memory (as trick). Me right?

Product Version: UE 4.17
Tags:
more ▼

asked Aug 31 '17 at 12:29 PM in C++ Programming

avatar image

broly
529 40 61 88

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

2 answers: sort voted first

Hi,

TArray and FScriptArray are layout compatible, and are required as a basis for the property system. There is also FScriptMap, FScriptSet and FScriptSparseArray which are layout compatible with TMap, TSet and TSparseArray respectively.

However, while it's always going to be the case that these FScript* classes have this compatibility, it's generally not recommended to use them, as they are more an implementation detail of the UE4 reflection system. Correctly reading and manipulating elements within containers and honouring object lifetimes is considerably more involved than just casting to those classes and using them. What is your use case?

Steve

more ▼

answered Aug 31 '17 at 01:05 PM

avatar image

Steve Robb STAFF
2.8k 58 27 99

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

I can't comment answers on my question, so I post comment as answer for you, Steve Robb.

My case is serialization exception where I serializing specific struct with TArray field. I need to get this field as TArray:

 // This struct is FMyStruct?
 if (StructStructure->GetName() == GetNameSafe(FMyStruct::StaticStruct())
 {
       // And then find field "Data" in this struct
       if ( UProperty* field = FindField<UProperty>(StructStructure, "Data") )
       {
            // This is TArray<uint8>, I need to cast to property value to TArray:
            const FScriptArray& script_value = field->GetPropertyValue_InContainer(StructData);
            ...
       }
 }

more ▼

answered Sep 04 '17 at 12:24 PM

avatar image

broly
529 40 61 88

avatar image Steve Robb STAFF Sep 04 '17 at 04:13 PM

If you absolutely know your field is a TArray<uint8>, then you can just cast to that as in your original post, without any problem.

If you want to be certain, you can add some extra checks:

 UArrayProperty* field = FindField<UArrayProperty>(StructStructure, "Data");
 if (field && field->Inner->IsA<UByteProperty>())
 {
     const TArray<uint8>& byte_array = (const TArray<uint8>&)field->GetPropertyValue_InContainer(StructData);
 }

Steve

(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