Jump to content

[TIPS & TRICKS] LUA


Recommended Posts

Posted

[TIPS & TRICKS] LUA

Prolusion

I want to depict few tips and tricks on LUA which new scripters may/may not know.

Assigning a value which is not nil

I saw most scripters does that using the following code:

  
local someVar = anotherVar 
if someVar == nil then someVar = 10 end 
  

Which can be shortened to this:

  
local someVar = anotherVar or 10  
  

This is what LUA is doing. It takes the anotherVar, and if it's nil or false it will use the value which is after the or statement.

You can also do it more than once:

  
local someVar = anotherVar or otherAnotherVar or justSomeVar or 10 
  

The same thing can be done inside a function as well.

Singular Check

This is a nice trick which allows you to make singular checks more efficient than before.

The following code:

  
local someCondition = "Yes" 
local someVar = 5 
  
if someCondition == "Yes" then someVar = 10 
else someVar = 0 
end 
  

Which can be shortened to this:

  
local someVar = (someCondition == "Yes") and 10 or 5 
  

This is how LUA understand. If the condition is true(which is someCondition on the example) it will assign the value of someVar to whatever the value is after the and operator and if it's false or nil the value after or operator will be assigned.

Ignore Parenthesis

Yet another simple trick.

The following code:

  
function doSomething(doWhat) 
if type(doWhat) == "string" then print(doWhat) end 
end 
  
doSomething("Hey now brown cow.") 
  

Which can also written like this:

  
function doSomething(doWhat) 
if type(doWhat) == "string" then print(doWhat) end 
end 
  
doSomething"Hey now brown cow." 
  

When the first and only argument to a function is a string or a table you can ignore the parenthesis.

Named Arguments

Named arguments is a system where a function takes a table of named arguments instead of individual arguments. This allows the function to be called with missing arguments. That's how I define it

Use the following code:

  
local function someThing(player) 
    print(player.Name) 
    print(player.Exp) 
    print(player.Health) 
end 
  
someThing{Name = "Cassandra", Age = 199, Health = 0.5} 
  

Then you can also ignore argument:

  
someThing{Name = "Cassandra", Health = 0.5} 
  

Instead of:

  
someFunc("Cassandra", nil, 0.5) 
  

For Loop Optimization

Access to external locals (that is, variables that are local to an enclosing function) is not as fast as access to local variables, but it is still faster than access to globals.

The following code:

  
for i = 1, 1000000 do 
x = x + math.sqrt(i) 
end 
  

Can be optimized to:

  
local squareRoot = math.sqrt 
for i = 1, 1000000 do 
x = x + squareRoot (i) 
end 
  

Object Behavior from OOP

You can use object behavior from Object Oriented Programming inside LUA too using tables.

Consider using this:

  
AnObject = {}; 
AnObject.AFunction = function () 
print"I am an object yeppie!" 
end 
  
  
AnObject.AFunction();  
--Which call the AFunction inside AnObject class. 
  

Whenever I discover something new, I will attempt to share it here.

  • Thanks 1

Regards,

Cassandra - V:MP

Posted

Very nice tutorial, it'll help these who don't know this.

Good job :).

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

Object Behavior from OOP

You can use object behavior from Object Oriented Programming inside LUA too using tables.

Consider using this:

  
AnObject = {}; 
AnObject.AFunction = function () 
print"I am an object yeppie!" 
end 
  
  
AnObject.AFunction();  
--Which call the AFunction inside AnObject class. 
  

Whenever I discover something new, I will attempt to share it here.

It's not oop. Learn http://lua-users.org/wiki/ObjectOrientedProgramming

http://vk.com/the_kenix

Вопросы задавайте на форуме, не пишите мне в личку.

Please don't pm me.

Posted

Object Behavior from OOP

You can use object behavior from Object Oriented Programming inside LUA too using tables.

Consider using this:

  
AnObject = {}; 
AnObject.AFunction = function () 
print"I am an object yeppie!" 
end 
  
  
AnObject.AFunction();  
--Which call the AFunction inside AnObject class. 
  

Whenever I discover something new, I will attempt to share it here.

It's not oop. Learn http://lua-users.org/wiki/ObjectOrientedProgramming

It's part of OOP.

Regards,

Cassandra - V:MP

Posted (edited)

Where is self? Where are the objects? Where is methods? Where is exemplar class?

Edited by Guest

http://vk.com/the_kenix

Вопросы задавайте на форуме, не пишите мне в личку.

Please don't pm me.

Posted
Where self? Where objects? Where exemplar class?

As I said earlier it's not OOP but a part of it, thus the heading says "Object Behavior".

Regards,

Cassandra - V:MP

Posted (edited)

This isn't even a part of OOP.

Where did you create a method? Where are objects?

You just created a table and created a function inside it.

Also, the title says "Object Behavior from OOP" not "Object Behavior".

Edited by Guest

"[...] If you don’t love it, if you’re not having fun doing it, you don’t really love it, you’re going to give up." - Steve Jobs, 2007

Posted

May I append that, instead of;

if var == true then 

and

if var ~= false then 

and

if var == this or var == that then 

you can use

if var then 

Retired

  • MTA Team
Posted

For Loop Optimization

Access to external locals (that is, variables that are local to an enclosing function) is not as fast as access to local variables, but it is still faster than access to globals.

The following code:

  
for i = 1, 1000000 do 
x = x + math.sqrt(i) 
end 
  

Can be optimized to:

  
local math.sqrt = math.sqrt 
for i = 1, 1000000 do 
x = x + math.sqrt(i) 
end 
  

Object Behavior from OOP

You can use object behavior from Object Oriented Programming inside LUA too using tables.

Consider using this:

  
AnObject = {}; 
AnObject.AFunction = function () 
print"I am an object yeppie!" 
end 
  
  
AnObject.AFunction();  
--Which call the AFunction inside AnObject class. 
  

WTF? This isn't class, this is just a function inside a table! You cant do AnObject:AFunction(...) can you? That is what OOP is!

Sorry but this tutorial is innacurate

  • 9 months later...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...