Godot: Array Initialization contains old values from memory. Bug or feature?

Created on 20 Oct 2019  路  3Comments  路  Source: godotengine/godot

Godot version: 3.1

OS/device including version: Linux

Issue description:
If a Node instance initializes an array like this: var a: Array, the next instance will contain the data of the last one. Instead, var a = [] is fine. Is this intended behaviour? Here is a really small example:

game.gd

extends Node2D

onready var Car = preload("res://car.tscn")

func _ready():
    var car = Car.instance()
    add_child(car)
    print(car.wheels)

    var car2 = Car.instance()
    add_child(car2)
    print(car2.wheels)

car.gd

extends Node2D

var wheels: Array

func _ready():
    for i in range(0,4):
        wheels.append("wheel %s" % i)

Now both cars should contain 4 strings in each array - Wheel 0 - 3. Instead the second car gets the data from the first car's array. This is the output:
[wheel 0, wheel 1, wheel 2, wheel 3]
[wheel 0, wheel 1, wheel 2, wheel 3, wheel 0, wheel 1, wheel 2, wheel 3]

This doesn't happen if you change car.gd to var wheels = []
Very hard to figure out in a more complex scene...

archived

All 3 comments

This sounds like a duplicate of #24134 and/or #28978. Initialize all the things! :slightly_smiling_face:

Yep, this should be already fixed in 3.2 alpha. You can test it to make sure.

Awesome! In 3.2 it is fixed. Confirmed! :-)

Was this page helpful?
0 / 5 - 0 ratings