iirc GameMaker 8.1 and below don't even recognize const char * correctly and will only take char * as
@chamaeleon mentioned, so for backwards compatibility it's always nice to cast out the const to a regular char *. But not many people besides me care to do that these days.
Regardless he makes a good point that whether you use const char * or char * either is fine, but in the case of char * a lot of the time you will need to free memory after using the string which should be done on the c++ side after making a copy of the string when using it as a return value.
You aren't using a string as a return value for your function it seems, but it's also worth noting windows, UWP, and other microsoft based platforms such as the Xbox i believe are the only ones with this issue. MacOS, Linux, mobile, and other consoles will accept char * or const char * with full UTF-8 support.
I also believe I do not need an NDA to share that about console because it did eventually come out as public knowledge Sony and Nintendo products are based on FreeBSD which does support UTF-8 with char * and const char * (being a FreeBSD developer I know this, not as a console developer).
Also when returning a string, you must make it static like so:
C++:
const char *function_name() {
const char *hi_fam = ...
static std::string hey_boi; // do not initialize when declaring as static
hey_boi = hi_fam; // now you may initialize here instead
return hey_boi.c_str();
}
Otherwise your string might not equal the correct value when returned back to gamemaker because it is not guaranteed to last the lifetime of the function all the way to it's return value.
If you do:
C++:
const char *function_name() {
const char *hi_fam = ...
static std::string hey_boi = hi_fam; // big no-no
return hey_boi.c_str();
}
then whatever string hey_boi is set to will remain the same after the first time the function is called for the lifetime of the program, meaning after the first call, the function will return the same thing for the rest of the time the instance of the game is running. Obviously you probably don't want that.
Lastly, if you don't want to use static inside a function body you may use a variable outside of a function body instead:
C++:
std::string hey_boi; // you may also use static here if you only want this variable accessible in the source it is used in
const char *function_name() {
const char *hi_fam = ...
hey_boi = hi_fam;
return hey_boi.c_str();
}
Here I leave a comment noting outside of a function body the static keyword has a different use completely. This code is also fine but whether you use a variable outside of a function body or inside with static keyword either way it will not be thread safe.