How to pass string as parameter? C ++ module

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PerduGames
:warning: Old Version Published before Godot 3 was released.

If I can not have “const char *” parameters in the functions, how would I do if I need to pass a string as a parameter?

Example:

#In my.h

myFunction(const char *s, int i=0);

#In my.cpp

MyClass::myFunction(const char *s, int i)
{
//Here you would use the parameters
}

#Calling myFunction with gd script:

myFunction("I exist?", 2)

I’m not sure this works in the binding API, you should try a String instead (Godot has its own String class which handles more than just 8-bit ASCII characters).

Zylann | 2017-07-24 12:47

And how to convert String to “const char *”? Because I need to pass parameters to other functions that require “const char *”.

PerduGames | 2017-07-24 16:46

String stores characters as wchar_t, not char, because it supports multiple languages. If you want to use UTF8 or if your code absolutely wants char, then you need to convert the string using the ascii() or utf8() functions in String. These will return a CharString, which is basically a vector of char, from which you can get a const char* with get_data: https://github.com/godotengine/godot/blob/master/core/ustring.h#L41

Zylann | 2017-07-24 17:37

I do not understand how to do that.

In my.h

myFunction(String s, int i=0);

In my.cpp

MyClass::myFunction(String s, int i)
{
  //How to convert here to String in const char * 
  //to pass to function that needs to be 
 //const char *?
my_function_requesting_const_char( ? );
}

Calling myFunction with gd script:

myFunction("I exist?", 2)

PerduGames | 2017-07-24 17:59

I think I got it like this, at least it’s compiling here, let’s see later:

In my.cpp

MyClass::myFunction(String s, int i)
{
my_function_requesting_const_char( s.ascii() );
}

PerduGames | 2017-07-24 18:06

Is the function requesting const char * a third-party library? Or is it something that you wrote? Keep in mind const char* is just a pointer, so if your function copies the string or doesn’t stores it, it should be fine. If not… then it will break.

Zylann | 2017-07-24 18:20

Is a third-party library, Enet, the function would be this:

Int enet_address_set_host (ENetAddress * address,
Const char * hostName)

http://enet.bespin.org/group__Address.html#ga979aff1766944d841950c35b57705b49

PerduGames | 2017-07-24 18:34

Are you using the Enet integrated in Godot already?

Zylann | 2017-07-24 18:44

No, it’s a module I’m creating using Enet, very simple P2P.

PerduGames | 2017-07-24 18:48

I am getting “undefined reference to” for Enet functions.

If you can help, I uploaded the module:

GitHub - perdugames/SimpleP2P: A simple local multiplayer module for Godot Engine.(Incomplete)

PerduGames | 2017-07-24 19:16

I was just saying that because Enet REALLY is part of Godot, there is a module named after it: https://github.com/godotengine/godot/tree/master/modules/enet
“undefined reference” means linking failed or wasn’t setup properly, but I can’t really help you there. I would integrate Enet’s source code directly in your module for simplicity, given that it’s very small (or use the Enet in Godot, which has support for ipv6)

Zylann | 2017-07-24 19:42

I was able to compile here, I am aware of this new multiplayer API, but only in 3.0, and what I want to do is version independent. And of course much simpler, I have little knowledge yet, both of c ++, and of godot.

PerduGames | 2017-07-24 20:20

:bust_in_silhouette: Reply From: hungrymonkey

Why dont you use the object String

#include "ustring.h"
myFunction(String s, int i=0);



 char *str = s.utf8();