[GDNative] Convert interger to string?

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

Does anybody with good knowledge in GDNative (C++) happen to know how to convert an interger to string? I’ve ran into compiling errors trying to get it to work:

gdtest.cpp:

#include <iostream>
#include <string>
#include <cmath>

using namespace godot;
using namespace std;

String math(){
    int number = 6;
    return godot::String(to_string(number));
}

This is what the error says:

src\gdtest.cpp(11): error C2440: '<function-style-cast>': cannot convert from 'std::string' to 'godot::String'

Any info on how to convert to string would be great! Thanks!

:bust_in_silhouette: Reply From: hilfazer

Try sending const char* to godot::String’s constructor, instead of std::string:

return godot::String( to_string(number).c_str() );

It works thanks! Despite some examples of GDScript, it felt completely as if I entered uncharted territory. Hopefully this answer will help others figuring out how to convert to string.

Corruptinator | 2019-01-29 00:00