# [TIPS & TRICKS] LUA

## Recommended Posts

[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}

```

```
someFunc("Cassandra", nil, 0.5)

```

For Loop Optimization

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.

• 1

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

Good job .

• Other Languages Moderators

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

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.

• Other Languages Moderators

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

Edited by Guest
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".

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

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
```

For Loop Optimization

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...

"Singular check" could also be a ternary operator.

The principle is the same!