Cannot convert Integer to String in C++ GDNative

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Pulsarch

I CANNOT FIGURE OUT HOW TO CONVERT AN INTEGER TO A STRING!!!

I am simply trying to get this code to work:

int number = 43;
Godot::print(number);

But it prints nothing every time.
I just want it to be able to print the value of a variable to the Godot Engine.

If I do:

Godot::print("Hello");

It will work every time without a hiccup.

Is there a method I am not using? Is there a header file I could be missing? I’ve been sampling and have tried everything I feel like. I dont have access to the itos() method for some reason, although I see it in the codebase at times and I know it would solve my issue. I have tried variations of the std namespace (which doesnt work), I have tried multiple things with Godot::String… and it doesnt work.

Does anyone happen to know a fix to this? I feel like it might be something simple but I have been spending hours on this and cannot find an answer anywhere.

One solution online also used godot::String( to_string(number).c_str() ); but I didn’t have access to the .c_str() method… I am not sure where (or what header) it came from.

You would be a LIFESAVER if you knew why this may be. I am fairly confident my project is setup correctly because just about everything else works perfectly.

:bust_in_silhouette: Reply From: Teln0

.c_str() is a method of std::string from the standart library, that you include with

#include <string>

Not the ~exact~ answer but led me in the right direction :))))
Thank you so much!

FOR THOSE WHO HAVE A SIMILAR ISSUE HERE IS THE SOLUTION:

I am not an expert programmer, but the concept is that Godot doesn’t have the ability to go from an int to a godot::String but it DOES have the functionality to go from a std::string to a godot::String. (i.e. from a normal C++ string to Godot’s string).

So you need to convert it from an intstd::stringgodot::String then it is ready to print!!!

(Here is a working example)

#include <string>
int num = 35;
std::string standardString = std::to_string(num);
godot::String godotString = godot::String(standardString.c_str());
Godot::print(godotString);

And it will print ‘35’ to the Godot console.

To make the most out of this, I would probably just make a function so you don’t have to do this converting every time :slight_smile:

Pulsarch | 2020-01-26 16:57

For the record, you could have done it simplier because godot::String class have this static method for converting:

static String num_int64(int64_t p_num, int base = 10, bool capitalize_hex = false);

so you didn’t need the std::string it could have been done like this:

using namespace godot;
 
int num = 35; 
Godot::print(String::num_int64(num));

abrajam | 2020-06-28 18:11

:bust_in_silhouette: Reply From: vannongtinh

Godot cpp provides static void godot::Godot::print(const godot::String &message) function. So to really answer your question. You need to pass godot::String object into its parameter.
Here is an example:

String message = String("String: ") + "Hello Godot\n"
                   + "Vector2: " + Vector2(1,2) + '\n'
                   + "Vector3: " + Vector3(3,4,5) + '\n';
Godot::print(message);