Skip to main content
Giving explicit credit for the C++ code, and noting how it differs from the gdscript above.
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

The reason this is throwing an error is that Godot's string format accepts two ways of matching placeholders to values:

  • By index:
    "{0}, {1}".format(["Hello", "world"])

  • By key-value pairs:
    "{greeting}, {subject}".format([["greeting", "Hello"], ["subject", "world"]])

Because your array [1.0, [1.0, 2.0, 3.0]] contains a nested array, the format function thinks you want the second style, and so it expects each array entry to be an array of size 2 for the key and the value, and complains when that isn't what it finds.

Since you want it to treat the nested array as just a single value, you'll first have to flatten your angles array to a string, then interpolate that string into your template string.

In gdscript, it would look like this:

var angles := [1.0, 2.0, 3.0]
var s := "%d %s" % [1.0, str(angles)]
print(s)  #Prints "1.000000 [1.0, 2.0, 3.0]"

InHere's a C++ method for flattening a Godot list of numbers to a string, provided by OP, Luke B.:

godot::String list_to_string(const godot::List<float>& numbers) {
    godot::String ret;
    for (auto n : numbers) {
        ret += godot::String("{0},").format(n);
    }
    return ret;
}

The reason this is throwing an error is that Godot's string format accepts two ways of matching placeholders to values:

  • By index:
    "{0}, {1}".format(["Hello", "world"])

  • By key-value pairs:
    "{greeting}, {subject}".format([["greeting", "Hello"], ["subject", "world"]])

Because your array [1.0, [1.0, 2.0, 3.0]] contains a nested array, the format function thinks you want the second style, and so it expects each array entry to be an array of size 2 for the key and the value, and complains when that isn't what it finds.

Since you want it to treat the nested array as just a single value, you'll first have to flatten your angles array to a string, then interpolate that string into your template string.

In gdscript, it would look like this:

var angles := [1.0, 2.0, 3.0]
var s := "%d %s" % [1.0, str(angles)]
print(s)  #Prints "1.000000 [1.0, 2.0, 3.0]"

In C++:

godot::String list_to_string(const godot::List<float>& numbers) {
    godot::String ret;
    for (auto n : numbers) {
        ret += godot::String("{0},").format(n);
    }
    return ret;
}

The reason this is throwing an error is that Godot's string format accepts two ways of matching placeholders to values:

  • By index:
    "{0}, {1}".format(["Hello", "world"])

  • By key-value pairs:
    "{greeting}, {subject}".format([["greeting", "Hello"], ["subject", "world"]])

Because your array [1.0, [1.0, 2.0, 3.0]] contains a nested array, the format function thinks you want the second style, and so it expects each array entry to be an array of size 2 for the key and the value, and complains when that isn't what it finds.

Since you want it to treat the nested array as just a single value, you'll first have to flatten your angles array to a string, then interpolate that string into your template string.

In gdscript, it would look like this:

var angles := [1.0, 2.0, 3.0]
var s := "%d %s" % [1.0, str(angles)]
print(s)  #Prints "1.000000 [1.0, 2.0, 3.0]"

Here's a C++ method for flattening a Godot list of numbers to a string, provided by OP, Luke B.:

godot::String list_to_string(const godot::List<float>& numbers) {
    godot::String ret;
    for (auto n : numbers) {
        ret += godot::String("{0},").format(n);
    }
    return ret;
}

The reason this is throwing an error is that Godot's string format accepts two ways of matching placeholders to values:

  • By index:
    "{0}, {1}".format(["Hello", "world"])

  • By key-value pairs:
    "{greeting}, {subject}".format([["greeting", "Hello"], ["subject", "world"]])

Because your array [1.0, [1.0, 2.0, 3.0]] contains a nested array, the format function thinks you want the second style, and so it expects each array entry to be an array of size 2 for the key and the value, and complains when that isn't what it finds.

Since you want it to treat the nested array as just a single value, you'll first have to flatten your angles array to a string, then interpolate that string into your template string.

In gdscript, it would look like this:

var angles := [1.0, 2.0, 3.0]
var s := "%d %s" % [1.0, str(angles)]
print(s)  #Prints "1.000000 [1.0, 2.0, 3.0]"

I'm not fluent in the equivalentIn C++ syntax, but hopefully that points you in the right direction.:

If anyone else is able to provide the correct C++ code, that would be most welcome as an edit, comment, or complementary answer.

godot::String list_to_string(const godot::List<float>& numbers) {
    godot::String ret;
    for (auto n : numbers) {
        ret += godot::String("{0},").format(n);
    }
    return ret;
}

The reason this is throwing an error is that Godot's string format accepts two ways of matching placeholders to values:

  • By index:
    "{0}, {1}".format(["Hello", "world"])

  • By key-value pairs:
    "{greeting}, {subject}".format([["greeting", "Hello"], ["subject", "world"]])

Because your array [1.0, [1.0, 2.0, 3.0]] contains a nested array, the format function thinks you want the second style, and so it expects each array entry to be an array of size 2 for the key and the value, and complains when that isn't what it finds.

Since you want it to treat the nested array as just a single value, you'll first have to flatten your angles array to a string, then interpolate that string into your template string.

In gdscript, it would look like this:

var angles := [1.0, 2.0, 3.0]
var s := "%d %s" % [1.0, str(angles)]
print(s)  #Prints "1.000000 [1.0, 2.0, 3.0]"

I'm not fluent in the equivalent C++ syntax, but hopefully that points you in the right direction.

If anyone else is able to provide the correct C++ code, that would be most welcome as an edit, comment, or complementary answer.

The reason this is throwing an error is that Godot's string format accepts two ways of matching placeholders to values:

  • By index:
    "{0}, {1}".format(["Hello", "world"])

  • By key-value pairs:
    "{greeting}, {subject}".format([["greeting", "Hello"], ["subject", "world"]])

Because your array [1.0, [1.0, 2.0, 3.0]] contains a nested array, the format function thinks you want the second style, and so it expects each array entry to be an array of size 2 for the key and the value, and complains when that isn't what it finds.

Since you want it to treat the nested array as just a single value, you'll first have to flatten your angles array to a string, then interpolate that string into your template string.

In gdscript, it would look like this:

var angles := [1.0, 2.0, 3.0]
var s := "%d %s" % [1.0, str(angles)]
print(s)  #Prints "1.000000 [1.0, 2.0, 3.0]"

In C++:

godot::String list_to_string(const godot::List<float>& numbers) {
    godot::String ret;
    for (auto n : numbers) {
        ret += godot::String("{0},").format(n);
    }
    return ret;
}
Adding explicit invitation for C++ example
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

The reason this is throwing an error is that Godot's string format accepts two ways of matching placeholders to values:

  • By index:
    "{0}, {1}".format(["Hello", "world"])

  • By key-value pairs:
    "{greeting}, {subject}".format([["greeting", "Hello"], ["subject", "world"]])

Because your array [1.0, [1.0, 2.0, 3.0]] contains a nested array, the format function thinks you want the second style, and so it expects each array entry to be an array of size 2 for the key and the value, and complains when that isn't what it finds.

Since you want it to treat the nested array as just a single value, you'll first have to flatten your angles array to a string, then interpolate that string into your template string.

In gdscript, it would look like this:

var angles := [1.0, 2.0, 3.0]
var s := "%d %s" % [1.0, str(angles)]
print(s)  #Prints "1.000000 [1.0, 2.0, 3.0]"

I'm not fluent in the equivalent C++ syntax, but hopefully that points you in the right direction.

If anyone else is able to provide the correct C++ code, that would be most welcome as an edit, comment, or complementary answer.

The reason this is throwing an error is that Godot's string format accepts two ways of matching placeholders to values:

  • By index:
    "{0}, {1}".format(["Hello", "world"])

  • By key-value pairs:
    "{greeting}, {subject}".format([["greeting", "Hello"], ["subject", "world"]])

Because your array [1.0, [1.0, 2.0, 3.0]] contains a nested array, the format function thinks you want the second style, and so it expects each array entry to be an array of size 2 for the key and the value, and complains when that isn't what it finds.

Since you want it to treat the nested array as just a single value, you'll first have to flatten your angles array to a string, then interpolate that string into your template string.

In gdscript, it would look like this:

var angles := [1.0, 2.0, 3.0]
var s := "%d %s" % [1.0, str(angles)]
print(s)  #Prints "1.000000 [1.0, 2.0, 3.0]"

I'm not fluent in the equivalent C++ syntax, but hopefully that points you in the right direction.

The reason this is throwing an error is that Godot's string format accepts two ways of matching placeholders to values:

  • By index:
    "{0}, {1}".format(["Hello", "world"])

  • By key-value pairs:
    "{greeting}, {subject}".format([["greeting", "Hello"], ["subject", "world"]])

Because your array [1.0, [1.0, 2.0, 3.0]] contains a nested array, the format function thinks you want the second style, and so it expects each array entry to be an array of size 2 for the key and the value, and complains when that isn't what it finds.

Since you want it to treat the nested array as just a single value, you'll first have to flatten your angles array to a string, then interpolate that string into your template string.

In gdscript, it would look like this:

var angles := [1.0, 2.0, 3.0]
var s := "%d %s" % [1.0, str(angles)]
print(s)  #Prints "1.000000 [1.0, 2.0, 3.0]"

I'm not fluent in the equivalent C++ syntax, but hopefully that points you in the right direction.

If anyone else is able to provide the correct C++ code, that would be most welcome as an edit, comment, or complementary answer.

Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401
Loading