Jump to content

Welcome to Gaming On Steroids Forums
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account
Photo

Lua Tips & Tricks


  • Please log in to reply
13 replies to this topic

#1
jouzuna

jouzuna

    Advanced Member

  • Contributor
  • 73 posts

Hello

 

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

 

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

 

Ternary operation

In C and other languages it is possible to use a ternary operator such as (a == 3 ? b : c) which is essentially a very short one line if statement that says:

if a is equal to three then b otherwise c

you can also do this in Lua as follows

a == 3 and b or c

Multiline string

You can write multiline strings using [[text]] such as

print([[Lorem ipsum dolor sit amet,

consectetur adipiscing elit.]])

Function parenthesis

If you are calling a function which has a single parameter of either a string or a table you can use the following syntax:

print("test") -- Valid
print "test"  -- Also valid

func({ m_test = true }) -- Valid
func { m_test = true }  -- Also valid

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 :)

 


  • 2

#2
Zwei

Zwei

    Advanced Member

  • Contributor
  • 2,201 posts

[[ string ]] is also useful if you want to use \ in your string since it will normally interpreted as a command.

 

Edit: Super thread for making code more compact <3


  • 0

#3
CptZeyx

CptZeyx

    Henry

  • Banned
  • PipPipPip
  • 784 posts
  • LocationBetween here and there

Useful post for those who are starting with coding :D


  • 0

#4
Zwei

Zwei

    Advanced Member

  • Contributor
  • 2,201 posts

Useful post for those who are starting with coding :D

Nah that's more useful for advanced.


  • 0

#5
Platypus

Platypus

    Bitcoin and Paysafecard payments are now available

  • Banned
  • PipPipPip
  • 1,289 posts
function Janna:Advance(___)
do local __ = ___:lower() local function ______(__) if __[1](_) then __[2](___) end end self.____[__] = self.____[__] + 1 if self.____[__] > self._____[__] then self.____[__] = 1 end ______(self._[__][self.____[__]]) end
end
 
exists only to troll handell

  • 1

#6
Deftsu

Deftsu

    donthackourgames

  • Ex-Core Dev
  • PipPipPip
  • 4,812 posts
you can concatenate numbers :
print(3 .. 3) -- 33
2 type of strings:
string1 = 'gg'
string2 = "gg"
if string1 == string2 then print("string1 is equal to string2") end
apostrophes (put \):
string = 'Can\'t Carry this game
print(string)
string and table length:
table = {} 
table2 = {"Hello", "World", "How", "Are", "You"}
print(#table) -- 0
print(#table2) -- 5
string1 = "ggwp"
print(#string1) -- 4
-- Or you can use :
print(string.len(string1))
... is arg:
function stuff(...)  
  for i,v in pairs(arg) do    
    print(v)  
  end
end
stuff(1,2,3,'a','b','c') -- 123abc6, the 6 is table.n
Reversing bools:
bool = true
-- if you want to reverse it you can either do
bool = false
-- or
bool = not bool

  • 2

#7
jouzuna

jouzuna

    Advanced Member

  • Contributor
  • 73 posts

you can concatenate numbers :

print(3 .. 3) -- 33
2 type of strings:
string1 = 'gg'
string2 = "gg"
if string1 == string2 then print("string1 is equal to string2") end
apostrophes (put \):
string = 'Can\'t Carry this game
print(string)
string and table length:
table = {} 
table2 = {"Hello", "World", "How", "Are", "You"}
print(#table) -- 0
print(#table2) -- 5
string1 = "ggwp"
print(#string1) -- 4
-- Or you can use :
print(string.len(string1))
... is arg:
function stuff(...)  
  for i,v in pairs(arg) do    
    print(v)  
  end
end
stuff(1,2,3,'a','b','c') -- 123abc6, the 6 is table.n
Reversing bools:
bool = true
-- if you want to reverse it you can either do
bool = false
-- or
bool = not bool

first one I wasn't aware of :o thanks for sharing


  • 0

#8
Hanndel

Hanndel

    datebest.net - visit website and win smartphone!

  • Contributor
  • 604 posts
  • Locationhttps://t.me/pump_upp

function Janna:Advance(___)
do local __ = ___:lower() local function ______(__) if __[1](_) then __[2](___) end end self.____[__] = self.____[__] + 1 if self.____[__] > self._____[__] then self.____[__] = 1 end ______(self._[__][self.____[__]]) end
end
 
exists only to troll handell


**** u
  • 0

#9
Deftsu

Deftsu

    donthackourgames

  • Ex-Core Dev
  • PipPipPip
  • 4,812 posts
commenting multiple lines :
-- instead
-- of 
-- doing this

you can just :

--[[instead
of 
doing this]]

  • 0

#10
Inspired

Inspired

    Took the red pill.

  • Ex-Core Dev
  • PipPipPip
  • 723 posts
  • LocationWonderland

Ternary operation
In C and other languages it is possible to use a ternary operator such as (a == 3 ? b : c) which is essentially a very short one line if statement that says:
if a is equal to three then b otherwise c
you can also do this in Lua as follows

a == 3 and b or c

Reversing bools:

bool = true
-- if you want to reverse it you can either do
bool = false
-- or
bool = not bool
bool = true
bool = bool and not bool or true
print(bool)
local var1 = math.huge
local var2 = var1 - var1
if var2 == 0 then
    print("amazing")
end
local var1 = math.huge
local var2 = var1 / var1
if var2 < 0 or var2 == 0 or var2 > 0 then
    print("amazing")
end

  • 0

#11
Zwei

Zwei

    Advanced Member

  • Contributor
  • 2,201 posts
bool = true
bool = bool and not bool or true
print(bool)
local var1 = math.huge
local var2 = var1 - var1
if var2 == 0 then
    print("amazing")
end
local var1 = math.huge
local var2 = var1 / var1
if var2 < 0 or var2 == 0 or var2 > 0 then
    print("amazing")
end

 

reminds me of 

https://github.com/D...r.lua#L709-L713


  • 1

#12
Hanndel

Hanndel

    datebest.net - visit website and win smartphone!

  • Contributor
  • 604 posts
  • Locationhttps://t.me/pump_upp

HAHAHAHAHAHHAHAHAHA


  • 0

#13
ilovesona

ilovesona

    Sona's wife

  • Contributor
  • 1,096 posts

remove key from table

tablename.keyname = nil

  • 0

#14
Platypus

Platypus

    Bitcoin and Paysafecard payments are now available

  • Banned
  • PipPipPip
  • 1,289 posts

 

remove key from table

tablename.keyname = nil

 

Instructions unclear, cped deftsu autolvl


  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users