GameMaker: Studio
Set multiple array values neatly?
Is there a way to "neatly" set up variables for multiple indexes in an array (without a script)?

For example, maybe I currently have this below snippet of code:
array[7]=2; array[6]=2; array[5]=2; array[4]=2; array[3]=1; array[2]=1; array[1]=1; array[0]=1;
(I started with 7 and went down to 0 for optimization reasons.)

Is there a way to write that in a "neater" way? For example, something like either of these?
array[7]=2; array[4..7]=2; array[0..3]=1;
array[7]=2; array[0]=1,1,1,1,2,2,2,2

Edit: Note that I will be having hundreds of objects per room, each with their own arrays.
แก้ไขล่าสุดโดย Zappy; 19 มี.ค. 2018 @ 4: 18am
< >
กำลังแสดง 1-9 จาก 9 ความเห็น
I think ds_list would be optimal.

arr = ds_list_create()
ds_list_add(arr, 1, 1, 1, 56, ...)

so its like

arr[1] = 1
arr[2] = 1
arr[3] = 1
arr[4] = 56
...
แก้ไขล่าสุดโดย maras; 19 มี.ค. 2018 @ 4: 00am
โพสต์ดั้งเดิมโดย marasovec:
I think ds_list would be optimal. -
Depending on exactly how such lists work, they might not be optimal; I'm going to have hundreds of objects with their own arrays/lists in each room.
(Not that arrays would necessarily be optimal in this case, either, of course, but still, they might be better, like (maybe?) less resource-intensive and getting rid of themselves automatically when their objects are removed on room change.)

I do admit that I probably should've mentioned that I would be using such many arrays.

(But still, very nice suggestion, and it looks very neat to write, it just might not be quite what I'm looking for in this case.)
แก้ไขล่าสุดโดย Zappy; 19 มี.ค. 2018 @ 4: 26am
This[docs.yoyogames.com] is the only thing that I came across for mass initializing an array. But alas it only allows to create an (N) size array with just (1) value for all indicies.
If there's a pattern to the numbers changing then there's a neater way to do it.

if every 5 values decreases by one, just use a counter to track it and modify in the loop accordingly.
แก้ไขล่าสุดโดย RandomDude; 20 มี.ค. 2018 @ 4: 36pm
If I may add to the comment about arrays being better in this case or whatever. In my experience with other programmers and game developers I have found a general rule of thumb is

If the length of the container (list, array, etc...) is known before runtime then it is best to use an array. If however, the container is dynamic and can grow or shrink during runtime then a list,
or link list is much more preferable.


As per resources, coding time, etc.. there is no difference between the two in a general sense of things. Lists are designed to handle dynamic data. They are optimised to handle the data best and should be used if your data is dynamic. Another thing to mention is though data structures already come pre packaged with mehods to examine and extract data from the data structure, you could build methods to work with arrays. If you understand how data structures work and why they were built then you could code your own methods to do the same function on arrays with the same overhead as the pre-packaged ones do.

EDIT: on a side note... arrays came first when we started to build computers to except language. Then we notices that lists, ques, maps, etc.. were great way to handle data. So we learned to optimize them as best we can. It's similar to Vector math in Unity and C+. Just optimized way to handle data. Along with being easy to read and interpret.
แก้ไขล่าสุดโดย The Winter Bud; 20 มี.ค. 2018 @ 6: 23pm
I wish I'd used data structures earlier because they would of saved me a TON of headache lol
โพสต์ดั้งเดิมโดย RandomDude:
If there's a pattern to the numbers changing then there's a neater way to do it. -
Sometimes all values will be the same, sometimes they'll rise by one for every other array index, other times they will be completely "random" (though pre-determined and specifically typed in by hand). So there's not really an exact pattern for them in most cases.

โพสต์ดั้งเดิมโดย The Winter Bud:
If I may add to the comment about arrays being better in this case or whatever. In my experience with other programmers and game developers I have found a general rule of thumb is -Snip- As per resources, coding time, etc.. there is no difference between the two in a general sense of things. Lists are designed to handle dynamic data. They are optimised to handle the data best and should be used if your data is dynamic.
-Also snipped, to keep the quote somewhat short-
Depending on what you mean by that, the main thing/reason I want to avoid using a "list" is because GameMaker: Studio's manual says[docs.yoyogames.com] "NOTE: As with all dynamic resources, data structures take up memory and so should always be destroyed when no longer needed to prevent memory leaks which will slow down and eventually crash your game." and I don't want to have to manually "destroy" the lists whenever a room ends (not even if I make a universal parent object with a room end event or such that destroys them), while an array is tied to a local instance and destroyed when an instance ceases to exist (as far as I'm aware).

โพสต์ดั้งเดิมโดย The Winter Bud:
- If you understand how data structures work and why they were built then you could code your own methods to do the same function on arrays with the same overhead as the pre-packaged ones do. -
I know, but I just find it silly to have to write a script in order to (neatly) initialize an array in such a way.
Just going to say this because no one else did.
Studio 2 introduced this way to writing arrays:
array = [1, 1, 2, 3, 1, 1, 2, 3]

array[0] will be 1, array[7] will 3, etc.
แก้ไขล่าสุดโดย Abusfad; 23 มี.ค. 2018 @ 11: 05am
Zappy 23 มี.ค. 2018 @ 11: 29am 
โพสต์ดั้งเดิมโดย Abusfad:
Just going to say this because no one else did. Studio 2 introduced this way to writing arrays:
array = [1, 1, 2, 3, 1, 1, 2, 3]
array[0] will be 1, array[7] will 3, etc.
That's basically exactly what I want... but I have GameMaker: Studio 1, and GameMaker Studio 2('s standard edition) costs money even though I have 1, and I'm not enthusiastic enough about game development to buy 2.
Which means I assume that I'll have to write a custom script to fill out arrays "neatly" (which won't be much of a problem, I'd just have preferred a native solution).
< >
กำลังแสดง 1-9 จาก 9 ความเห็น
ต่อหน้า: 1530 50

วันที่โพสต์: 19 มี.ค. 2018 @ 3: 23am
โพสต์: 9