How can one define an array of colours in code and have them shown from the scene editor?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By gonzo191
:warning: Old Version Published before Godot 3 was released.

As the title states, I’m trying to define an array of colours in script and have them displayed in the scene editor but nothing is working. Normally I’d define each colour separate and then store them in an array from the _ready function but I’m trying to bypass this step since you can define arrays of items. What I’m seeing is that no matter what type I set the array as in script, I’ve still to edit the array, set its type and then manually set each item from the scene editor which is kind of tedious.

I’ve tried all of the following but to no avail

export (PoolColorArray) var cols = PoolColorArray([Color('hex'), ...])
export (ColorArray) var cols = ColorArray([Color('hex'), ...])
export (Array) var cols = [Color('hex'), ...]

When an attempt is made to get the value, an error is thrown

Invalid get index '0' (on base: 'Nil')

Hell, I’ve even tried to store the colour hexes in an array but I still have to manually edit it from the scene.

export (Array) var cols = ['hex', 'hex']

Oddly enough, a dictionary works, only partially, in that I can access the values in script later, but I still can’t edit the colours from the scene editor.

export var cols = {Gray = Color('hex'), Black = Color('hex')}

So is what I’m asking possible at all?
Note: the above was tested in a daily build of v3a (#0e8db63).

if you are looking for a way to change the color while the game is not running(aka in the editor itself) this tutorial might help:
https://www.youtube.com/watch?v=vsxB4L6rufo
its called a tool mode and it sorta runs in the editor itself

rustyStriker | 2017-11-09 18:05

I know a way to have fancy editing in inspector but it requires relatively low level code to be written in your script.
The idea would be to use _get_property_list to create fake properties for every item in your array (using a for loop), and use _get and _set for edition to work on them. Then an extra property would be added which carries the actual array so that it doesnt slow the game due to editor code.
I’ve done this in C++ once so that I could have an array of resources in the inspector, but I dont have a GDScript example, I could try to write one if you want.

Zylann | 2017-11-09 18:50

Actually I have tested this in 3.0 alpha 2:

export(PoolColorArray) var colors = PoolColorArray()

This gives you a property with a little arrow to click, then you can choose the size of the array and you can edit all the colors you want without having to set their type. What’s the problem with this?

Zylann | 2017-11-09 19:05