A different way to combine the two strings

void UPuzzPlat_GameInstance::Join(const FString& joinAddress)
{
	UEngine* engine = GetEngine();
	if (!ensure(engine != nullptr)) return;

	FString msg = "Join called. Address=";
	msg += joinAddress;
	engine->AddOnScreenDebugMessage(-1, 3.f, FColor::Green, msg);
}

Thought I would show my way I did this whilst paused for the challenge. It is different to Sam’s but I will stick with mine for now :stuck_out_tongue:

1 Like

Don’t forget to enclose the string literal with TEXT().

FString msg = TEXT("Join called. Address=");

2 Likes

While += will work, it’s not efficient when it comes to strings.

It involves allocating space for the constant and copying into the original variable, the allocation of space for the new string, copying the existing string into it and then the new string you are appending as well. Then it assigns the newly allocated memory to the variable and releases the memory for the original string.

FString::Printf on the other hand takes a constant determines what formatting needs doing and allocates the space for the whole string and performs the format. There’s no extra allocating and deallocating going on here (except maybe cleanup which happens in both cases)

The other thing, += would only work with FStrings. printf can format numbers too.

The FString::Printf function is almost certainly more efficient. In this case and that is an important factor. Use of printf style formatting is also pretty standard in C++ so it is useful to know as well - MFC also has CString::Format which does the same sort of thing.

That’s not to say never use +=, just consider the implications of doing it this way.

1 Like

I was taught to write it simple and worry about optimization afterwards. :smiley:
Perhaps this is the wrong approach to life and programming though haha. Tbh though I just wanted to share this for fun and also 'cause Sam/Ben are always saying “Jump on the forum and share what you think” which I very much enjoy :stuck_out_tongue:

Thanks for the detailed info though this is how I learn :smiley:

EDIT:
(If Printf can add/subtract numbers from the string that would be awesome. But I assum you mean I would not have to format the values first to convert them from Ascii?)

1 Like

That’s my first rule - simple is good. However, with experience the printf is second nature and is simpler than declaring and appending, and less code too so faster to author.

Also, if you’re working on large projects, these optimisations never get done because usually there’s always something else to do.

Some basic changes in C++ makes for more efficient code and doing it as you go along is more efficient too.
e.g. += and strings are never good - for throwaway code like temporary UE_LOG it’s fine but get into the habit of doing it right. ++var instead of var++ is faster because, for the same reason as += with strings, it allocates a new memory location to perform the calculation when you use var++. If you can loop backwards to 0 then do that as it’s more efficient because the end condition in a for loop looking for var==0 equates to assembly language jump if zero

Printf is just for formatting strings - it doesn’t enable maths operations on the numbers in the strings - that’s really inefficient anyway as you have to parse the string to a number, do the calculation and convert back. Do the maths in the correct variable type (int or float) and format to a string only when you need it as a string.

1 Like

Thanks again for taking the time to write back. This is golden information. I may have to go back and study more of the Std library and other basic C++ stuff. Although I must say I am learning much more and much faster since I decided to purchase these courses.

The help you are giving on these forums will be helping lots of people with stuff that could have taken months or years to learn alone. It’s very much appreciated.

1 Like

The write it simple - does still apply. However, it’s a bit like refactoring. Get your code working - could be as bad can be, that’d be the green state. Refactor. You may break it in the process (red state) but do it a small bit at a time. Get it working again with the refactored code (green state) and repeat as often as necessary.

Basically, Implement->Get to green->Refactor>red->green>loop back to refactor

1 Like

Privacy & Terms