C++ Usage Kismet Array Library

I want to use c++ equivalent of Blueprint Set Array Elem Node, as far as understand this is KismetArrayLibrary::GenericArray_Set static function, but I do not clearly understand ho to setup FArrayProperty as argument for this method. Could someone help me with the example if target should be TArray<FTransform>

The short answer is: You don’t.

Instead you should be calling the TArray member functions like Add, Push & Remove or using the [ ] operator to access the elements.

I suspect this method has its advantages, it allows you to avoid some additional checks, does not bother with initialization, control over the size of the array.

Short answer: Maybe I should have used stronger language: You can’t.

But that’s not entirely true either, there’s probably some way to do it. It just isn’t worth the effort over calling the actual member functions.

I’m not sure what you mean by advantages. All of those things are either done Blueprint side, so calling them from native won’t have the same benefits (ie avoiding additional checks) or are more clearly expressed using the actual member functions.

The only reason the Kismet functions exist is to deal with the template nature of the TArray. But in native, the C++ compiler already does that.

There are versions of insert and add that you can call to add uninitialized instances to an array if that’s what you really need. However it would be more correct to just construct the instance of the structure that you need and add it to the array. You could do this with a temporary on the stack (which the compiler would probably optimize away, or you could directly construct it in the array using the Emplace function (although I think that’s limited to adding to the end of the array).

Any suspicions about advantages are largely outweighed by the fact that those functions aren’t designed to be called directly in native. They’re designed to be called from the Blueprint VM.

While I wouldn’t discourage anyone from using Blueprint functions to understand how to do something in native, it’s not always a good idea to just write C++ as a bunch of calls to the same functions called by Blueprint. Almost always there is a better version that should be used instead that is more efficient, more readable or more powerful/flexible. If you’re going to write C++ it’s worth the effort to write actual C++ and not just a faux-Blueprint in C++.

2 Likes

To reproduce what the BP function is doing, all you need is:

if (Index >= Array.Num())
    Array.SetNum(Index + 1);
Array[Index] = Value;

Assuming your Index is >= 0 and you do in fact want to “Size to Fit”. Looking at the kismet code, what I’ve written above is quite likely to run faster, especially if you’re compiling development or shipping. There are ways to optimize that further, but in general you won’t need to.

1 Like