3

I would like to call a function that takes data and then populates an array I name in one of the function parameters. Example of what I'm looking for:

function readSaveFile(saveFileName, arrayName)
    if love.filesystem.exists(saveFileName) then
        arrayName = Tserial.unpack(love.filesystem.read(saveFileName))
    end
end

The issue is that instead of creating an array with the string under arrayName, it replaces the parameter with an array explicitly called "arrayName". Is there any way I could populate the array name specified instead?

  • 3
    Why not just return the data? Variable manipulation like this is usually bad practice, and difficult if the variable isn't in scope and not a global. – Colonel Thirty Two Oct 10 '14 at 19:10
  • If you still don't have answer, please post clarification as I don't understand last paragraph of your post. – Oliver Oct 11 '14 at 16:21

1 Answers1

4

You can always insert the data into given array (provided it is not nil). For example:

function readSaveFile(saveFile, arrayName)
  if love.filestystem.exists(saveFileName) then
    for k, v in pairs(love.filesystem.read(saveFileName)) do
      arrayName[k] = v
    end
  end
end
lisu
  • 2,213
  • 14
  • 22