• Print

Author Topic: Learning Lua  (Read 7368 times)

0 Members and 1 Guest are viewing this topic.

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Learning Lua
« on: July 19, 2015, 03:06:11 am »
So about 1 month ago, I got into lua coding, and learned myself the basics of it, but there's still enough out there to learn from the lua language, and I just wanted to ask, which is the best way to learn lua, the most easiest, but powerful language?

What I've tryed so far:
Look into people's addons, and see how they have done it, and learn from it.
Wikis.
Asking around, especially on these forums.
Looked at the thread of Megiddo for lua resources to learn from.

But, are there any other ways of learning lua?
I bet there are, but can't really think of one, and I'd love to get a hand of this language, so was wondering if you developers/coders out there mind helping me out a bit?

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Learning Lua
« Reply #1 on: July 19, 2015, 03:12:44 pm »
Wouldn't say Lua is the easiest language to learn. It all depends on the developer though.

Have you read this article about the Lua fundamentals on the Gmod wiki? If you're more of a visual learner, see if you can find some video tutorials on basic Lua.

You can learn a lot about a language just by experimenting with it. There's a bunch of code examples on the Gmod wiki you can play with.

Since you said you already know the basics, I encourage you to write some simple addons using the things you've learned so far. Use the Garry's Mod Wiki as a reference.

Here are some ideas:
- Console command that prints the nicknames of all players
- Kill every player that uses the words "kill", "dead" or "death" in a sentence
- Ulx command that tells you if any of your friends are also playing on that server
- ... (follow your curiosity)

Edit: Looking at your posts, I can see that's what you're already doing. Learning a programming language takes time. Experiment and play with them.
« Last Edit: July 19, 2015, 03:41:16 pm by Timmy »

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Learning Lua
« Reply #2 on: July 27, 2015, 03:13:55 am »
Wouldn't say Lua is the easiest language to learn. It all depends on the developer though.

Have you read this article about the Lua fundamentals on the Gmod wiki? If you're more of a visual learner, see if you can find some video tutorials on basic Lua.

You can learn a lot about a language just by experimenting with it. There's a bunch of code examples on the Gmod wiki you can play with.

Since you said you already know the basics, I encourage you to write some simple addons using the things you've learned so far. Use the Garry's Mod Wiki as a reference.

Here are some ideas:
- Console command that prints the nicknames of all players
- Kill every player that uses the words "kill", "dead" or "death" in a sentence
- Ulx command that tells you if any of your friends are also playing on that server
- ... (follow your curiosity)

Edit: Looking at your posts, I can see that's what you're already doing. Learning a programming language takes time. Experiment and play with them.
Hey! Sorry for the late response, I don't have loads of freetime, but I tryed to make a fix for sprays for users that can't see sprays in gmod (like me).

Code: Lua
  1. function ulx.fixspray( calling_ply )
  2.        
  3.         calling_ply:ConCommand("r_decal_cullsize ","1")
  4.         calling_ply:ConCommand("r_decals ","200")
  5.         calling_ply:ConCommand("r_decalstaticprops ","1")
  6.         calling_ply:ConCommand("r_drawmodeldecals ","1")
  7.         calling_ply:ConCommand("r_drawbatchdecals ","1")
  8.         calling_ply:ConCommand("mp_decals ","200")
  9.         calling_ply:ConCommand("cl_playerspraydisable ","0")
  10.         calling_ply:PrintMessage( HUD_PRINTTALK, "You should now be able to see sprays, if not, try and rejoin the server!" )
  11.        
  12. end
  13.  
  14. local fixspray = ulx.command( "Menus", "ulx fixspray", ulx.fixspray, "!fixspray" )
  15. fixspray:defaultAccess( ULib.ACCESS_ALL )
  16. fixspray:help( "For if your spray does not work, or you can't see any sprays." )

It fixed it for me, but you need to run some local console command, is there a way to do this, as I keep getting this error when I run my !fixspray command:

FCVAR_SERVER_CAN_EXECUTE prevented server running command: r_decal_cullsize
FCVAR_SERVER_CAN_EXECUTE prevented server running command: r_decals
FCVAR_SERVER_CAN_EXECUTE prevented server running command: r_decalstaticprops
FCVAR_SERVER_CAN_EXECUTE prevented server running command: r_drawmodeldecals
FCVAR_SERVER_CAN_EXECUTE prevented server running command: r_drawbatchdecals
FCVAR_SERVER_CAN_EXECUTE prevented server running command: mp_decals
FCVAR_SERVER_CAN_EXECUTE prevented server running command: cl_playerspraydisable


