Not sure whether this belongs in Developer Discussion, but it seems to fit more than Programming.
It would be cool to see a new module tutorial, the old one by Overv looks quite outdated and I don't really understand the example.
I'd really like to implement some stuff for OSC and Android.
There were two other threads asking the same thing. Not many people seem to know, and they are disinterested.
A simple hello world tutorial would be nice just to get people started
I would love to add a tutorial on the wiki but i really cant figure out how to. If you have any specific questions though i could probably answer them for you.
Garry has an example on his github..
[url]https://github.com/garrynewman/gmod-module-base/blob/master/example/src/gm_example.cpp[/url]
Learn how the lua stack works. I have a post I made awhile ago of me experimenting with the lua stack that might help you in learning how it works. In regards for adding a function to the global table, garry has a tutorial that covers that on the page that hosts the new module headers. I haven't tried making lua hooks in c++, but I imagine it's basically the same as it is in gm12 modules.
Really the best way to learn is seeing how other people do it. There are several gm13 modules you can find and look at. Other than basic lua stack stuff, the rest is pretty much just plain old c++ and shouldn't be too hard to pick up.
[QUOTE=obliviator;41644143]I would love to add a tutorial on the wiki but i really cant figure out how to. If you have any specific questions though i could probably answer them for you.[/QUOTE]
If you login to the Wiki and navigate to: [url]http://wiki.garrysmod.com/page/Binary_Module_Tutorial[/url] or [url]http://wiki.garrysmod.com/page/Simple_Binary_Module[/url] there should be an option to edit the page.
[QUOTE=BlackAwps;41644522]Garry has an example on his github..
[url]https://github.com/garrynewman/gmod-module-base/blob/master/example/src/gm_example.cpp[/url][/QUOTE]
I looked at the example, I couldn't really understand it.
I also looked at Overvs' tutorial from a while back, which doesn't seem to hold much relevance with the new headers.
The tutorial on the wiki isn't really a tutorial, it's more a "where to start when creating binary modules".
It would be good if someone could give a walk through of the github example though.
What do you not understand about the example?
Quite a few things, I'll post some questions later
I first tried to get into c++ by making binary modules for gmod. I had a pretty rough time with the little c++ experience I had. If you aren't too experienced, I would really reccomend you practice with some other projects. Looking at module code now from gm12, it makes perfect sense to me, whereas about a year and a half ago I had no idea what I was looking at.
I think I'm fine experience wise.
I just don't understand binding stuff to Lua.
Like, I don't understand why you would push the name of the function and then the actual function instead of just doing that in one.
[editline]30th July 2013[/editline]
A simple tutorial, from start to finish, explaining each line would be great. Although, that's probably asking too much.
There's like what, less than 50 lines in the example?
Since the garrysmod lua binary module interface is so similar to the standard lua C API, your best bet is to read about that.
[url]http://www.lua.org/pil/24.html[/url]
[url]http://www.lua.org/manual/5.1/manual.html#3[/url]
[QUOTE=jaooe;41651052]I think I'm fine experience wise.
I just don't understand binding stuff to Lua.
Like, I don't understand why you would push the name of the function and then the actual function instead of just doing that in one.
[editline]30th July 2013[/editline]
A simple tutorial, from start to finish, explaining each line would be great. Although, that's probably asking too much.
There's like what, less than 50 lines in the example?[/QUOTE]
SetTable assigns a given value to a given index in a given table. To do that you need three things, 1) the table, 2) the index, 3) the value. You push those three things onto the stack then make the SetTable call with the posititon to start at.
[lua]
LUA->PushSpecial( GarrysMod::Lua::SPECIAL_GLOB ); // Push global table
LUA->PushString( "TestFunction" ); // Push Name
LUA->PushCFunction( MyExampleFunction ); // Push function
LUA->SetTable( -3 ); // Set the table [/lua]
The first line is the table, in this case the Global table.
The second line is the index, in this case it's the string "TestFunction".
The third line is the value, in this case it's the function.
These three values are now on the stack, so the stack looks like this
[code]
-3: _G
-2: "TestFunction"
-1: MyExampleFunction
[/code]
We call SetTable with -3 because this is the position of the table we want to start with.
As we read in the Lua manual for the Lua C API equivalent [url=http://www.lua.org/manual/5.1/manual.html#lua_settable]lua_settable[/url], the index and the value are then popped (removed) from the stack, so now it looks like this
[code]
-1: _G
[/code]
Technically we should then call
[code]
LUA->Pop();
[/code]
to remove that last _G from the stack as we don't need it anymore, but garry has neglected to in this example.
[QUOTE=jaooe;41651052]I think I'm fine experience wise.
I just don't understand binding stuff to Lua.
Like, I don't understand why you would push the name of the function and then the actual function instead of just doing that in one.
[editline]30th July 2013[/editline]
A simple tutorial, from start to finish, explaining each line would be great. Although, that's probably asking too much.
There's like what, less than 50 lines in the example?[/QUOTE]
[url]http://lua-users.org/wiki/BindingCodeToLua[/url]
Most of the interface is just a direct call to the original lua counter-part.
Sorry, you need to Log In to post a reply to this thread.