Download

Optimising this tiny piece of code

Hello,

I’m not too clued-up on data type conversion, especially involving pointers. Is there any way to make this code more efficient? I’m creating a std::string and need it to become a const TCHAR*, but I’m not keen on the dereferencing of the string, plus c_str(). It seems a bit convoluted for passing a simple string.


    std::string path = "StaticMesh'/Game/Items/Models/";
    path += model;
    path += "/";
    path += model;
    path += ".";
    path += model;
    path += "'";

    ConstructorHelpers::FObjectFinder<UStaticMesh> BaseMeshAsset(*FString(path.c_str()));


How many times is this piece of code being called? If it is only called a few times, don’t worry about optimizing it. Premature optimization is the root of all evil.

It’s being called very minimally - twice in the whole game, actually. My point is that I want to know how to optimise that so I know how to do it efficiently in case there’s a much more intensive need for it in the future.

hey @AntiRix_ to begin with, you shouldn’t be using std::strings, just use the normal FString. it has all the functionality you will ever need. In general avoid any use of the standard libraries.
if you want to know more about string converstion you might take a look of this header: StringConv.h, it has a lot of useful macros for strings handling

Optimizing too early isn’t recommended in trivial cases like this one.
However, from a readability perspective alone, there are a few things to change:
1- FString, instead of std::string. Is there even one usecase where std is useful in UE4 C++?
2- This looks like a FString::Format case to me :slight_smile:

Do you really need std::string?



 const FString path = FString::Printf(TEXT("StaticMesh'/Game/Items/Models/%s/%s.%s'"), model);
 ConstructorHelpers::FObjectFinder<UStaticMesh> BaseMeshAsset(*path); 

Not tested, but should get you going.