Can I have some help on how to make player make it run locally only on their client, and not the server itself?

Wouldn't say Lua is the easiest language to learn. It all depends on the developer though.

Have you read this article about the Lua fundamentals on the Gmod wiki? If you're more of a visual learner, see if you can find some video tutorials on basic Lua.

You can learn a lot about a language just by experimenting with it. There's a bunch of code examples on the Gmod wiki you can play with.

Since you said you already know the basics, I encourage you to write some simple addons using the things you've learned so far. Use the Garry's Mod Wiki as a reference.

Here are some ideas:
- Console command that prints the nicknames of all players
- Kill every player that uses the words "kill", "dead" or "death" in a sentence
- Ulx command that tells you if any of your friends are also playing on that server
- ... (follow your curiosity)

Edit: Looking at your posts, I can see that's what you're already doing. Learning a programming language takes time. Experiment and play with them.
The kill one keeps crashing me and others out of the server, and advantually gmod itself.
Here's the code:
Code: Lua
  1. function ulx.killme( ply, calling_ply )
  2.  
  3.         ply:Kill()
  4.         ULib.tsayColor( nil, false, white, "", blue, ply:Nick(), white, " Just really wanted to die!" )
  5.        
  6. end
  7.  
  8. local killme = ulx.command( "Fun", "ulx killme", ulx.killme, "kill me" )
  9. killme:defaultAccess( ULib.ACCESS_SUPERADMIN )
  10.  

I think it's the ULib.tsayColor( nil, false, white, "", blue, ply:Nick(), white, " Just really wanted to die!" ) part that crashes you as I wanted a chat message which everyone can see but instead makes everyone crash. (Did I just make a crash command? Yay xD)

I've also tryed to mess with this, it's supposed to be a coloring text changing it's color all the time.
Code: Lua
  1. ulx_tsay_color_table = { "hue", "black", "white", "red", "blue", "green", "orange", "purple", "pink", "gray", "yellow" }
  2.  
  3. function ulx.tsaycolor( calling_ply, message, color )
  4.  
  5.                 local hue = math.abs(math.sin(CurTime() *0.9) *335)
  6.  
  7.                         calling_ply = Color( hue, 1, 1 )
  8.         if color == "hue" then
  9.        
  10.                 ULib.tsayColor(  nil, false, hue, message )
  11.         if util.tobool( GetConVarNumber( "ulx_logChat" ) ) then
  12.        
  13.                 ulx.logString( string.format( "(tsay from %s) %s", calling_ply:IsValid() and calling_ply:Nick() or "Console", message ) )
  14.                
  15.         end
  16.        
  17. end

It is supposed to be something like Chewgum's atlaschat, where you have <hsv>text</hsv>.

« Last Edit: July 27, 2015, 01:28:21 pm by Janjakob2000 »

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Learning Lua
« Reply #3 on: July 28, 2015, 12:57:30 am »
You'd have been better off creating a new topic for these questions. They are not related to the process of learning a programming language. (:

Can I have some help on how to make player make it run locally only on their client, and not the server itself?
You can tell the server to trigger client-side code using the Net library. There's some examples in the Wiki that should help get you started.

The kill one keeps crashing me and others out of the server, and advantually gmod itself.
Here's the code: [...]
Your function currently takes 2 arguments, ply and calling_ply respectively. ULX automatically passes in the player entity of the caller as the first argument (ply). It has no idea what to do with the second argument (calling_ply) that you specified.

Where do the "white" and "blue" variables come from? Do their values reference colors? I can't see you define those anywhere. Lua has no idea what white is but, it does know what Color(255, 255, 255) is.

Try getting your command working before focusing on fancy animations and other aesthetics.
« Last Edit: July 28, 2015, 12:59:18 am by Timmy »

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Learning Lua
« Reply #4 on: July 28, 2015, 02:25:42 am »
You'd have been better off creating a new topic for these questions. They are not related to the process of learning a programming language. (:
You can tell the server to trigger client-side code using the Net library. There's some examples in the Wiki that should help get you started.
Your function currently takes 2 arguments, ply and calling_ply respectively. ULX automatically passes in the player entity of the caller as the first argument (ply). It has no idea what to do with the second argument (calling_ply) that you specified.

Where do the "white" and "blue" variables come from? Do their values reference colors? I can't see you define those anywhere. Lua has no idea what white is but, it does know what Color(255, 255, 255) is.

Try getting your command working before focusing on fancy animations and other aesthetics.

I don't really feel like creating another thread, it seemed a but "pushy" for me.

