Skip to content

Using `argStream.ReadEnumString`

Qais Patankar edited this page Aug 19, 2019 · 3 revisions

function ReadEnumString is used for white-listing string value.

If you want add example that umpteenth argument is equal "one", "two" or "three", you have to follow steps below:

  1. declare your enum
enum eNumbers
{
  ONE,
  TWO,
  THREE,
}

in header file related to application, example mtasa-blue\Client\mods\deathmatch\logic\lua\CLuaFunctionParseHelpers.h

  1. In file mtasa-blue\Client\mods\deathmatch\logic\lua\CLuaFunctionParseHelpers.h below others DECLARE_ENUM you add your declaration of enum: DECLARE_ENUM(eNumbers)

  2. In same file, below others GetClassTypeName you have to add following lines defined what name is displayed if error occurs.

inline SString GetClassTypeName(eNumbers*)
{
    return "number";
}
  1. Now move to mtasa-blue\Client\mods\deathmatch\logic\lua\CLuaFunctionParseHelpers.cpp file and add what every enum field mean. Goto below others IMPLEMENT_ENUM_BEGIN, ADD_ENUM and IMPLEMENT_ENUM_END declaration, and add your enum:
IMPLEMENT_ENUM_BEGIN(eNumbers)
//                   ^ enum name.
ADD_ENUM(ONE, "one")
ADD_ENUM(TWO, "two")
ADD_ENUM(THREE, "three")
// THREE is field in enum, second argument is friendly string which user put at specified argument.
IMPLEMENT_ENUM_END("number")
//                  ^ name displayed for user if error occur. should be same as above.

Now, you can use your enum in function, example:

CScriptArgReader argStream(luaVM);
eNumbers eNumber;
argStream.ReadEnumString(eNumber);
if (!argStream.HasErrors())
{
     lua_pushnumber(luaVM, eNumber)
     return 1;
else
     m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());

lua_pushboolean(luaVM, false);
return 1;

Example this function is called convertStringToNumber where first argument is our enum,

now in lua

convertStringToNumber( "one" ) -- return: 1
convertStringToNumber( "two" ) -- return: 2
convertStringToNumber("three") -- return: 3
convertStringToNumber( "four" ) -- return false and throw error with information that first argument should be `number`.

Authors: CrosRoad95, Shaviq

Clone this wiki locally