How do you learn really good GLua?

I have a Question to the Developer Communtiy of GMOD.
How learned you Lua ?
What tips can you give me to learn Lua?
What the best way to learn Lua and GLua?

check this out!

everything is on the link above

This is what I post each time this question comes up:

Here are some resources which may help you:

The Lua manual, this will definitely help you get started!

Learn optimization techniques right away to avoid bad habits:
http://www.lua.org/gems/sample.pdf [PAGE 4 – But read the entire thing]

The New Garry’s Mod Wiki, has a ton of information, how to use the function calls, how to integrate vgui elements, and more!

Not everything is on the new Garry’s Mod Wiki yet, if it’s not there, it’s here. There is a lot of things that has changed since Garry’s Mod 12, to Garry’s Mod 13. This is primarily for 12.
http://maurits.tv/data/garrysmod/wik...index4875.html

One of the best places to start, if by modifying TTT. Start by changing the sweps, more realistic fire-rates, balance the damage, add a feature which changes the cone based on if you’re moving, firing from the hip, aiming down the sights, crouching, holding the walk button, etc etc. Start small, start by doing simple things and work your way up!

Thanks for the nice and fast Answer :smiley:

Good place to learn the syntax: Learn Lua in 15 Minutes

I would say the opposite, editing SWEPs isn’t going to teach you anything, start by creating your own scripts, make a round based gamemode, make an entity that hovers around the player, make a gun that shoots penises. Creating stuff from scratch gives you a much better understanding than any editing will do.

Don’t start by learning optimization methods, learn how to write clean functioning code first. ( Avoiding bad habits, that’s kind of ironic coming from you hehehe )

As for the rest, follow the links for the wikis that Acecool posted, they’re a good place to look for any hooks / functions you require.

[editline]30th December 2013[/editline]

Oh and code_gs posted a wonderful guide not even a week ago.

I’d have to agree with Matt here. I’m in no way a good coder or anything, but I’ve learnt the basics. I started out as Acecool recommends, by just editing stuff. Sure, it may teach you to read and somewhat understand Lua, but you’re still just walking out from what you can guess your way to.

So when I, after a year of doing this, sat down and tried to make a simple gamemode, you would just start all over. Starting out by editing code doesn’t really teach you anything. You might learn a bit from it, but take care not to just write over what others did. Find your own style and way of doing things, only then will you be able to code great stuff.

Starting out by editing TTT isn’t supposed to teach you everything; it’s to get you acquainted with the language, understanding the syntax, understanding how variables and other things work. It’s to give you a generalized background.

Lua.org is supposed to give you syntax, other tutorials, and more.

GLua is different than plain-Jane Lua. You have additional keywords, integration within a popular game, etc.

The reason I suggest people start out by editing stuff on TTT is for the purpose of getting their feet wet and wanting more. A lot of people jump in and get frustrated because they don’t understand a lot of things and that leads them to quit. Others like jumping right in, it depends on you. Slow-and-steady seems to be the safest route for a lot of people which is why I suggest it.

I dunno I learn best when I’m way out of my depth

so I’m pretty much always learning my best :v:

I disagree, a full gamemode is way too intimidating for a beginner, something as simple as figuring out which file does what would be already severely discouraging. Even looking at weapons wouldn’t be a good idea, since most weapons shoot bullets, they usually derive from a weapon base which takes care of all the bullet shooting part, and this is greatly confusing.

If anything, scripted entities would be more interesting for a beginner to study. Garry’s bouncy ball is a fairly decent starting point because it demonstrates spawnmenu integration, editable properties, custom physics and custom rendering. Unlike with weapons, you don’t need to bother with prediction so there is also a clear separation between the client and the server, which makes it easier to understand which side (or realm, if you want to insist) does what.

Also the only thing GLua adds to Lua is a bunch of syntactic sugar for those who are unable to adapt to a language that is different from C. !=, !, &&, ||, as well as // and /* */ for comments. Things that do not bring anything to the language other than confusion and bad habits. Even the “continue” keyword isn’t necessary, although I have to admit it can be handy sometimes.
“Integration within a popular game” doesn’t change the language either. Doesn’t matter if it’s Lua within GMod or Lua within any other game, it’s still Lua.

