1

I was just reading some introductory stuff from GameMonkey Script on https://www.gamedev.net/articles/programming/engines-and-middleware/introduction-to-gamemonkey-script-r3297/ and when they were explaining about Mixed Arrays they say that you can access the elements using and index or a key depending on how the value was declared, so for example if i have the next array

myMixedArray = table( 1, 3, 4, KeyV = "Test", 33);

then i can access 1, 2, 4 and 33 using the next indices 0, 1, 2, 3 and to access "Test" i'll do it like this

myMixedArray["KeyV"] <- ("Test")

now according with the following image that you can find in the above link

enter image description here

The number expected to be at myTest[3] is 7, but that would mean that both regular values and key-val elements are not really separated in the array.

If not then why would 7 be at the index 3 of the array?

Gerard097
  • 815
  • 4
  • 12

1 Answers1

0

While you can treat a gm Table as an Array or Map, you can't effectively do both at the same time. Internally, the Table is just a hash table, and your index access method is a bit like an iterator. In your example, because value "Test" is assigned to key 'KeyV', it messes up the otherwise contiguous index order.

Hopefully that gives you an idea of the cause. Try iterating a table with no 'keys' and again with all key value pairs. Observe the different behavior.

If you are serious about arrays, you may be better off using a binding to create an Array type with the behavior you want. GM source has an example of an array container.

Greg
  • 1,181
  • 13
  • 13