Walid

[TUT] Lua Tips & Tricks

Recommended Posts

Logo_Makr.png

Hello guys, I am creating this thread so that members of the community can share useful tips and tricks they have learned during their time of scripting. I don't know if it's already created before or not but i think it's something useful to help beginners to learn Lua language plus i think Tutorials board is the best place.

to begin with, here a few tricks I have picked up:

You can do this:

local variable = "no" 
if Condition then variable = "yes" end 
 

But it's faster if you do it like this:

local variable = (Condition and "yes") or "no" 
 

Reversing bools:

bool = true 
-- if you want to reverse it you can either do 
bool = false 
-- or 
bool = not bool 
 

When assigning a variable you don't want to be nil you could do this:

local variable = otherVariable 
if variable == nil then variable = 10 end 
 

But it can be done a lot faster:

local variable = otherVariable or 10 
 

Commenting multiple lines :

-- line1 
-- line2 
-- line3 
 

You can just use this:

 --[[line1 
line2  
lin3]] 
 

There are a lot more cool things you can do with lua, if you have something you wish to share feel free to post below.

  • Like 1

Share this post


Link to post

About this trick is named "Ternary Operator".

local variable = (Condition and "yes") or "no" 

It works like that, if the condition is true then it will trigger the string "yes" otherwise will trigger "no".

Share this post


Link to post
About this trick is named "Ternary Operator".
local variable = (Condition and "yes") or "no" 

It works like that, if the condition is true then it will trigger the string "yes" otherwise will trigger "no".

Thanks for explaining :x, I always was confused of these tricks in some scripts.

Share this post


Link to post

Sometimes I have problems with booleans. I use this to return an absolute boolean value. I don't know if it really fits in here.

function boolean(var) 
    return not (not var) 
end 
  
boolean("foo") -- true 
boolean(nil) -- false 
boolean(true) -- true 

Share this post


Link to post
Sometimes I have problems with booleans. I use this to return an absolute boolean value. I don't know if it really fits in here.
function boolean(var) 
    return not (not var) 
end 
  
boolean("foo") -- true 
boolean(nil) -- false 
boolean(true) -- true 

I want to explain something all Lua values when used as Booleans evaluate to true, except nil and false. This does not mean that values that evaluate to true are equal to true. If you want to convert a "var" to Boolean, use not not "var".

(not nil)   --> true 
(not false)    --> true 
(not 0)        --> false 
(not not nil)  --> false 

Share this post


Link to post

I wrote this recently to concatenate the keys and values of a table to display them in a list fashion:

for k,v in pairs(data) do 
    Key = (Key or "") .. k..":\n" 
    Value = (Value or "") .. v.."\n" 
end 
  
dxDrawText(Key..... 
dxDrawText(Value..... 

Think this will be useful for some people if they have named keys in their tables and they want to display the content.

Just using table.concat() wouldn't be possible in this situation.

The trick is in the way I'm defining and concatenate the strings. You can use it for anything.

Share this post


Link to post
I wrote this recently to concatenate the keys and values of a table to display them in a list fashion:
for k,v in pairs(data) do 
    Key = (Key or "") .. k..":\n" 
    Value = (Value or "") .. v.."\n" 
end 
  
dxDrawText(Key..... 
dxDrawText(Value..... 

Think this will be useful for some people if they have named keys in their tables and they want to display the content.

Just using table.concat() wouldn't be possible in this situation.

The trick is in the way I'm defining and concatenate the strings. You can use it for anything.

Check this one to convert a table into string you can use it to output your table values into log messages.

-- convert value to string 
function table.valueToString (value) 
    if type(value) == "string" then 
        value = string.gsub( value, "\n", "\\n" ) 
            if string.match( string.gsub(value,"[^'\"]",""), '^"+$' ) then 
                return "'" ..value.. "'" 
            end 
        return '"' .. string.gsub(value,'"', '\\"' ) .. '"' 
    else 
        return type(value) == "table" and table.tostring(value) or tostring(value) 
    end 
end 
  
-- convert key to string 
function table.keyToString (key) 
    if type(key) == "string" and string.match(key, "^[_%a][_%a%d]*$" ) then 
        return key 
    else 
        return "[" ..table.valueToString(key).. "]" 
    end 
end 
  
-- convert table to string 
function table.tostring(tbl) 
    local result, done = {}, {} 
        for k, v in ipairs(tbl) do 
            table.insert( result,table.valueToString( v ) ) 
            done[ k ] = true 
        end 
         
        for k, v in pairs( tbl ) do 
            if not done[ k ] then 
            table.insert( result,table.keyToString(k) .. "=" ..table.valueToString( v )) 
        end 
    end 
    return "{" .. table.concat( result, "," ) .. "}" 
end 
  

Share this post


Link to post

Yeah that's neat. I feel like you could write that with much fewer code than that though.

The code/func I'm using is perfect for what it needs to do, the point was the concatenation I did on a single line.

Same idea as " local variable = (Condition and "yes") or "no" " with the 'or' and stuff.

It's only a few lines of code, and you don't have to seperate the keys and values into tables to do table.concat afterwards, even though you could. I wrote something like it already before to output a table to a text file.

Share this post


Link to post

I want to share with you a simple usage of the tonumber lua function, everyone knows that this tonumber function convert a number with the string form to an actual number type,

But what might you don't know is, it can also convert the letters with string form to actual number.

tonumber function has an optional argument named 'base', The base can be any number between ( 2 - 36 ).

if you put a string with the lettre B ( upper or lower case ) it will return the number 11.

Examples:

tonumber ( "A", 10 ) or tonumber ( "A", 11 ) -- 10 
tonumber ( "B", 12 ) -- 11 
tonumber ( "C", 13 ) -- 12 
tonumber ( "D", 14 ) -- 13 
tonumber ( "E", 15 ) -- 14 
tonumber ( "F", 16 ) -- 15   
....etc.... 
tonumber ( "z", 36 ) -- 35  

- The lettre A outputs the number: 10

- The letter B outputs the number: 11

- The letter C outputs the number: 12

- The letter D outputs the number: 13

.......... Etc ...........

Untill the letter Z, it outputs the number: 35.

Edited by Guest

Share this post


Link to post
I can't replicate that, KariiiM.

My post is edited, forgot to complet the explanation, thank you for reminding me

Share this post


Link to post

Here is an other cool trick.

Using backslashes (\) before single quotes inside single quotes: (to prevent errors when using words with apostrophes).

-- Example:

local variable = 'don\'t'

outputChatBox(variable) -- Result : don't

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.