GDExtension convert String to Char*
Asked Answered
C

10

0

I'm struggling to get a usable string from a Variant to either char* or std::string

reference to post 8:
https://mcmap.net/q/1244/godot-gd4-gdextension-return-variant-of-std-vector

Variant GDXml2Json::test(Variant vin)
{
	auto type = vin.get_type();
	if(type==4){
		//	String
		UtilityFunctions::print(vin);
		String s = vin.stringify();	//.get_data();
		UtilityFunctions::print(s);

		PackedByteArray ba = s.to_utf32_buffer();
		std::u32string xml = std::u32string(ba);
		etc...
		std::string xml = s;
Cowboy answered 21/5, 2023 at 19:59 Comment(0)
D
0

Cowboy Check if sc is a valid pointer. There is a String::String(const char *p_str); constructor so it should work. Try it out with a C string literal.

Danged answered 21/5, 2023 at 21:46 Comment(0)
D
0

Cowboy
According to source code

s.utf8().get_data()

Should return const char*
You shouldn't need any additional utf8 decoding if your strings consist only of ascii characters.

Danged answered 21/5, 2023 at 20:16 Comment(0)
C
0

Danged

That appears to work but now struggling to get it back out...


	//	INIT & PARSE XML STRING INTO DOC
	tinyxml2::XMLDocument doc;
	
	auto type = vin.get_type();
	if(type==4){
		//	String
		
		String s = vin.stringify();	//.get_data();
		const char *xml = s.utf8().get_data();
		doc.Parse(xml);
		//doc.Print();
		
		//	GET ROOT ELEMENT
		tinyxml2::XMLElement* root = doc.FirstChildElement();
		//std::cout << "Name: " << root->Name() << std::endl;
		UtilityFunctions::print("Name:");
		
		//const char* sc = root->Name();
		const char* sc = root->Name();
		//std::string sss = sc;	//"yoyo";	//root->Name();
		//UtilityFunctions::print(sss.c_str());
		//UtilityFunctions::print(sc);
		
		Variant v = sc;
		
		//	START
		//std::string indent = "";
		//tester4_recursive(root, indent);
		
		
	}
	else{
	}

Cheers

Cowboy answered 21/5, 2023 at 20:56 Comment(0)
D
0

Cowboy
You can construct Godot's String from that char *, and then pass it to Variant constructor.

String s;
s.copy_from(sc);
Variant v(s);

Btw, looking at String class interface in ustring.h can help you a lot in figuring things out.

Danged answered 21/5, 2023 at 21:5 Comment(0)
C
0
		const char* sc = root->Name();
		
		String sg;
		sg.copy_from(sc);
		Variant vv(sg);
		UtilityFunctions::print(vv);

but I get the error:

error: 'class godot::String' has no member named 'copy_from'
  139 |                 sg.copy_from(sc);

even though the following does show it at line 187...
https://github.com/godotengine/godot/blob/master/core/string/ustring.h

Sorry

Cowboy answered 21/5, 2023 at 21:20 Comment(0)
D
0

Cowboy
Oh, sorry I didn't see it's private. There's a constructor from const char* though (as expected). So this should work:

String sg(sc);
Variant vv(sg);

Or shorter:

Variant vv(String(sc));
Danged answered 21/5, 2023 at 21:30 Comment(0)
C
0

For me that crashes on the construction of sg...

BTW
I'll have to pick this up later, need to get off.
Many thanks

Cowboy answered 21/5, 2023 at 21:40 Comment(0)
D
0

Cowboy Check if sc is a valid pointer. There is a String::String(const char *p_str); constructor so it should work. Try it out with a C string literal.

Danged answered 21/5, 2023 at 21:46 Comment(0)
C
0

nope

		const char* ww = "What";
		String gg(ww);
Cowboy answered 21/5, 2023 at 22:47 Comment(0)
D
0

Cowboy What the debugger says?

Danged answered 22/5, 2023 at 0:46 Comment(0)
C
0

I believe da booga says oh ******!

Think you were correct all along! The error was in the library, this on its own works fine:

Variant GDTest::test(Variant vin)
{
	UtilityFunctions::print("Hello from GDExtension");
	
	String sg(vin);
	String s = vin.stringify();
	const char *xml = s.utf8().get_data();
	
	const char* ww = "Godot/n";
	String gg(ww);
	
	Variant v = gg;		//"Alice";
	return v;
}

Thankyou

Cowboy answered 22/5, 2023 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.