Is there any way to create your own variable type?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By abelgutierrez99

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

:bust_in_silhouette: Reply From: timothybrentwood

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()

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 =

Wakatta | 2021-05-08 21:33

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

abelgutierrez99 | 2021-05-09 19:21

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!

abelgutierrez99 | 2021-05-09 19:23

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

Wakatta | 2021-05-11 20:08

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.

abelgutierrez99 | 2021-05-15 12:49

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

Wakatta | 2021-05-15 22:04