为我的游戏im试图完成由C++以下数组结构,因为数据来自外部来源,应该在lua_script中可用。我可以把lua_table放在另一个表中来通过C++来构建一个多维数组吗?
数组结构应该看起来像这样:(数据在映射中,映射包含变量的名称和对的列表(每对是被认为是一个子数组中的一个元素的关键值对) ...
在地图制作的数据是完整的,结构definetly没关系。 所以基本上我有
typedef std::map<std::string, std::list<std::pair> >;
/\index(e.g: sword) /\ /\
|| ||
|| Pair: Contains two strings (key/value pair)
||
List of Pairs for each array
items = {
["sword"] = {item_id = 1294, price = 500},
["axe"] = {item_id = 1678, price = 200},
["red gem"] = {item_id = 1679, price = 2000},
}
我得到了,使f AR现在是:
for(ArrayMap::iterator it = this->npc->arrayMap.begin(); it != this->npc->arrayMap.end(); it++) {
std::string arrayName = (*it).first;
if((*it).second.size() > 0) {
lua_newtable(luaState);
for(ArrayEntryList::iterator itt = (*it).second.begin(); itt != (*it).second.end(); itt++) {
LuaScript::setField(luaState, (*itt).first.c_str(), (*itt).second.c_str());
}
lua_setglobal(luaState, arrayName.c_str());
}
}
但是,这只会产生以下结构:
(table)
[item_id] = (string) 2000
[name] = (string) sword
[price] = (string) 500
的问题是,该表可以ofcourse只包含每个索引一次。 这就是为什么我需要像“表中的一张桌子”这样的事情吗?
有没有办法做到这一点?我很高兴任何提示。
嗯,我想你让我错了,或者我解释错了。项目名称和ID是唯一的,但我需要为每个表格字段分配一个数组而不是一个值,因为每个项目都可以有可选数量的数据。 – Steini
std :: map>其中Properties是为了存储某些数据而创建的结构,而key是名称(或者可以使其成为id,在这种情况下,key将会是int)。根据你的例子,你可以创建一个Property结构/类,它携带可变数量的字段,如int id,std :: vector 价格/成本等。 –