Lua: Writing Files

From Mario Fan Games Galaxy Wiki
Add to this template
 Standardwikimessagebox.png This article assumes the use of Lua 5.1.

Information may not be accurate or may need revision if you are using a different version.

 Stub.png This article or section is in need of expansion.

Please add further information.

Writing to a file in Lua is a simple process. For this tutorial, we will write a script that generates a few polygons and exports them as an SVG file. The function for generating the polygons won't be explained, as it is beyond the scope of this tutorial.

-- a function to generate regular polygons
function polygon(x, y, sides, radius)
    local points = ""
    local ang_offset = 360 / sides
    for p = 1, sides do
        local point_angle = math.rad(p * ang_offset)
        local point_x = math.cos(point_angle) * radius + x
        local point_y = math.sin(point_angle) * radius + y
        points = points .. point_x .. "," .. point_y .. " "
    return '<polygon points="' .. points .. '" /> \n'

-- use a variable to hold the file handle, and open a new file
svg ="./polygon_lua.svg", "wb")

-- write the start of the svg info 
svg:write('<?xml version="1.0" standalone="no"?>', '\n')
svg:write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "">', '\n')
svg:write('<svg width="640" height="480" xmlns="" version="1.1">', '\n')

-- draw a few polygons
svg:write(polygon(200, 120, 12, 30))
svg:write(polygon(300, 300, 22, 64))
svg:write(polygon(128, 256, 5, 50))

-- write the end of the svg info
svg:write('</svg>', '\n')

-- we're done with the file, so close it

First, we have to create a new file, which is done with The first parameter is the name of the file to create (in this case, it will be created in the same directory as the script), and the second parameter is a string that specifies some options to use: the "w" means to open it in write mode, and the "b" means to open it in binary mode, therefore, "wb" means to open it in write-binary mode. returns a file handle, which we assign to a variable svg. From this point, we can use Lua's colon syntax as a shortcut to perform operations on this variable.

We use svg:write() to write various parts of the SVG file. Note that none of this is actually saved into the file yet; instead, it's stored in a buffer. Once we are ready to close the file, we can use svg:flush() to actually dump the contents into the file. svg:close() tells Lua to discard the file handle; this isn't really required in this example (because the program ends after the file is written anyway), but in other applications that reference files all the time, not discarding your file handles when you are finished with them can cause excess memory usage.

In the basic OO tutorial, using the colon syntax to create your own methods will be covered. For example, you could create a size() method to return the file size of a file, or the length of a table or string, or a trim() method to remove leading and trailing spaces from a string.