Godot Version
Godot 4.4
Question
I am a Godot beginner. I’m working with arrays, subarrays etc. I’m thinking of a main array that has a number of elements (not a fixed number), each element being a sub-array. I’ve worked out how to find the size of the main array (number of elements), but I would like to loop through each element of the main array, extracting each element into another array to work on each separately. How could I set aside a number of arrays dynamically in readiness to store the subarray information?
I really do hope I’ve explained myself clearly enough. sorry if I haven’t …I’ve done this so far, as a test, but stopped because of creating new arrays for storage etc. Array4 in this instance has just two elements, but what if the situation is where a varying number of elements could be added?
for item in range(4):
my_array1.append(randi_range(1,10))
for item in range(4):
my_array2.append(randi_range(1,10))
my_array4.append(my_array1)
my_array4.append(my_array2)
var array4_size:int = my_array4.size()
Im not 100% sure if I get what you mean but I will try to understand.
So as far as I am getting you have an array lets call it main_array which contains a (unkown) number of subarrays.
You could do the following things:
for sub_array in main_array:
print(sub_array.size()) #Or do something else with each sub_array
This would be the best way of doing something with each sub_array individually. If you want to create a copy of each sub_array, and do something with this copy just copy the main array and then do the same like this:
var main_array_copy = main_array
for sub_array_index in range(main_array_copy.size()-1):
main_array_copy[sub_array_index].append(0) #This is just an example
#You may do anything you like to each subarray here.
I used an index here in order to be able to actually modify the sub_arrays in the main_array_copy. If I would have directly looped trough the elements of main_array_copy I would just have copies of the Elements to work with.
So basically:
for sub_array in main_array:
sub_array.append(1)
would leave main_array unchanged but
for sub_array_index in range(main_array.size()-1):
main_array[sub_array_index].append(1)
would modify main_array
Is this what you are looking for or do you mean something else?
Note:
Working with nested Arrays in gdscript is a bit cursed as of right now, since static typing does not allow it. For example
var example_array: Array[Array[int]]
would not be allowed. It is possible to do this dynamically. You can sometimes somewhat find your way around that by creating a custom Datatype which contains an Array and then just put that into another Array. I oftentimes find this less cursed than doing the Nesting completely dynamically.
Hi. firstly, thankyou for the quick reply.
from the first bit you’ve written, i.e.
for sub_array in main_array:
print(sub_array.size()) #Or do something else with each sub_array
For my example, my main array does have two elements, each of which is a sub array. Also, I can see that the code snippet you have provided shows the size of each subarray contained within the main array. that’s great so far. At least now it isolates each subarray etc.
objective:- the main array can be made up of an unknown or varied number of sub arrays. The idea is to extract each subarray individually and do something with each one.
I shall need a little more time to look further into the other bits of code to ascertain the ideas behind them.
once again, thank you for your help.
p.s. as a newbie, i really don’t know how you guys format your posts with code snippets shonw separately/neatly from the rest of the text - it looks great!
Im glad if I could help.
As far as the fancy code goes do this:
```gdscript
#Paste your code here
/```
without the /
There also is a preformatted text button (or ctr + e) to quickly past in the ```
Symbols.
1 Like