C++ Modules?

There is no up to date documentation I am aware of on writing C++ code for this game. Does anyone know how to do this for the modern game who feels like sharing? Preferably for the modern Visual Studio.

This is not the most useful but it is a start



using namespace std;
using namespace GarrysMod::Lua;

int ConCommandRemove( lua_State *state )
{
    LUA->CheckType( 1, Type::STRING );
    
    const char* command = LUA->GetString(1);
    if ( cvar->FindCommand(command) ) {
        cvar->UnregisterConCommand( cvar->FindCommandBase(command) );
        LUA->PushBool(true);
    } else {
        LUA->PushBool(false);
    }
    return 1;
}

GMOD_MODULE_OPEN()
{
    LUA->PushSpecial( SPECIAL_GLOB );
        LUA->GetField(-1, "engine");
            LUA->PushCFunction( ConCommandRemove ); LUA->SetField(-2, "UnregisterConCommand");
        LUA->Pop();
    LUA->Pop();
    
    return 0;
}


and if you want to create a table, you can do this



LUA->CreateTable();
    LUA->PushCFunction( AnotherFunction ); LUA->SetField(-2, "AnotherFunction");
LUA->SetField(-2, "newtable");


This is what I’ve found and have been using.

using namespace std;

:((

What’s the issue with doing that? There’s not going to be any overlap there, if you were throwing a “using namespace …” in a header, then you’re asking for something to go wrong, but in a single really basic cpp file, I really don’t see any trouble.

It’s a very bad habit, and it doesn’t take much longer to write std:: in front of the standar library functions, especially when coding a gmod module.

I hardly ever use c++ and don’t really know much of it, it’s just that I needed to do a few things which won’t possible with lua.
But thanks for the tip, I’ll keep it in mind.

You weren’t even using std library functions anyway, nor were you including headers that you need.

I suggest using BlackAwps’ backward headers along with older tutorials. The new ones Garry provided are useless and make things pointlessly difficult.

[editline]20th June 2013[/editline]

Also, you won’t notice a huge difference between VS2008, VS2010 and VS2012 - some of the menus have changed around, but everything’s in pretty much the same place.

Yeah, that was only a snippet of what I had.

Look at the headers garry provides, the functions he gives are almost completely analogous to the Lua C API: http://www.lua.org/manual/5.1/manual.html#3

Also, the example garry provides is a useful place to start.

Honestly I wouldn’t recommend using the backwards headers if you’re actually looking to learn from this, if you can figure out how the stack works and understand it, it’s infinitely more valuable. If you’re just looking to quickly make/port something from GMod 12 over then go for the backwards headers.

[editline]20th June 2013[/editline]

The only main difference is how userdata is handled, and I think I documented that on the wiki somewhere a while back

Yeah, if you really want to make GMod modules, please abstain from using the backwards headers. It’s an additional wrapper on top of a wrapper (since Garry’s interface wraps the Lua C API). It also allows you to quickly port your knowledge to the plain Lua C API.

Actually no, it’s a wrapper on top of a wrapper that only uses half of the underlying functions. All of the old ILuaInterface functions exist inside the lua_shared dll.

Tutorials for the new for the new API are scarce and don’t really cover much, the old one on the old wiki’s much better and it covers basic IDE setup.

The IDE setup is mostly the same, we don’t need tutorials on the new API because it’s basically the same as the Lua API and there are plenty of tutorials and information available on that.

[editline]20th June 2013[/editline]

You don’t even have to do the IDE setup yourself, just take garrys example and rename it

I agree that you should use garry’s headers if your adding one or two functions, or just messing around. If you actually want to facilitate C++ on your server, or if you’re adding more than 2 functions you should use the backwards headers, it will make your life a bunch easier.

I also agree that knowing how the Lua stack works and using functions similar to lua_* would prove useful down the line, I don’t agree that this is a good place to start learning though, the easier the better.

I also wouldn’t recommend using my headers as a place to learn other than maybe looking at how something works in it. I only made the backwards compatible stuff for lazy people like myself who didn’t want to have to rewrite a module.

If you learn the Lua API then you’ll understand the new one. The backwards headers are there mainly to allow compiling of old modules.

I was under the impression OP didn’t have C++ coding skills, coming from a Lua background could be difficult, trying to understand C-side Lua at the same time could become even more difficult and deter people from learning it.

Which is why you should learn now instead of putting it off

I’ve already made a module for Couchbase using the new headers, it’s really not that hard, especially with the MySQLoo SVN contents being fully in the 13 format to reference.