+1 vote

I think Array lacks of some basic functions for my project (intersections, get random element/index...). Is there anyway to create my own type of array? I think defining a new variable that inherits array methods and properties is a good option, instead of defining functions somewhere.

I tried extends Array, but syntax error

Godot version 3.2.3
in Engine by (244 points)

1 Answer

+1 vote
Best answer

Not without modifying your engine with C++, which you're more than welcome to do:

You could write your functions and store them into an auto-load singleton which makes them globally available:

Random indexes and elements are easy in-liners:
my_array[randi() % my_array.size()]
rand_index = randi() % my_array.size()

by (3,870 points)
selected by

Nice question.

Most likely not without rebuilding the engine so smply create a script that extends reference and do all your custom manipulations there.

class_name SuperArray extends Reference

var contents = Array()

func init():

func append(variant) :

func that_normal_array_lacks():

And in any script you can call it like this

var array = SuperArray.new()

It's a good idea, but I want to preserve all methods of Array to avoid problems. So it's hard to make that.

I'd thinked about modifying the engine, but I don't know C++, only Python, so I decided to ask. What I was doing before that question is that you said, use a singleton. Maybe in a future I try the first option. Thanks!

Simpler than you think you can do a comparison with all of array's methods like I did with the append function above or call it directly like this SuperArray.contents.append(). So basically SuperArray is just a fancy class to manipulate a regular array

Since I don't know C++, I will try your solution @Wakatta, but I still think that it would be better to modify Array, so that other people can benefit from it.

Agreed. Maybe post a feature request as there are other classes with missing stuff but can't be extended either like Marshall

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 Frequently asked questions and 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 [email protected] with your username.