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

reference to post 8:
https://godotforums.org/d/33672-gd4-gdextension-return-variant-of-stdvector/7

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;
  • xyz replied to this.
  • totoro 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.

    totoro
    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.

      xyz

      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

      • xyz replied to this.

        totoro
        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.

        		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

        • xyz replied to this.

          totoro
          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));

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

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

          • xyz replied to this.

            totoro 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.

            nope

            		const char* ww = "What";
            		String gg(ww);
            • xyz replied to this.

              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