0 votes

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.

in Engine by (19 points)

2 Answers

+2 votes
Best answer

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

#include <string>
by (83 points)
selected by

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 int -> std::string -> godot::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 :)

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));
+1 vote

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);
by (67 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.