Leveraging Lua as a data storage format for your game

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Kyle
Party member
Posts: 146
Joined: Sat Mar 16, 2013 9:46 pm

Leveraging Lua as a data storage format for your game

Post by Kyle »

Lua's an extremely capable language, as I'm sure you all know. Tables allow you to do anything and everything, through the magic of metaprogramming!

So why would we bother with XML, Json, or other formats unless absolutely necessary? We can just use Lua! It allows us to do a lot of different things that would be nothing but extra work in any other format. For example, here's a basic space station hub module's settings:

Code: Select all

HubModule.ModuleSettings = {
	1 = {
		Name = "Basic Hub",
		Description = "",

		ImageFilename = "N/A",

		Bins = {
			In = ModulePresets.Bin.Disabled()
			Out = ModulePresets.Bin.Disabled()
			Both = ModulePresets.Bin.AllNoPeople(150)
			People = ModulePresets.Bin.OnlyPeople(25)
		},

		BuildingParameters = {
			1 = {
				Name = "Constructing basic frame",
				Required = true,
				Labor = {
					{ Type = Resources["engineer"], Manhours = 10  },
					{ Type = Resources["laborer"],  Manhours = 100 },
				},
				Resources = {
					{ Resources["bundles of titanium beams"], 10, true },
				},
			},
			2 = {
				Name = "Plating outer hull",
				Required = true,
				Labor = {
					{ Type = Resources["engineer"], Manhours = 20  },
					{ Type = Resources["laborer"],  Manhours = 200 },
				},
				Resources = {
					{ Resources["bundles of titanium plates"], 10, true }
				}
			},
			3 = {
				Name = "Building internal structure",
				Required = true,
				Labor = {
					{ Type = Resources["engineer"], Manhours = 5  },
					{ Type = Resources["laborer"],  Manhours = 100 },
				},
				Resources = {
					{ Resources["bundles of titanium beams"], 4, true },
					{ Resources["bundles of titanium plates"], 5, true },
					{ Resources["spools of electrical wiring"], 2, true },
					{ Resources["bundles of high pressure pipes"], 2, true },
				}
			},
			4 = {
				Name = "Installing internal computer system",
				Required = true,
				Labor = {
					{ Type = Resources["engineer"], Manhours = 100 },
				},
				Resources = {
					{ Resources["computer system"], 10, true },
				}
			}
			5 = {
				Name = "Furnishing interior",
				Required = false,
				Labor = {
					{ Type = Resources["laborer"], Manhours = 10 },
				}
				Resources = {
					{ Resources["assorted furniture"], 10, true },
				}
			}
		}
	},
}
Take a look at the Bins = {} table. I'm calling a function that's stored elsewhere (in a table called ModulePresets) that returns a table that fits with my format. So basically, you can use Lua to simplify your Lua data formatting, easily.

Yeah, I know this is simple stuff. But think of the other uses - you could write a table that stores entries and allows you to index them by ID or by name - in fact, I'm doing just that with my Resources table. It doesn't even care if you use a singular form of the name or a plural name. The possibilities are endless, hopefully I've given you some ideas.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Leveraging Lua as a data storage format for your game

Post by ejmr »

You can also take advantage of metatables and some syntax sugar to make Lua a friendly data format for non-programmers, which I've been doing on a project.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
Kyle
Party member
Posts: 146
Joined: Sat Mar 16, 2013 9:46 pm

Re: Leveraging Lua as a data storage format for your game

Post by Kyle »

ejmr wrote:You can also take advantage of metatables and some syntax sugar to make Lua a friendly data format for non-programmers, which I've been doing on a project.
Nice! I doubt I have to worry about non-programmers in my project, but that's pretty cool.
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests