Skip to main content
gamedev.net
🔒 Locked

table

Started by luasitdown 2 replies 650+ views
Original Post
luasitdown
luasitdown

// lua content
table1 = {}
table1 ["arth"] = "meshName"
table1 ["minotaur"] = "meshName"
table1 ["villager"] = "meshName"
table1 ["footman"] = "meshName"
table1 ["skin_tiny"] = "meshName"


table2 = {}
table2 ["arth"] = "root"
table2 ["minotaur"] = "root"
table2 ["villager"] = "root"
table2 ["footman"] = "root"
table2 ["ogre"] = "root"

table2 = {}
table2["0"] = "wall00"
table2["111"] = "wall01"
table2["88"] = "wall02"
table2["33"] = "wall03"
table2["111"] = "wall04"
table2["0"] = "wall05"
table2["33"] = "wall06"
table2["0"] = "wall07"
table2["0"] = "wall08"
table2["0"] = "wall09"
table2["0"] = "wall10"
table2["88"] = "wall11"

table1 ["0.6"] = "key"
table1 ["2.2"] = "mouse"

// c++

lua_getglobal(L, tableName.c_str() );

tableInStack(L);

void Lua::tableInStack(lua_State * L , int tableIndex)
{
	lua_pushnil(L);

	// pop nil and put first key/value at step 1
	while(lua_next(L, tableIndex) != 0)
	{
		// value at top and 处理他
		if(lua_isstring(L, -1))
			//printf("%s = %s\n", lua_tostring(L, -2), lua_tostring(L, -1));
			//m_data[lua_tostring(L, -2)] = lua_tostring(L, -1);
			m_data.insert( MapStr::value_type(lua_tostring(L, -1), lua_tostring(L, -2)) );

		else if(lua_isnumber(L, -1))
			//printf("%s = %d\n", lua_tostring(L, -2), lua_tonumber(L, -1));
			m_numList.push_back(lua_tonumber(L, -1));

		else if(lua_istable(L, -1))
			tableInStack(L, tableIndex);

		//pop value , keep key 
		lua_pop(L, 1);
	}
}

question is: I can only load 4 wallXX, if you change line of wallXX ,you still load 4 .but content is different. sometimes you just load wall02 wall04 wall11 to table sometimes you load wall02 wall11 wall03 wall06 how to load all of item to table?
Kylotan
Kylotan
You keep using the same indexes in table2. This means you end up overwriting the old value at that index. Treat it like an array - 1 value per key.
luasitdown
luasitdown
Quote:
Original post by Kylotan
You keep using the same indexes in table2. This means you end up overwriting the old value at that index. Treat it like an array - 1 value per key.


Could you please explain more detail? thanks.
Kylotan
Kylotan
You are only using 4 indices: 0, 33, 88, and 111. These are the keys in your array. Each key links to one value. This means you are only ever storing 4 items. When you re-use the key, the old value is overwritten with the new one.

If you want to store more items, each one needs a unique key, just like a C++ std::map, or a C array (where the key is just the index number).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.