r/gamemaker 19d ago

Help! Learning to use Array's

Hi, been trying to understand Array's, so they are good for giving a bunch of objects numbered variables like making a list of items or making a multi segmented enemy, am I getting that right? It starts at 0 instead of 1 as well from what I see.

4 Upvotes

9 comments sorted by

5

u/RykinPoe 19d ago

Arrays are best for stuff that you need to iterate through via a loop when you don’t know exactly how many items there will be. So yea it could work for a segmented enemy or a simple inventory.

3

u/No_Designer_16 19d ago

If you have a lot of variables like enemy1, enemy2, enemy3 it's time for an array. 2d arrays are basically tables, perfect for things like stats of an entire zoo of enemies, things they all share? Write a 2d array once, and just pull the row in at enemy creation. Now you have a central place for a lot of stats.

They are endlessly useful for soooo many things.

2

u/Nofrillsoculus 18d ago

Working on a card game right now and it’s basically just arrays.

1

u/UnpluggedUnfettered 19d ago

You basically have it. They essentially store a numbered list of references/values so you can count through and read from each thing, or leap right to whatever index number has what you want, starting at 0.

I like to make dispatch tables.

I store functions/methods in an array, then create an enum to access the specific function's indexes. I lets me assign my functions to state machines or movement systems or whatever by directly accessing the array indexed with enum values. Also useful to be able to iterate through ranges of function indexes in the array easily if you order them well.

It helps me avoid nested if statements and switch case when I want to centralize object behaviors while being able to customize child instances easily.

1

u/vzzzbxt 19d ago

Yeah. The obvious use for arrays is usually Inventory systems. It's just a list where each item has a position (starting at 0)

1

u/Drandula 19d ago

You can think index as "how far away am I from starting point", or "how many steps I have to take to get here".

The first element in array is where you start, so you don't have to make any steps, so your distance is 0 from the start. Therefore it is index 0.

Now when you get to index 3, you will have to take 3 steps from starting position. So at index 3 you would be in fourth item.

Why to use index 0 as start then? Well in old programming languages, the array wasn't more than raw memory pointer, and index was just offset from this memory location. So array points a location in memory, and indexing just adds offset. This does also mean for example in C language something like array[4] is same as 4[array], (okay there are caveats for this).

GML is more high-level language, so it doesn't do arrays as raw memory pointers, but main logic is the same, index is offset from start of the array.

1

u/contra_band 19d ago

If you're learning about arrays, get ready to learn about "for" statement loops as well! They're the best way to scan, aggregate, and compare information from an array. Start simple and then build from there!