• 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!

[c++] passing lua tables arguments through c++ luainterface

Doggynub

LUA / C++
Joined
Sep 28, 2008
Messages
2,541
Reaction score
186
okay i need to have this table in lua
LUA:
local status = { equip = int , hold= int, depot = int, slot= int)

I want to make it like that with the lua api
i have a std:: map like that :
[cpp]
std::map<std::string,int> status;

status["equip"] = int;
status["depot"] = int;
status["hold"] = int;
status["slot"] = int;
[/cpp]

Now in the c++ this supposed to be a argument in a function in creatureevent, so how to make it the proper way i have it like that
[cpp]
lua_newtable(L);
LuaInterface::setField(L,"equip",(int32_t)status["equip"]);

LuaInterface::setField(L,"hold", (int32_t)status["hold"]);

LuaInterface::setField(L,"depot",(int32_t)status["depot"]);

LuaInterface::setField(L,"slot", (int32_t)status["slot"]);

[/cpp]

and i get that error in console when the event supposed to be executed
Code:
[Error - CreatureScript Interface]
data/creaturescripts/scripts/move.lua:onMoveItem
Description:
attempt to call a number value
stack traceback:
 
Last edited:
Create a creatureevent function and pass the parameters you want to have and then look at how it is send in lua..
Also you need to look at how it is checked if there is a creatureevent?
[cpp]uint32_t CreatureEvent::executeLook(Player* player, Thing* thing, const Position& position, int16_t stackpos, int32_t lookDistance)
{
//onLook(cid, thing, position, lookDistance)
if(m_interface->reserveEnv())
{
ScriptEnviroment* env = m_interface->getEnv();
if(m_scripted == EVENT_SCRIPT_BUFFER)
{
env->setRealPos(player->getPosition());
std::stringstream scriptstream;
scriptstream << "local cid = " << env->addThing(player) << std::endl;

scriptstream << "local thing = " << env->addThing(thing) << std::endl;
env->streamPosition(scriptstream, "position", position, stackpos);
scriptstream << "local lookDistance = " << lookDistance << std::endl;

scriptstream << m_scriptData;
bool result = true;
if(m_interface->loadBuffer(scriptstream.str()))
{
lua_State* L = m_interface->getState();
result = m_interface->getGlobalBool(L, "_result", true);
}

m_interface->releaseEnv();
return result;
}
else
{
#ifdef __DEBUG_LUASCRIPTS__
char desc[30];
sprintf(desc, "%s", player->getName().c_str());
env->setEventDesc(desc);
#endif

env->setScriptId(m_scriptId, m_interface);
env->setRealPos(player->getPosition());

lua_State* L = m_interface->getState();
m_interface->pushFunction(m_scriptId);

lua_pushnumber(L, env->addThing(player));
LuaScriptInterface::pushThing(L, thing, env->addThing(thing));

LuaScriptInterface::pushPosition(L, position, stackpos);
lua_pushnumber(L, lookDistance);

bool result = m_interface->callFunction(4);
m_interface->releaseEnv();
return result;
}
}
else
{
std::cout << "[Error - CreatureEvent::executeLook] Call stack overflow." << std::endl;
return 0;
}
}[/cpp]
 
already know that, as i can pass nomral arguments
[cpp]//custom moveuint32_t CreatureEvent::executeOnMove(Player* player, Item* item, const Position& fromPosition, const Position& toPosition,
Item* fromItem, Item* toItem, Item* fromGround, Item* toGround, std::map<std::string,int> status)
{
//onMoveItem(cid, item, formPosition, toPosition, fromItem, toItem, fromGround, toGround, equip, hold, depot, slot)
if(m_interface->reserveEnv())
{
ScriptEnviroment* env = m_interface->getEnv();
if(m_scripted == EVENT_SCRIPT_BUFFER)
{
env->setRealPos(player->getPosition());
std::stringstream scriptstream;
scriptstream << "local cid = " << env->addThing(player) << std::endl;


env->streamThing(scriptstream, "item" ,item, env->addThing(item));
env->streamPosition(scriptstream, "fromPosition", fromPosition);
env->streamPosition(scriptstream, "toPosition", toPosition);



env->streamThing(scriptstream, "fromItem",fromItem, env->addThing(fromItem));
env->streamThing(scriptstream, "toItem",toItem, env->addThing(toItem));
env->streamThing(scriptstream, "fromGround",fromGround, env->addThing(fromGround));
env->streamThing(scriptstream, "toGround",toGround, env->addThing(toGround));




scriptstream << "equip = " << (int32_t)status["equip"] << std::endl;
scriptstream << "hold = " << (int32_t)status["hold"] << std::endl;
scriptstream << "depot = " << (int32_t)status["depot"] << std::endl;
scriptstream << "slot = " << (int32_t)status["slot"] << std::endl;




if(m_scriptData)
scriptstream << *m_scriptData;


bool result = true;
if(m_interface->loadBuffer(scriptstream.str()))
{
lua_State* L = m_interface->getState();
result = m_interface->getGlobalBool(L, "_result", true);
}


m_interface->releaseEnv();
return result;
}
else
{
#ifdef __DEBUG_LUASCRIPTS__
char desc[30];
sprintf(desc, "%s", player->getName().c_str());
env->setEvent(desc);
#endif


env->setScriptId(m_scriptId, m_interface);
env->setRealPos(player->getPosition());


lua_State* L = m_interface->getState();
m_interface->pushFunction(m_scriptId);


lua_pushnumber(L, env->addThing(player));

LuaInterface::pushThing(L, item, env->addThing(item));
LuaInterface::pushPosition(L, fromPosition);
LuaInterface::pushPosition(L, toPosition);


LuaInterface::pushThing(L, fromItem, env->addThing(fromItem));
LuaInterface::pushThing(L, toItem, env->addThing(toItem));
LuaInterface::pushThing(L, fromGround, env->addThing(fromGround));
LuaInterface::pushThing(L, toGround, env->addThing(toGround));






lua_pushnumber(L, status["equip"]);
lua_pushnumber(L, status["hold"]);
lua_pushnumber(L, status["depot"]);
lua_pushnumber(L, status["slot"]);








bool result = m_interface->callFunction(12);
m_interface->releaseEnv();
return result;
}
}
else
{
std::clog << "[Error - CreatureEvent::executeMove] Call stack overflow." << std::endl;
return 0;
}
}
//
[/cpp]



but can't make a table having keys like i said up, it always gives error ,so i cant check it.
 
Back
Top