The kill command works now, thank you!
Code: Lua
  1. function ulx.killme( calling_ply )
  2.  
  3.         white = Color(255, 255, 255)
  4.         blue = Color(0, 146, 219)
  5.         red = Color(255, 0, 72)
  6.         calling_ply:Kill()
  7.         ULib.tsayColor( nil, false, red, "[SUICIDE] ", blue, calling_ply:Nick(), white, " just really wanted to die!" )
  8.        
  9. end
  10.  
  11. local killme = ulx.command( "Fun", "ulx killme", ulx.killme, "kill me" )
  12. killme:defaultAccess( ULib.ACCESS_SUPERADMIN )

Here's a command I did on my own:
Code: Lua
  1. function ulx.donatorhp( calling_ply )
  2.  
  3.         calling_ply:PS_TakePoints(200)
  4.         calling_ply:PrintMessage( HUD_PRINTTALK, "200 points have been reducted from you in exchange for doubled hp!" )
  5.         calling_ply:SetHealth(calling_ply:Health() * 2)
  6.        
  7. end
  8.  
  9. local donatorhp = ulx.command( "Fun", "ulx donatorhp", ulx.donatorhp, "!donatorhp" )
  10. donatorhp:defaultAccess( ULib.ACCESS_ADMIN )
  11. donatorhp:help( "Doubles your hp, costs 200 points everytime you run this command!" )

So what's my next step to get a bit more advanced in lua? (Or am I going a little bit too far now?)

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: Learning Lua
« Reply #5 on: July 28, 2015, 07:47:43 am »
It is supposed to be something like Chewgum's atlaschat, where you have <hsv>text</hsv>.
I think you're a bit far from making that.  It's much more complicated than throwing colors at the chatbox.

I would next try to make some kind of addon independent of ULX or ULib.  Then after that (or even next) make a simple gamemode on your own.

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Learning Lua
« Reply #6 on: July 28, 2015, 11:51:57 am »
I think you're a bit far from making that.  It's much more complicated than throwing colors at the chatbox.

I would next try to make some kind of addon independent of ULX or ULib.  Then after that (or even next) make a simple gamemode on your own.

So if I were to make an addon with a command that heals , completely independent from ulx/ulib, would it look something like this:

Code: Lua
  1. hook.Add( "PlayerSay", "PlayerSayExample", function( ply, text, team )
  2.         if ( string.sub( text, 1, 5 ) == "/heal" ) then
  3.                 ply:SetHealth(200)
  4.         end
  5. end )

I've looked a bit on the wiki and found this, and made it so it (should) heal you when you say /heal.

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: Learning Lua
« Reply #7 on: July 28, 2015, 02:42:00 pm »
You should try something more complex than something that takes about 5 minutes.  Make yourself think a little bit, that's the only way you will learn and get better.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Learning Lua
« Reply #8 on: July 28, 2015, 07:13:40 pm »
make some kind of addon independent of ULX or ULib

Heresy!
THOU SHALL HAVE NO OTHER LIBRARY BEFORE THY ULIB
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Learning Lua
« Reply #9 on: July 29, 2015, 05:30:39 am »
You should try something more complex than something that takes about 5 minutes.  Make yourself think a little bit, that's the only way you will learn and get better.
Well for now I don't really have any interests for anything else, unless my friends bug me about it on steam, but I tryed this out, restarted my gmod, and it won't work, I have no errors at all, why does it not work then?

Heresy!
THOU SHALL HAVE NO OTHER LIBRARY BEFORE THY ULIB

haha, JamminR, haven't you heard of evolve? Just kidding they're all scrubs anyways :3. (or are they?)

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Learning Lua
« Reply #10 on: July 29, 2015, 12:56:04 pm »
Well for now I don't really have any interests for anything else, unless my friends bug me about it on steam, but I tryed this out, restarted my gmod, and it won't work, I have no errors at all, why does it not work then?

That code will work. Where did you put it?

This would need to be in a serverside autorun location.

garrysmod/lua/autorun/server

If any of those folders don't exist, create them.

If you're doing it inside an addon, similar path.

garrysmod/addons/{youraddon}/lua/autorun/server

Gamemodes are a bit different. If you're doing a gamemode, you'd want to put this code INSIDE of the init.lua file for your gamemode.

Offline Janjakob2000

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
Re: Learning Lua
« Reply #11 on: August 01, 2015, 03:11:38 am »
Thanks MrPresident, you're really helpful, I really appreciate it, I hope someday I can do the same. :D

  • Print