Welcome to The Forum

Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads

Recommended Posts

I finally decided to start the gmod standard library. This is a library to be used when creating binary modules for gmod servers/clients. It is a collection of useful functions that make the development of binary modules physiologically possible. I just started it today, and plan to add a lot more features. I'll post a link to the api documentation once I finish writing it. When complete, this library will allow anything that can be done in lua to be implemented in c++.

 

The current set of functions:

 

A simple function to print a string to the console. Versions for both string types exist.

void PrintC(const char* msg);
void PrintC(std::string& msg);

 

Example:

PrintC("Hello World!");

 

 

A simple function to print a string to the console with the specified color. Versions for both string types exist.

void PrintC(const char* msg, Color& c);
void PrintC(std::string msg, Color& c);

 

Example:

PrintC("Hello World!", Color(255, 0, 0, 255));

Example:

PrintC("Hello World!", COLOR_RED);

 

 

A simple utility to quickly concatenate strings.

std::string formstring();

Example:

PrintC(formstring("Player ", player_name, " has joined team ", team_number));

 

 

A function to execute a console command.

void RunCommand(lua_State* state, const char* cmd);

 

Example:

RunCommand(LUA, "npc_create npc_citizen");

 

 

A function to compile a string containing lua code into lua bytecode, then execute it.

void RunLua(lua_State* state, const char* lua);

 

Example:

const char* code = "local players = player.GetAll()"
"PrintTable(players)";
RunLua(LUA, code);

 

 

A function to register functions written in c++, and make them available for use in Garry's Mod lua.

void RegisterFunction(lua_State* state, const char* name, CFunc& func);

 

Example:


int SendToConsole(lua_State* state) {
if(LUA->GetType(1) == Type::STRING) {
PrintC(formstring("[Test Function]: ", LUA->GetString(1));
}
return 1;
}

...

RegisterFunction(LUA, "SendToConsole", SendToConsole);

 

In Progress:

 

A function to register a c++ function into a game event hook.

void Hook(lua_State* state, const char* hook, const char* name, GarrysMod::Lua::CFunc& func);

 

Example:

 Hook(LUA, "PlayerSpawn", "awesome_spawnhandler", PlayerSpawnHandler);

Edited by Techmo
  • Like 1
Link to comment
Share on other sites

shit post

 

explain this library thing a bit more to me.

what will it functions be for since you will add more features

 

The functions will pretty much be the standard set of functions provided by Garry's Mod in glua, but implemented in c++ to make them faster. I also would like to add a few things that seem useful to me and make life easier.

 

A library is pretty much a set of prewritten functions and code to be used by a developer. Writing directx, which is millions of lines of code, from scratch every time you make a new game would be tedious, so you use the directx library from microsoft. It's just the same thing here, i've done the work of implementing these functions in c++ to make things easier.

 

If you were to use just the headers given to you from Garrys Mod, to print a message to the game chat would require the following:

state->luabase->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
state->luabase->GetField(-1, "PrintMessage"); // Get the function field from lua
state->luabase->PushNumber(3); // Push a function parameter to the global stack. Print to game chat and console, from the Garry's mod HUD enumerations (3 = chat + console)
state->luabase->PushString("hello players");
state->luabase->Call(2, 0); // Call with 2 arguments, expecting 0 return value
state->luabase->Pop(); // Pop the values from the lua stack

 

Using the library, the same could be done with:

Broadcast(LUA, "hello players");

 

(I just implemented this in the library)

Edited by Techmo
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share