• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Referencing LUA Tables in C++ Sources

  • Thread starter Thread starter Icy
  • Start date Start date
I

Icy

Guest
Hello any potential thread readers, let me just tell you that I've been trying to figure this out for about two and a half hours now (currently 5:05 AM here :blink:). Let me explain my dilemma:

The recent `doPlayerSetSpecialDescription` function was removed and as a result, one of my custom scripts that I was heavily working on got basically butt-fucked. I know that in player.cpp around lines 184-217 has this tidbit of code in it:
Code:
184	{
185		s << nameDescription;
186		if(!hasCustomFlag(PlayerCustomFlag_HideLevel))
187			s << " (Level " << level << ")";
188
189		s << ". " << (sex % 2 ? "He" : "She");
190		if(hasFlag(PlayerFlag_ShowGroupNameInsteadOfVocation))
191			s << " is " << group->getName();
192		else if(vocation_id != 0)
193			s << " is " << vocation->getDescription();
194		else
195			s << " has no profession";
196
...		s << getSpecialDescription();
...	}
...
...	std::string tmp;
...	if(marriage && IOLoginData::getInstance()->getNameByGuid(marriage, tmp))
...	{
...		s << ", ";
...		if(vocation_id == 0)
...		{
...			if(lookDistance == -1)
...				s << "and you are";
...			else
...				s << "and is";
...
...			s << " ";
...		}
...
...		s << (sex % 2 ? "husband" : "wife") << " of " << tmp;
...	}
...
...     s << ".";

And I'm wondering how I would insert a value based on what variables I have declared in an LUA creaturescript.

I know where I need to put it already
Line 187 said:
s << (sex % 2 ? "he" : "she") << " is insertLuaVariableHere (Level " << level << ")";
but unsure how I would go about implementing it, any help?

My LUA table looks somewhat like this:
LUA:
local ADJECTIVE, MAX_ADJECTIVE = {
	[0] = {210, ""},
	[1] = {190, "awesome"},
	[2] = {45, "prosauce"}
}, 2

Bonus points if you can figure out how to display it in ADJECTIVE[1] colour (if that's even possible!).

Thanks in advance :)
 
If you are using 0.4_DEV then you can set string as storage, so you can do somethnig like:
Code:
doCreatureSetStorage(cid, 12345, ADJECTIVE[1][2])

Then in C++ part:
Code:
std::string strg;
getStorage(12345, strg);

s << (sex % 2 ? "he" : "she") << " is " << strg << "(Level " << level << ")";
 
doPlayerSetSpecialDescription was removed? o.0

Code:
int32_t LuaInterface::luaDoPlayerSetSpecialDescription(lua_State* L)
{
	//doPlayerSetSpecialDescription(cid, description)
	std::string description = popString(L);

	ScriptEnviroment* env = getEnv();
	if(Player* player = env->getPlayerByUID(popNumber(L)))
	{
		player->setSpecialDescription(description);
		lua_pushboolean(L, true);
	}
	else
	{
		errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
		lua_pushboolean(L, false);
	}

	return 1;
}
I still got it there...
 
If you are using 0.4_DEV then you can set string as storage, so you can do somethnig like:
Code:
doCreatureSetStorage(cid, 12345, ADJECTIVE[1][2])

Then in C++ part:
Code:
std::string strg;
getStorage(12345, strg);

s << (sex % 2 ? "he" : "she") << " is " << strg << "(Level " << level << ")";

Thanks Chojrak!

Ill have to test this out after the latest items.otb error is fixed. :)
 
Heres a little snipplet of code from a non-tibia related project I'm doing.

You'll be able to understand how it works if you simply analyze the code some.
Code:
//L->getGlobalFieldInteger(MAX_ADJECTIVE, 190, temp);
bool LuaScript::getGlobalFieldInteger(char* table, char* key, int &value)
{

	lua_getglobal(L, table);
	if (!lua_istable(L, -1))
	{
		lua_pop(L,1);
		return false;
	}

	lua_pushstring(L, key);
	lua_gettable(L, -2);
	if (!lua_isnumber(L, -1))
	{
		lua_pop(L, 1);
		return false;
	}

	value = (int)lua_tonumber(L, -1);
	lua_pop(L, 1);  // remove number and key
	return true;

}
 
Heres a little snipplet of code from a non-tibia related project I'm doing.

You'll be able to understand how it works if you simply analyze the code some.
Code:
//L->getGlobalFieldInteger(MAX_ADJECTIVE, 190, temp);
bool LuaScript::getGlobalFieldInteger(char* table, char* key, int &value)
{

	lua_getglobal(L, table);
	if (!lua_istable(L, -1))
	{
		lua_pop(L,1);
		return false;
	}

	lua_pushstring(L, key);
	lua_gettable(L, -2);
	if (!lua_isnumber(L, -1))
	{
		lua_pop(L, 1);
		return false;
	}

	value = (int)lua_tonumber(L, -1);
	lua_pop(L, 1);  // remove number and key
	return true;

}

Mmm, I guess that could work; not sure which way I'll try first now :/
 

Similar threads

Back
Top