Skip to content
Permalink
Browse files

Merge pull request #1163 from Simi23/feature/is-element-data

Add hasElementData Lua feature to client and serverside
  • Loading branch information
qaisjp committed Mar 14, 2020
2 parents ddbb65b + 8ed3e11 commit 8b98f795ab154041d321e63a918ee1d90d8f1e79
@@ -48,6 +48,7 @@ void CLuaElementDefs::LoadFunctions()
{"getAttachedElements", GetAttachedElements},
{"getElementDistanceFromCentreOfMassToBaseOfModel", GetElementDistanceFromCentreOfMassToBaseOfModel},
{"isElementLocal", IsElementLocal},
{"hasElementData", HasElementData},
{"getElementAttachedOffsets", GetElementAttachedOffsets},
{"getElementAlpha", GetElementAlpha},
{"isElementOnScreen", IsElementOnScreen},
@@ -134,6 +135,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
lua_classfunction(luaVM, "isStreamedIn", "isElementStreamedIn");
lua_classfunction(luaVM, "isStreamable", "isElementStreamable");
lua_classfunction(luaVM, "isLocal", "isElementLocal");
lua_classfunction(luaVM, "hasData", "hasElementData");
lua_classfunction(luaVM, "isSyncer", "isElementSyncer");
lua_classfunction(luaVM, "getChildren", "getElementChildren");
lua_classfunction(luaVM, "getChild", "getElementChild");
@@ -969,7 +971,7 @@ int CLuaElementDefs::GetElementsWithinRange(lua_State* luaVM)
{
// Query the spatial database
CClientEntityResult result;
GetClientSpatialDatabase()->SphereQuery(result, CSphere{ position, radius });
GetClientSpatialDatabase()->SphereQuery(result, CSphere{position, radius});

lua_newtable(luaVM);
unsigned int index = 0;
@@ -27,6 +27,7 @@ class CLuaElementDefs : public CLuaDefs
LUA_DECLARE(GetElementByID);
LUA_DECLARE(GetElementByIndex);
LUA_DECLARE(GetElementData);
LUA_DECLARE(HasElementData);
LUA_DECLARE(GetElementParent);
LUA_DECLARE_OOP(GetElementMatrix);
LUA_DECLARE_OOP(GetElementPosition);
@@ -69,6 +69,7 @@ void CLuaElementDefs::LoadFunctions()

// Element data
{"getElementData", GetElementData},
{"hasElementData", HasElementData},
{"setElementData", setElementData},
{"removeElementData", removeElementData},

@@ -160,6 +161,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
lua_classfunction(luaVM, "getAllData", "getAllElementData");
lua_classfunction(luaVM, "getColShape", "getElementColShape");
lua_classfunction(luaVM, "getData", "getElementData");
lua_classfunction(luaVM, "hasData", "hasElementData");
lua_classfunction(luaVM, "getPosition", "getElementPosition", OOP_getElementPosition);
lua_classfunction(luaVM, "getRotation", "getElementRotation", OOP_getElementRotation);
lua_classfunction(luaVM, "getMatrix", "getElementMatrix", OOP_getElementMatrix);
@@ -206,6 +208,7 @@ void CLuaElementDefs::AddClass(lua_State* luaVM)
lua_classvariable(luaVM, "velocity", "setElementVelocity", "getElementVelocity", setElementVelocity, OOP_getElementVelocity);
lua_classvariable(luaVM, "angularVelocity", "setElementAngularVelocity", "getElementAngularVelocity", setElementTurnVelocity, OOP_getElementTurnVelocity);
lua_classvariable(luaVM, "isElement", NULL, "isElement");
// Don't know how this works, but don't forget to add isElementData if needed
// lua_classvariable(luaVM, "data", "setElementData", "getElementData", OOP_setElementData, OOP_getElementData);
// lua_classvariable(luaVM, "visibility", "setElementVisibleTo", "isElementVisibleTo", OOP_setElementVisibleTo, CLuaOOPDefs::IsElementVisibleTo); //
// .visibility[john]=false
@@ -1025,7 +1028,7 @@ int CLuaElementDefs::getElementsWithinRange(lua_State* luaVM)
{
// Query the spatial database
CElementResult result;
GetSpatialDatabase()->SphereQuery(result, CSphere{ position, radius });
GetSpatialDatabase()->SphereQuery(result, CSphere{position, radius});

lua_newtable(luaVM);
unsigned int index = 0;
@@ -68,6 +68,7 @@ class CLuaElementDefs : public CLuaDefs

// Element data
LUA_DECLARE(GetElementData);
LUA_DECLARE(HasElementData);
LUA_DECLARE(setElementData);
LUA_DECLARE(removeElementData);

@@ -49,3 +49,39 @@ int CLuaElementDefs::GetElementData(lua_State* luaVM)
lua_pushboolean(luaVM, false);
return 1;
}

int CLuaElementDefs::HasElementData(lua_State* luaVM)
{
// bool hasElementData ( element theElement, string key [, bool inherit = true ] )

#ifdef MTA_CLIENT
CClientEntity* pElement;
#else
CElement* pElement;
#endif
SString strKey;
bool bInherit;

CScriptArgReader argStream(luaVM);
argStream.ReadUserData(pElement);
argStream.ReadString(strKey);
argStream.ReadBool(bInherit, true);

if (argStream.HasErrors())
{
return luaL_error(luaVM, argStream.GetFullErrorMessage());
}

if (strKey.length() > MAX_CUSTOMDATA_NAME_LENGTH)
{
// Warn and truncate if key is too long
m_pScriptDebugging->LogCustom(luaVM, SString("Truncated argument @ '%s' [%s]", lua_tostring(luaVM, lua_upvalueindex(1)),
*SString("string length reduced to %d characters at argument 2", MAX_CUSTOMDATA_NAME_LENGTH)));
strKey = strKey.Left(MAX_CUSTOMDATA_NAME_LENGTH);
}

// Check if data exists with the given key
bool exists = pElement->GetCustomData(strKey, bInherit) != nullptr;
lua_pushboolean(luaVM, exists);
return 1;
}

0 comments on commit 8b98f79

Please sign in to comment.
You can’t perform that action at this time.