While the wiki is useful when searching for functions, it doesn’t really teach good programming habits, or smart ways to do things.
So, let’s have a thread where we share our tips and tricks!
It doesn’t have to be purely lua code tips, it could also be things like:
** • general programming tips
• useful math equations
• alternate ways to do something
• and anything else you’ve picked up, that you think could be useful to others.**
**Try to provide some sort of code example with your tips (if possible), since that’s what this thread is about! **
P.S: Don’t be afraid to post things that are simple and obvious, because it may not be as obvious to new coders.
Looking forward to seeing what you will share with the community!
I’ll get the thread started with a few tips and tricks I’ve picked up:
[sp](I have experience in other languages, but I’m still fairly new to lua, so correct me if anything’s wrong)[/sp]
1. Toggle a boolean between True and False
myBool = !myBool
**2. Make positive numbers negative, and negative numbers positive **
myNumber = -myNumber
3. Tween any number towards 0, and stop at exactly 0
if myNumber != nil and myNumber != 0 then myNumber = myNumber - math.Clamp( myNumber, -0.5, 0.5 ) end //Change -0.5 and 0.5 with how much you want the number to raise/lower each time the code is being called. //Since this needs to happen over time, you should put it in a Think function (or any other function that happens over time)
4. Getting the angle between 2 vectors:
myAngle = ( targetVector - startVector ):Angle() //This will create an angle aiming in a line from startVector towards targetVector.
5. Smarter validity checks without getting lua errors:
Example: we want to check if our table exists and if it contains a value, [sp](table is just an example, could be something else)[/sp]
so we COULD do something like this:
if !myTable then return false end if !table.HasValue( myTable, "candy" ) then return false end print("Table exists and has candy! Yay!")
Stop! Why use multiple seperate if-statements when you can just use 1?
if !myTable or !table.HasValue( myTable, "candy" ) then return false end print("Table exists and has candy! Yay!")
Q: Wait a minute… Won’t that give us an error if the table doesn’t exist and it tries to check the table…?
A: Nope! Because it will bail out before reading that far into the code!
That’s because the program will try to read as little code as possible, starting from left to right. *(so if you started with the table.HasValue check, you could get an error)
Basically it does this:
“Oh, I need to check if any of boolean1 OR boolean2 are true…**”
Outcome 1: “I see, boolean1 is true, which means I don’t need to read boolean2 to know that any one them are true.”
**Outcome 2: "I see, boolean1 is false, so I must check if boolean2 is true instead"
(a thing to remember is that “not variable” or "**!**variable" will actually return a boolean with the value true when “variable” is false or doesn’t exist. Just though I’d remind in case anyone was confused by the weird talking lua example above)
This isn’t restricted to just if-statements. Another example:
// set mySpeed to either 300 or 150 depending on whether myEntity is running or not mySpeed = ( IsValid(myEntity) and myEntity.isRunning ) and 300 or 150
Which brings me to…
6. Setting a variable depending on whether something is true or false:
myVariable = myBoolean and 5 or 0 // myVariable will become 5 if boolean is true, and it will become 0 if boolean is false
another example on the same thing:
myString = (powerLevel > 9000) and "It's over 9000!!!!" or "it's not over 9000.." print(myString)
More code examples in this thread:
7. Convert velocity to KM/H and MPH
Scroll down the thread for even more tips and code examples