Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unreal Engine 4.x Scripting with C++ Cookbook

You're reading from   Unreal Engine 4.x Scripting with C++ Cookbook Develop quality game components and solve scripting problems with the power of C++ and UE4

Arrow left icon
Product type Paperback
Published in Mar 2019
Publisher
ISBN-13 9781789809503
Length 708 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
John P. Doran John P. Doran
Author Profile Icon John P. Doran
John P. Doran
Stephen Whittle Stephen Whittle
Author Profile Icon Stephen Whittle
Stephen Whittle
William Sherif William Sherif
Author Profile Icon William Sherif
William Sherif
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. UE4 Development Tools FREE CHAPTER 2. Creating Classes 3. Memory Management, Smart Pointers, and Debugging 4. Actors and Components 5. Handling Events and Delegates 6. Input and Collision 7. Communication Between Classes and Interfaces: Part I 8. Communication Between Classes and Interfaces: Part II 9. Integrating C++ and the Unreal Editor: Part I 10. Integrating C++ and the Unreal Editor: Part II 11. Working with UE4 APIs 12. Multiplayer Networking in UE4 13. AI for Controlling NPCs 14. User Interfaces - UI and UMG 15. Other Books You May Enjoy

UE4 – making an FString from FStrings and other variables

When coding in UE4, you often want to construct a string from variables. This is pretty easy using the FString::Printf or FString::Format function.

Getting ready

For this, you should have an existing project into which you can enter some UE4 C++ code. Putting variables into a string is possible via printing. It may be counter intuitive to print into a string, but you can't just concatenate variables together and hope that they will automatically convert into strings, as in some languages, such as JavaScript.

How to do it...

In this recipe, we will see how to print in two different ways. First, we will be using FString::Printf():

  1. Consider the variables you'd like to be printed into your string. Note what each variable type is.
  2. Open and take a look at a reference page of the printf format specifiers, such as http://en.cppreference.com/w/cpp/io/c/fprintf. For each variable you want to print, node what the specifier is. For example, %s for a formatted string.
  3. Try code such as the following:
FString name = "Tim"; 
int32 mana = 450; 
FString string = FString::Printf( TEXT( "Name = %s Mana = 
%d" ), *name, mana );

Notice how the preceding code block uses the format specifiers precisely like the traditional printf function does. In the preceding example, we used %s to place a string in the formatted string, and %d to place an integer in the formatted string. Different format specifiers exist for different types of variables, and you should look them up on a site such as cppreference.com.

It is also possible to print a string using FString::Format().

  1. Write code in the following form:
FString name = "Tim"; 
int32 mana = 450; 
TArray< FStringFormatArg > args; 
args.Add( FStringFormatArg( name ) ); 
args.Add( FStringFormatArg( mana ) ); 
FString string = FString::Format( TEXT( "Name = {0} Mana =
{1}" ), args );

UE_LOG( LogTemp, Warning, TEXT( "Your string: %s" ),
*string );

With FString::Format(), instead of using correct format specifiers, we use simple integers and a TArray of FStringFormatArg instead. FstringFormatArg helps FString::Format() deduce the type of variable to put in the string. Refer to the following screenshot:

No matter which method you use, upon calling UE_LOG and you will get the same output.

You have been reading a chapter from
Unreal Engine 4.x Scripting with C++ Cookbook - Second Edition
Published in: Mar 2019
Publisher:
ISBN-13: 9781789809503
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime