0 votes

Hello there,

I am having some troubles when trying to use a method with a Godot’s Array which was passed from GDScript to my C++ module.
In the C++ code below, I am trying to read the size of arrayFoo[1][2] which is a monodimensional array.

Array FooNode:FooFunction(Array arrayFoo)
{

 int arraySize = arrayFoo[1][2].size(); // This lines gives an error

 // Code continues

Visual Studio Code throws immediately an error once I have finished typing the line.

more than one conversion function from "Variant" to a built-in type applies: -- function "Variant::operator Object *() const" (declared at line 216 of "/_Godot_CPP_VSCode/Godot-CPP-Linux-VSCode-Full-Setup/source/godot/core/variant.h") -- function "Variant::operator Node *() const" (declared at line 217 of "/_Godot_CPP_VSCode/Godot-CPP-Linux-VSCode-Full-Setup/source/godot/core/variant.h") -- function "Variant::operator Control *() const" (declared at line 218 of "/_Godot_CPP_VSCode/Godot-CPP-Linux-VSCode-Full-Setup/source/godot/core/variant.h")C/C++(418)

In the variant header file I see these three lines in position 216, 217 and 218.

operator Object *() const;
operator Node *() const;
operator Control *() const;

First question: How can I choose the right conversion from the three listed? Or is there another way to get the size? Also, why the error concerns variant.h while I am using an Array? Shouldn’t it be an error coming from Array.h?

I have also noted that if I change the line to

 int arraySize = arrayFoo[1].size();

Now the squiggly lines appears below size() with the error

class "Variant" has no member "size"C/C++(135)

That’s kinda odd, as I have not defined the arrayFoo anywhere in the C++ module and, since it has been passed, then this could possibly be an array of variants.
Second question: what am I missing?

Unfortunately I couldn’t find any documentation on the topic and going through the source code confused me even further.

Does anyone have an idea on how to perform this simple operation, please?

Thanks :)

Godot version 3.2.3 stable
in Engine by (28 points)

1 Answer

+1 vote

Array::operator[] returns Variant, which doesn't have a size().
You need an explicit conversion from that Variant to Array.

Array arr = Array::make(1, 2, Array::make(3, 4, 5, 6)); // creating array with nested array
if (arr.size() > 2)
{
  if ( arr[2].get_type() == Variant::ARRAY) // make sure
  {
    int sz = ((const Array) arr[2]).size(); // typecasted
    Godot::print("nested array size: {0}", sz); // should be 4
  }
}
by (1,438 points)

Hi Sash-rc,

And thank you for replying to my question.

I spent some time looking for a casting method to use in this case, but the ones I found did not work (I am referring to cast_to, which casts Objects and few other classes but not Array). Interestingly I didn't know the make method of the class Array, and I will give it a try in the contest of an Array being passed.

Thank you again for your time and your suggestions, I will post the end code once I get it to work.

Cheers,
the BusSeatProgrammer

You said you need a size of Array nested in another Array. Isn't my code working?

If this is not what you're looking for, could you rephrase your question, that it will not be prone to misinterpretations (m.b. in a new topic)?
Please just note this site works best for a concrete questions.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.