• Learning GLua
    23 replies, posted
So ive been trying to learn lua for a long time now. I still have no idea what a table is, a hook, how a function works and all that other jazz. Somehow i managed to become a dev for a server and now im trying to develop a gamemode in NutScript. I have realized that im in way over my head. What i request is any good lua tutorials that i can learn from as i really want to learn lua and get into coding as so far with the little things ive been making ive been enjoying it. (mostly sweps with code scavanged from someone else's code.) Thank you for any help. --EDIT Im such a pleb. Didn't realize the post by garry as a sticky thread cause it was greyed out
How I learned is I started out small making very basic things by watching tutorials on YouTube. From there I progressed and starting doing harder things. I still only have a basic understanding of Lua, but more than most people.
Edit garrysmod.org scripts, try to read them, understand those, modify as you want, and then try to create yours
[QUOTE=Bakken1;50645058]So ive been trying to learn lua for a long time now. I still have no idea what a table is, a hook, how a function works and all that other jazz. Somehow i managed to become a dev for a server and now im trying to develop a gamemode in NutScript. I have realized that im in way over my head. What i request is any good lua tutorials that i can learn from as i really want to learn lua and get into coding as so far with the little things ive been making ive been enjoying it. (mostly sweps with code scavanged from someone else's code.) Thank you for any help. --EDIT Im such a pleb. Didn't realize the post by garry as a sticky thread cause it was greyed out[/QUOTE] Remember the wiki is your best friend! I never close the wiki tab because i use it so often.
Thanks for all the help so far, this will certainly help me advance my knowledge. Thank you guys so much [editline]4th July 2016[/editline] Ok so from what i understand strings are defined by "" and [[]]. They are used when a function is called Example (No clue if this will be right xD): [CODE]function getName( "Bob" ) end[/CODE] Example 2: [CODE]function getDescription( [[Bob is a blonde, fat guy roaming the streets of bobly city at night. No one knows why, he just does it. bla bla next line One morning he decided he had enough of going out at night and now he's ended up here]] end[/CODE] So basicly what ive gathered is that "" are used for short strings. While [[]] Are used for long strings. Then we have variables, which i really have no real clue on how work. but i'll make a poor example that might not even work anyway. Example: [CODE]--[[var]] name = "Bob" --[[var]] name = "Charlie" function getName( name ) end [/CODE] I have a question with variables. Could one put them like this? [CODE]function getName( name ) --[[var]] name = "Bob" --[[var]] name = "Charlie" end [/CODE] I understand if some think im stupid but i guess everyone was stupid when they first started learning right? ;) anyway thanks for any comment on this. Also if someone could explain to me how the creation of functions and how they really work that would be awesome :) I'll get into tables later as i believe variables have a great deal to do with tables so i guess learning vars first is the best choice -- EDIT Found out that vars and strings can be joined by concatenation Example: [CODE] --[[var]] name = "Bob" function getDescription( name .. "is a blonde, fat guy." ) end --[[var]] name = "Charlie" function getDescription( name .. "is a brown haired, fit girl.") end[/CODE] Please tell me if i did anything wrong as if i mess up at the basics then that will ruin me later on probably Also the last thing the wiki teached me was about new lines which seemed very simple [CODE]function getMsg( "am i doing this right?\nYea, i believe so. If not then please tell me :)" ) That should come out like this, right? am i doing this right? Yea, i believe so. If not then please tell me :) [/CODE]
Ok, so i know something is wrong with this because i can feel like something is wrong but it's only a hunch. [CODE] local name = "Bob" --[[So that other vars named name aren't overriden. Very smart. Makes it harder to overwrite other scripts.]] local name = "Charlie" local name = "Odin" local age = 19 local age = 21 local age = 25 function getCharacter( name, age ) if name == "Bob" then return age == 25 end end [/CODE] Would that be correct? i feel like there is something wrong with age == 25, maybe i need to make vars like age1 age2 age3 instead? Thanks for any help :)
I'd suggest you reading [URL="www.lua.org/pil/contents.html"]this[/URL], it shouldn't take more than a day or 2 to read depending on how much time you have and it'll tell you "everything" you need to know. After reading that you'll have the basic understanding of Lua and the language itself, and from then on it's a lot easier to learn how things in GLua work.
[QUOTE=bigdogmat;50646347]I'd suggest you reading [URL="www.lua.org/pil/contents.html"]this[/URL], it shouldn't take more than a day or 2 to read depending on how much time you have and it'll tell you "everything" you need to know. After reading that you'll have the basic understanding of Lua and the language itself, and from then on it's a lot easier to learn how things in GLua work.[/QUOTE] Thanks, I'll read up on this whenever i have the time, thanks alot
[QUOTE=Bakken1;50646255][CODE] local name = "Bob" --[[So that other vars named name aren't overriden. Very smart. Makes it harder to overwrite other scripts.]] local name = "Charlie" local name = "Odin" local age = 19 local age = 21 local age = 25 function getCharacter( name, age ) if name == "Bob" then return age == 25 end end [/CODE][/QUOTE] Uh... with that code what you're doing is setting a name variable to be Bob, then to be Charlie, then to be Odin, and then you set the Age variable to be 19, then 21 then 25. Then you created the function GLOBALLY instead of locally and you didn't even use any of the local variables you defined above. Sorry if you got confused about how functions work, it took me a while to understand them. I just mean that the name and age variables can only be accessed by that getCharacter function you made when it gets run, no other script. You don't need to define them above.
[QUOTE=MPan1;50646751]Uh... with that code what you're doing is setting a name variable to be Bob, then to be Charlie, then to be Odin, and then you set the Age variable to be 19, then 21 then 25. Then you created the function GLOBALLY instead of locally and you didn't even use any of the local variables you defined above. Sorry if you got confused about how functions work, it took me a while to understand them. I just mean that the name and age variables can only be accessed by that getCharacter function you made when it gets run, no other script. You don't need to define them above.[/QUOTE] I figured out that this [CODE] local name = "Bob" local name = "Charlie" local name = "Odin" local age = 19 local age = 21 local age = 25 function getCharacter( name, age ) if name == "Bob" then return age == 25 end end [/CODE] Doesn't work as they just keep overriding each other. so what you mean is that i should use local functions instead of global ones? anyway what i came up with is this, the vars no longer override eachother and i made some vars within the function and tried to use print to check if it had worked but i found out that i can't use print within a function. so basicly my function doesn't do shit atm. Anyway whats the difference between a local and a global function? Here is the code i wrote if you want to look at how bad i am at coding xD [CODE] local name = "Bob" local age = 19 local function getCharacter( name, age ) if name == "Charlie" --Variables created within a function stays within the function it seems. then if age == 25 then print( name, age ) --print doesn't work inside function so im guessing functions doesn't work inside functions which would make sense. end end end print( age, name ) 19 Bob[/CODE]
You can run functions in functions just fine... and the difference between a local and a global function is the same as it is with local and global variables, since a function IS a type of variable. Also, I meant that you should make your function local since you seem to be worried about using local variables. [editline]5th July 2016[/editline] Also, the code you posted before doesn't work because you never RAN the function. I'll try to explain how functions work a bit better since they used to confuse me as well: [CODE] local function SomeFunction( var1, var2 ) -- This creates a function that has two arguments- var1 and var2. These arguments DO NOT need to be set as anything beforehand, as they only exist within the function when it is run print(var1, var2) -- This will print both the arguments that are entered end SomeFunction( 1, 2 ) -- This RUNS the function called SomeFunction, and this causes the numbers 1 and 2 to be printed. [/CODE] Maybe that might make more sense to you, I don't know. [editline]5th July 2016[/editline] Also, I'll try to remake your function in the way I would do it if it helps: [CODE] local function getCharacter( name, age ) -- using these arguments makes the first thing you give this function get set as the 'name' and the second as the 'age', if that makes sense if name == "Charlie" and age == 25 then -- remember that these variables only exist in this function and DO NOT need to be defined before print( name, age ) end end getCharacter( 'Charlie', 25 ) -- This runs the function, and should print 'Charlie' and 25 [/CODE]
Try running these scripts in GMod after you made them: it'll help you figure out your errors, and how different functions behave.
[QUOTE=MPan1;50649251]You can run functions in functions just fine... and the difference between a local and a global function is the same as it is with local and global variables, since a function IS a type of variable. Also, I meant that you should make your function local since you seem to be worried about using local variables. [editline]5th July 2016[/editline] Also, the code you posted before doesn't work because you never RAN the function. I'll try to explain how functions work a bit better since they used to confuse me as well: [CODE] local function SomeFunction( var1, var2 ) -- This creates a function that has two arguments- var1 and var2. These arguments DO NOT need to be set as anything beforehand, as they only exist within the function when it is run print(var1, var2) -- This will print both the arguments that are entered end SomeFunction( 1, 2 ) -- This RUNS the function called SomeFunction, and this causes the numbers 1 and 2 to be printed. [/CODE] Maybe that might make more sense to you, I don't know. [editline]5th July 2016[/editline] Also, I'll try to remake your function in the way I would do it if it helps: [CODE] local function getCharacter( name, age ) -- using these arguments makes the first thing you give this function get set as the 'name' and the second as the 'age', if that makes sense if name == "Charlie" and age == 25 then -- remember that these variables only exist in this function and DO NOT need to be defined before print( name, age ) end end getCharacter( 'Charlie', 25 ) -- This runs the function, and should print 'Charlie' and 25 [/CODE][/QUOTE] Ok so what i have created is this, basicly what you told me [CODE]local function getCharacter( name, age, desc ) if name == "Bob" and age == 23 and desc == "Random blonde guy" then print( name, age, desc ) end end getCharacter( 'Bob', 23, 'Random blonde guy' ) [/CODE] So my question is now, is there a reason to why i need to put 'Bob' there or is this because the var name needs to be "Bob" as we stated in the function? Now my second question is variables that are created outside of the function as a local variable will work anywhere in the script right? So if i did this [CODE]local name = "Charlie" local function getCharacter( name, age, desc ) if name == "Bob" and age == 23 and desc == "Random blonde guy" then print( name, age, desc ) end end getCharacter( 'Bob', 23, 'Random blonde guy' ) print( name )[/CODE] then that should print the function and the variable i defined above the function right?
[QUOTE=Bakken1;50649348]:snip:[/QUOTE] Well, I think it should. You can always test it though. Also, what did you mean about the 'Bob' thing? Do you mean the 'Bob' thing from the 'if' statement, or the 'Bob' thing from the bit where you run the function?
[QUOTE=MPan1;50649408]Well, I think it should. You can always test it though. Also, what did you mean about the 'Bob' thing? Do you mean the 'Bob' thing from the 'if' statement, or the 'Bob' thing from the bit where you run the function?[/QUOTE] I meant the one the bob specified where i run it. Also the code worked ☺
[QUOTE=Bakken1;50649456]I meant the one the bob specified where i run it. Also the code worked ☺[/QUOTE] Well, you don't have to put 'Bob' there, you can put anything there really, but since you're checking whether the name is 'Bob' in the 'if' statement, it wouldn't work with other things there
[QUOTE=MPan1;50649539]Well, you don't have to put 'Bob' there, you can put anything there really, but since you're checking whether the name is 'Bob' in the 'if' statement, it wouldn't work with other things there[/QUOTE] Thats what i thought, would you be able to put the var name there or would you have to define it first? As from my understanding we aren't actually creating a var within the function we are only saying that if the var name is equal to bob and age is equal to 23 and desc is equal to the desc then print the vars. If the var name is Charlie then print would never go trough right?
Ok so i think i've managed to learn myself the basics of tables. Does this seem correct? [CODE] -- Attempting to make a table local name = "Bob" local name1 = "Charlie" local name2 = "Odin" local name3 = "Ryan" print( name ) local names = {} names[name] = name .. " is fat" names[name1] = name1 .. " is fit" names[name2] = name2 .. " is barbaric" names[name3] = name3 .. " is a mechanic" print( names[name3] ) local age = {" and the age of 20", " and the age of 21", " and the age of 24", " and the age of 26"} print( names[name2] .. age[1] )[/CODE] In my mind this should output something like this: Bob Ryan is a mechanic Odin is barbaric and the age of 20 Please tell me if i did something wrong or if you have any hints, this was basicly what i got off of tables from that book mentioned earlier which has been really helpful to understand the language so far. Thanks everyone for the contribution of your time to help me :)
Ok so i was wondering, now that i have a basic knowledge of lua, where should i start off learning to use that knowledge for scripting in gmod? Lets say i wanted to make a system where whenever you spawned the script would try set your model skin to 1 (the first available non default) and if there were none then just stick to default? Is this to complex to be something to start with? Ive made sweps before by changing values and such but ive never actually made anything from scratch so maybe learning to how to make a very simple base or something? anyway as always thanks for any advice/help/contribution to me learning more. Also if there is anyone here that has a special interest in NutScript or who just knows a bit of it i would greatly appreaciate getting some tips there aswell as that seems even harder than just normal Glua even though most things that you're gonna use are predefined. Anyway thanks for reading and i hope you have a good day and sorry if you feel like im a total waste of your time
[QUOTE=Bakken1;50652379]Lets say i wanted to make a system where whenever you spawned the script would try set your model skin to 1 (the first available non default) and if there were none then just stick to default? Is this to complex to be something to start with?[/QUOTE] Well, that doesn't really have anything to do with what you've been doing previously, but these should give you an idea of what to do: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerSpawn]GM:PlayerSpawn[/url], [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/SkinCount]Entity:SkinCount[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/SetSkin]Entity:SetSkin[/url]
So something like this? [CODE] function GM:PlayerSpawn( ply ) function setSkin( ply, model, skin ) model = ply:GetModel() skin = if ply:SkinCount() > 0 then ply:SetSkin( 1 1 ) then print( "You succesfully changed your skin" ) end end end[/CODE] Or is this completely wrong? xD Also do i need to specify ply and in that case how do i sepcify it so it tells gmod that im after the player running the function?
[code]hook.Add( "PlayerSpawn", "SetPlayerSkin", function( ply ) if ( ply:SkinCount() ~= 0 ) then ply:SetSkin( 1 ) end end )[/code] A few things: - Use hooks unless you're making a gamemode - You don't have to declare a function to do an action, you can just run it - if statements are progressions to another code block: you cannot set them to a variable
[QUOTE=Bakken1;50655452]So something like this? [CODE] function GM:PlayerSpawn( ply ) function setSkin( ply, model, skin ) model = ply:GetModel() skin = if ply:SkinCount() > 0 then ply:SetSkin( 1 1 ) then print( "You succesfully changed your skin" ) end end end[/CODE][/QUOTE] That... doesn't make any sense within Lua. I really think you should test your scripts first before posting them and also probably read a couple of the guides people have posted before.
[QUOTE=MPan1;50655470]That... doesn't make any sense within Lua. I really think you should test your scripts first before posting them and also probably read a couple of the guides people have posted before.[/QUOTE] I'll just go back to reading then xD
Sorry, you need to Log In to post a reply to this thread.