Skip to content

Maps in an array cannot be updated in for...in loop #243

Open
@byronpendason

Description

@byronpendason

I noticed this odd behaviour when making a game. I had an array of enemies, and iterated through them using a for...in loop, and noticed their x and y values did not change!

So I simplified things to demonstrate the behaviour, and came up with this:

arr = []
for i=1 to 3
  local temp = {}
  temp.x = i
  arr << temp
next

print arr

for M in arr
  M.x += 3
next

print arr

My output:

[{"x":1}, {"x":2},{"x":3}]
[{"x":1}, {"x":2},{"x":3}]

The second line should be [{"x":4},{"x":5},{"x":6}].

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions