Godot: Suggestion: Should Array & Pool*Arrays have constructors for a predetermined size?

Created on 21 Apr 2018  路  5Comments  路  Source: godotengine/godot

This may be because I mostly come from the C-languages world (and GDScript may have a better way to go about this), but I continually find myself declaring an array, resizing it and then iterating through the array to set the data.

This is so often, that it'd be a nice quality of life improvement to be able to construct the array with an initial size, or a possible constructor that takes an initial size and a default value to initialize all the indices with.

enhancement junior job core

Most helpful comment

Working on it

All 5 comments

PoolByteArray is a typedef PoolVector

https://github.com/godotengine/godot/blob/master/core/dvector.h#L464

there is currently no constructor at the moment to create it in the C++ area.

I actually dont know how to use argument constructors in gdscript

@hungrymonkey For what i can tell, the three things to do are:

  1. Add the constructor method to PoolVector
    It should probably be something like this:
    PoolVector(int size) { alloc = NULL; resize(size); }
  2. In core/variant_call.cpp you have to handle the case of this constructor inside Variant Variant::construct
  3. Update the documentation.

I wondered why some constructors are not registered and that's because in function Variant Variant::construct the code handles generic constructor, copy constructors and "convertible constructor" (one args constructors with type that are convertible to the class you are instantiating. The conversion is only checked if the parameter p_strict is True, but i haven't found in code the function Variant::construct called with p_strict True...)
For example, NodePath(string path) is one of them, so it is handled inside Variant Variant::construct.

Assuming p_strict is always False, all one parameter constructors with different type of the class being constructed are handled there, so you should do something similar to what the Color class is doing here:

case COLOR: return p_args[0]->type == Variant::STRING ? Color::html(*p_args[0]) : Color::hex(*p_args[0]);

Otherwise it would just fallback to _convert_array_from_variant inside the Variant class.

But i'm not really sure about this approach, it seems more like a hack. Any suggestion? :tada:

@VinegarLove What you've found looks good, unless I am missing something.

I guess you should add it as a regular constructor (similar to Vector2_init1), so that it doesn't attempt to auto-convert between integers and arrays.

I am currently working on this issue...

Working on it

Was this page helpful?
0 / 5 - 0 ratings