This is basically a data structure and retrival system, external data has to come from somewhere and as I love using Lua I thought it the perfect opportunity to use it now, there will be some AI automation further down the line I'll keep you updated on the code and systems, I will not lean to heavily on Lua .. you'll see where I am going
This "city_placment.lua" is a data file, it will eventfully hold a massive amount of Lua table data and a few functions for C++ to retrieve the data
--#######################################
-- City Data file
--#######################################
-- Define table of city positions
local tCity = {}
tCity["Land 1"] = {}
tCity["Land 1"]["pos"] = {}
tCity["Land 1"]["rot"] = {}
tCity["Land 1"]["pos"]["x"] = 101
tCity["Land 1"]["pos"]["y"] = 102
tCity["Land 1"]["pos"]["z"] = 103
tCity["Land 1"]["rot"]["x"] = 104
tCity["Land 1"]["rot"]["y"] = 105
tCity["Land 1"]["rot"]["z"] = 106
tCity["Land 2"] = {}
tCity["Land 2"]["pos"] = {}
tCity["Land 2"]["rot"] = {}
tCity["Land 2"]["pos"]["x"] = 201
tCity["Land 2"]["pos"]["y"] = 202
tCity["Land 2"]["pos"]["z"] = 203
tCity["Land 2"]["rot"]["x"] = 204
tCity["Land 2"]["rot"]["y"] = 205
tCity["Land 2"]["rot"]["z"] = 206
tCity["Land 3"] = {}
tCity["Land 3"]["pos"] = {}
tCity["Land 3"]["rot"] = {}
tCity["Land 3"]["pos"]["x"] = 301
tCity["Land 3"]["pos"]["y"] = 302
tCity["Land 3"]["pos"]["z"] = 303
tCity["Land 3"]["rot"]["x"] = 304
tCity["Land 3"]["rot"]["y"] = 305
tCity["Land 3"]["rot"]["z"] = 306
-- Called by C++ to get city position
function GetCityPosition(sName)
return tCity[sName]["pos"]
end
-- Called by C++ to get city rotation
function GetCityRotation(sName)
return tCity[sName]["rot"]
end
And this is the C++ code that runs the script and retrieves the data (the Lua C API does seem a little complex but its a piece of cake when you understand the stack system, and what seems like a overly complicated data system really does have massive advantages, thus why a lot of AAA games use it)
int ipX, ipY, ipZ;
int irX, irY, irZ;
if (luaL_dofile(m_Lua, "scripts/city_placment.lua") == LUA_OK)
{
// get city position
lua_getglobal(m_Lua, "GetCityPosition");
if lua_isfunction(m_Lua, -1)
{
lua_pushstring(m_Lua, "Land 2");
if (lua_pcall(m_Lua, 1, 1, NULL) == LUA_OK)
{
if (lua_istable(m_Lua, -1))
{
// Get X position
lua_getfield(m_Lua, -1, "x");
if (lua_isnumber(m_Lua, -1)) {ipX = lua_tonumber(m_Lua, -1);}
lua_pop(m_Lua, 1);// pop this value off the stack
// Get Y position
lua_getfield(m_Lua, -1, "y");
if (lua_isnumber(m_Lua, -1)) {ipY = lua_tonumber(m_Lua, -1);}
lua_pop(m_Lua, 1);// pop this value off the stack
// Get Z position
lua_getfield(m_Lua, -1, "z");
if (lua_isnumber(m_Lua, -1)) {ipZ = lua_tonumber(m_Lua, -1);}
lua_pop(m_Lua, 1);// pop this value off the stack
lua_pop(m_Lua, 1);// pop the table off the stack
// debug show position
char buffer[255];
std::sprintf(buffer, "City Position: X=%i Y=%i Z=%i", ipX, ipY, ipZ);
agk::Message(buffer);
}
}
else
{
agk::Message(lua_tostring(m_Lua, -1));
}
}
else
{
agk::Message("GetCityPosition: Not Found!");
}
// Get city rotation
lua_getglobal(m_Lua, "GetCityRotation");
if lua_isfunction(m_Lua, -1)
{
lua_pushstring(m_Lua, "Land 2");
if (lua_pcall(m_Lua, 1, 1, NULL) == LUA_OK)
{
if (lua_istable(m_Lua, -1))
{
// Get X rotation
lua_getfield(m_Lua, -1, "x");
if (lua_isnumber(m_Lua, -1)) { irX = lua_tonumber(m_Lua, -1); }
lua_pop(m_Lua, 1);// pop this value off the stack
// Get Y rotation
lua_getfield(m_Lua, -1, "y");
if (lua_isnumber(m_Lua, -1)) { irY = lua_tonumber(m_Lua, -1); }
lua_pop(m_Lua, 1);// pop this value off the stack
// Get Z rotation
lua_getfield(m_Lua, -1, "z");
if (lua_isnumber(m_Lua, -1)) { irZ = lua_tonumber(m_Lua, -1); }
lua_pop(m_Lua, 1);// pop this value off the stack
lua_pop(m_Lua, 1);// pop the table off the stack
// debug show position
char buffer[255];
std::sprintf(buffer, "City Rotation: X=%i Y=%i Z=%i", irX, irY, irZ);
agk::Message(buffer);
}
}
else
{
agk::Message(lua_tostring(m_Lua, -1));
}
}
else
{
agk::Message("GetCityRotation: Not Found!");
}
}
else
{
agk::Message(lua_tostring(m_Lua, -1));
}
// assuming everything went ok (no script errors, city name found, bla bla bla)
// create and position city here!
// START AGK CODE
// agk::LoadObject()
// agk::SetObjectPosition()
// agk::SetObjectRotation()
The project will end up having a dozen or so scripts like this for various assets, AI decisions and anything else I need to store externally, the benefit of this is I can tweak things while the game is running, offer modding capabilities, and of course, show off my coding skills
Approved?
For anyone else wondering what on earth this is all about, As I am now making my project in Tier2 I asked the Mods if I could incorporate Lua, external data has to come from somewhere this is no different to using XML or SQLite its just way more flexible, anyway, the Mods said they want to see code and examples of how I would be using Lua as its an AppGameKit competition, the AppGameKit API must take precedence, but as you can see from the code it really is no different to using RapidXML, just a million times more flexible