Leveraging Lua as a data storage format for your game
Posted: Thu Mar 21, 2013 11:38 pm
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:
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.
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 },
}
}
}
},
}
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.