Append to Vector2 array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By KittyKate

I am trying to do this

vec2.Append(
   new Vector2(
      i * 100,
      rng.RandiRange(200, 500)));

And at the output I still get an empty array, the output to the console confirms this

(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)
(0, 0)

Maybe I’m doing something wrong? Explain to me please, here is the whole code.

using Godot;
using System;
using System.Linq;

public class Append : Node2D
{
public Vector2[] vec2 = new Vector2[10];

public RandomNumberGenerator rng = new RandomNumberGenerator();
public override void _Ready()
{
    for (int i = 0; i < 10; i++) {
        vec2.Append(
            new Vector2(
                i * 100,
                rng.RandiRange(200, 500)));
    }

    for (int i = 0; i < vec2.GetLength(0); i++) {
        GD.Print(vec2[i]);
    }
  }
}

Perhaps you need to see if the Vector assignment actually works with plain integers first? (new Vector2(1,1)) — And if not then can you Append from a variable instead?

Keep back-stepping your code until you find something that works, it will point you to what’s actually broken.

Yuminous | 2021-07-12 07:26

Yes, I’ve already tried Vector assignment with plain integers like new Vector (1, 1) and also tried adding from a variable like public Vector2 x = new Vector (1, 1), the result is the same.
At the same time, such a construction works and returns an array filled with random vectors, as I need

using Godot;
using System;
using System.Linq;

public class Append : Node2D
{

    public Vector2[] vec2 = new Vector2[10];

    public RandomNumberGenerator rng = new RandomNumberGenerator();
    public override void _Ready()
    {
        for (int i = 0; i < 10; i++) {
            vec2[i] = new Vector2( i * 100, rng.RandiRange(200, 500));
        }

        for (int i = 0; i < vec2.GetLength(0); i++) {
            GD.Print(vec2[i]);
        }
    }
}

With this output to the console:

(0, 340)
(100, 441)
(300, 419)
(200, 332)
(400, 216)
(500, 258)
(600, 468)
(900, 384)
(700, 207)
(800, 445)

KittyKate | 2021-07-12 21:23