0 votes

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]);
    }
  }
}
Godot version 3.3.2-stable-mono
in Engine by (12 points)

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.

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)

Please log in or register to answer this question.

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.