Godot Version
4.5.2
Question
I am trying to use the gdextension C API, following the tutorial:
I’ve added a C function requiring two strings as parameters, using the exact same patterns shown in the tutorial. I can call the function from gdscript - however It’s not at all clear to me how in my C code I can get a const char* from the struct defined in the example.
The tutorial includes this struct definition:
// The sizes can be obtained from the extension_api.json file.
#ifdef BUILD_32
#define STRING_SIZE 4
#define STRING_NAME_SIZE 4
#else
#define STRING_SIZE 8
#define STRING_NAME_SIZE 8
#endif
...
typedef struct
{
uint8_t data[STRING_SIZE];
} String;
I’ve copied this to a header file and I’m populating this struct like this:
constructors.variant_from_string_constructor
= api.get_variant_from_type_constructor(
GDEXTENSION_VARIANT_TYPE_STRING);
constructors.string_from_variant_constructor
= api.get_variant_to_type_constructor(GDEXTENSION_VARIANT_TYPE_STRING);
inline bool assert_argument_type(const GDExtensionConstVariantPtr *p_args,
GDExtensionCallError *r_error, uint32_t which,
String &out)
{
GDExtensionVariantType type = api.variant_get_type(p_args[which]);
if (type != GDEXTENSION_VARIANT_TYPE_STRING)
{
r_error->error = GDEXTENSION_CALL_ERROR_INVALID_ARGUMENT;
r_error->expected = GDEXTENSION_VARIANT_TYPE_STRING;
r_error->argument = which;
return false;
}
constructors.string_from_variant_constructor(
&out, (GDExtensionVariantPtr)p_args[which]);
return true;
}
Is there a function I can call to get a const char* out of this struct? It’s not obvious to me from reading the tutorial or browsing gdextension_interface.h how I can do this.