That’s what I said… A full game-mode is usually too much for a beginner; that’s why I said edit small portions and listed some good places to start.

I mention basically editing variable values ( realistic fire-rate is changing the delay. change damage. Add math to an existing cone function to change based on speed, etc etc. ); I say values so they can see what does what when and how. Ok so changing this does that, that’s cool; let’s try this… etc.

I think we’re agreeing with each other on that.

Entities are also a great place to start, +1. Some are really complicated, others are fairly simple.

I agree with the complexity of realms which is one of the reasons I share this when people ask: https://dl.dropboxusercontent.com/u/26074909/tutoring/loading_files_across_realms.lua
and why I say start with the easy stuff such as editing existing variables.

I agree with your other points; I do like that it’s got a C/Java twinge to it and continue isn’t necessary all the time, it’s quite nice to have to avoid a pyramid.

[lua]for x do
if ( cond ) then
blah
end
end

// vs

for x do
if ( !cond ) then continue; end

end[/lua]

I did what they both suggested. Edit something first, then build off it to make something of your own.

Grabbed a skeleton swep off the wiki as some chair thrower. Play with it and edit it.
A skeleton gives you a working model of something to build on top of. It’s not that you need any of the code, but the structure and functions that are common.

If you don’t get example code or a skeleton to work off of you are going to be doing lots of reading or generating lots of errors with no place to start. Most likely you will get bored and give up.

If you only do simple edits you will be dependent on everyone else or googling for code scraps you can paste together because you won’t be able to create your own logic. You won’t ever understand what is going on.

If you learn basic lua syntax, try to read and understand what is going on with the example code, and play with it you are going to learn. Then you should be getting ideas and just hammer through wiki functions and hooks, puzzle together some logic, and fire it up. fix all your type-o’s missed closing parenthesizes and end tags.

Most people get stuck on most trivial errors, posting whole blocks of code for just a missing tag because of a badly formatted inline function. They try to compact everything then when the line hits an error inside an inline your 100 line function is technically interpreted as 1 line.


152. hook.Add("whateverStuff", "myFunnyName", function()
     local savedBytesForInlineFunction = 2
     local confusingFactor = 10
     for k, v in pairs(player.GetAll)
         v:Kick("Absolutely None")
     end
end)

Error on line 152. bla bla error near for loop something.



152. function sensableFunctionNameLikeKickAllPlayers() --Named function gives us some idea of what this thing actually does.
153.     local savedBytesForInlineFunction = -1
154.     local confusingFactor = 0
155.     for i, ply in pairs(player.GetAll) --Introducing actual variable names that mean something. ply means a player to me.
156.         ply:Kick("Absolutely None")
157.     end
158. end
159. hook.Add("whateverStuff", "myFunnyName", sensableFunctionName)


Error on line 155. unexpected end tag blah blah. (we missed a do tag, and ended before the do)

That way you can actually solve problems for yourself and when you get error output you can read it and make sense of it. Line numbers and script names actually mean quite a lot, and basic syntax understanding along with looking for the problem will let you solve it.

Optimization always comes later… Much much later, and is prioritized by what is TOO slow, not what could be theoretically proper or most efficient.
These things are learned mainly through experience once you get something large enough that things go too slow. Welcome to the real world we fix things after they are broken. After you build enough experience you can cut out the process of causing a problem to fix, and just fix it before it happens because you are experienced and know about it.

Line 155 needs to have ( )…

player.GetAll( )

GetAll is a function, it must use parens to contain arguments. There can be 0 to many arguments.

See how ply:Kick( ) contains a string argument?

It references the player, and Kick is attached to the Player metatable. It’s so that player entities, can be kicked easily, the string is the reason for being kicked.

Functions can be stored in tables, variables, etc. Lua is very adaptive. The . delimiter means you’re referencing a string key in a table which can be a function. The : delimiter is a different way to define functions and it allows the use of self within the function. For example, in function GM:WhateverStuff( ), you can use self to reference GM.

