Just like doing a print() from GDScript I would like to print a message from C-code to the output of the Godot editor. As I understood I could use the Godot API for this.

I found the method "godot_print" in this file of godot_headers: In file "godot_headers/nativescript/gdnative_api.json". This is the definition I found:

{
	"name": "godot_print",
	"return_type": "void",
	"arguments": [
  		["const godot_string *", "p_message"]
	]
}

My main problem is how to define a "const godot_string *" in the C-file? And after that: How to call api->godot_print() with this string?

I've tried to define the string like this:

	const godot_string *cdata = "my output message";

And the compiler gives me this warning:

src/simple.c:109:22: warning: incompatible pointer types initializing 'const godot_string *' with an expression of type 'char [18]' [-Wincompatible-pointer-types]

Then I've tried it like this:

	const char *output = "my output message";
	const godot_string *cdata = (const godot_string *)output;

This works without a warning.

Now when I use the string in the function call:

	api->godot_print(cdata);

Godot editor crashes when building the project.

  • Thank you for your answers. @Cflow, I'm sorry, this didn't work on my side, I've got this error:

    error: expected expression
    godot::Godot::print("Hello World");
          ^

    I now found out how to do it. @Linky , thanks for the source code, it inspired me to find the solution:

    godot_string message;
    core_api->godot_string_new(&message);
    core_api->godot_string_parse_utf8(&message, "Hello world from simple.c");
    core_api->godot_print(&message);
16 days later

You can use godot_string_sprintfto create a godot_string, and then make it a constant:

Exemple

@Linky said: You can use godot_string_sprintfto create a godot_string, and then make it a constant:

Please post your code sample as an indented block (with 4 spaces) so it can be copy-pasted :)

I found out that godot::Godot::print("Hello World"); also work.

15 days later

Thank you for your answers. @Cflow, I'm sorry, this didn't work on my side, I've got this error:

error: expected expression
godot::Godot::print("Hello World");
      ^

I now found out how to do it. @Linky , thanks for the source code, it inspired me to find the solution:

godot_string message;
core_api->godot_string_new(&message);
core_api->godot_string_parse_utf8(&message, "Hello world from simple.c");
core_api->godot_print(&message);
a year later