Search code examples
arraysfunctionluaparameter-passinglove2d

Passing an array name to a function, then populating it


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?


Solution

  • 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