The hook, WhateverStuff doesn’t seem to exist. To create the WhateverStuff hook, you’d need to create the GM function WhateverStuff:

[lua]function GM:WhateverStuff( )

end[/lua]

And, somewhere is existing code, you’d have to broadcast it by using hook.Call( “WhateverStuff”, GAMEMODE )

If WhateverStuff took any arguments, you’d comma delimit them after GAMEMODE.

Read to help understand inclusions across realms: https://dl.dropboxusercontent.com/u/26074909/tutoring/loading_files_across_realms.lua

Someone asked for help with one of the first addons people create: First person death camera.
https://dl.dropboxusercontent.com/u/26074909/tutoring/cl_first_person_death_camera.lua

There’s a lot in that file. The first potion from 4 to 16 is the basic code. Use wiki.garrysmod.com to look up the meaning of the functions, and the realm ( located at the bottom of the page ).

The guy then asked how to enable/disable via console commands. So it covers console commands, adding hooks, removing hooks. Managing variables/storing a switch variable.

Then he was confused, so it goes on to comment everything, including simplifying the console command so instead of 2, it’s 1.

It also covers outputting colored messages to console MsgC, outputting colored messages to chat chat.AddText, adding hooks, removing hooks, managing a switch variable, if/else statements, etc.

If it helps you, feel free.

I also give this out: https://dl.dropboxusercontent.com/u/26074909/acecool_ttt_end_round.rar

End round music for TTT. Basically this shows that TTT is using a custom hook, which is broadcasted at the end of the round. It supplies data of the team which won. It gets in to transferring data with the net library and playing sounds on the client, from the received data.

Try experimenting. Create a copy of the ENTITY health station in TTT, and the WEAPON health station in TTT. Change the math so that it damages you instead of heals you, in addition to changing the language.
This is a simple edit; here’s the solution with a few other things. I think I made it so that traitors heal at normal rates to help fool innocents, etc…
https://dl.dropboxusercontent.com/u/26074909/ttt_acecool_pain_station.rar

Feel free to add me on Steam. I offer more in-depth tutoring to several people on a daily basis.

I don’t think so. I hope this is a joke. Are you really this dense?

I’m not trying to help you; I’m trying to help the OP and I was continuing off of what you told the OP of how you learned. I don’t mind the OP adding me if they’re really into learning. I appreciate the info you added for the OP, it’s always nice to have friendly people help out people willing to learn by sharing their personal experiences.

I think JayneHJKL is more concerned that you’re teaching new potential developers Lua when you’ve proven on numerous occasions the bad coding practices you have.

If you are going to learn by modifying then I suggest not using one of the worst coded gamemodes.

I’ve found coding your own simple weapons (mostly based off the wiki skeletons to begin with) gives you a good place to start with Lua. You can learn the basic syntax of the language, and start interacting with the various libraries it has as you expand the weapon and add features to it.

Start off with something that makes a noise when you fire it, then add shooting of some description, then reloading, secondary fire, etc. until you have something you can describe as a weapon (no matter how senseless it might be). Then think about how you can expand it, add more features to the various functions it has (make it colour players one colour, props another, etc.).

This is a much more practical exercise than working with existing code which is like to either teach you bad habits or be way above your level of understanding. It may not result in something worth releasing to the public, but it gives you something to play around with and try new things with as you come across them.

I can’t say I am a coder because all I can do are client-side stuff. Right now It’s really hard for me to understand how to make server-side things but I don’t know if it’s because I started with client-side first or just because I am dumb.
I just can’t understand how you coders know how to make custom function. I am pretty sure that’s every new coder question.
How do you know what to write in there?
Maybe someone will be able to explain this to me one day.

[lua]local function hiboowman()
print(“hi boowman”)
end

hiboowman()[/lua]

practice practice practice.

To be honest I think the only reason why nobody was able to explain it to me was because I don’t know what to ask.
I know how to make some sort of custom functions. I used custom functions in my scrips but I don’t know how to make more complicated functions.

I think nobody answered my question yet because I don’t even know what I want to know.