Found 82 Articles for Lua

Sort function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 12:09:16

8K+ Views

One of the most used functions in Lua is the sort function which is provided by the Lua library which tables a table as an argument and sorts the values that are present inside the table.The sort function also takes one more argument with the table and that argument is a function which is known as the order function. This order function is used to provide the logic if we want to sort the elements of the table in a certain order.The order function takes two arguments, and these two arguments must return true if the first argument should come ... Read More

Semicolon Conventions in Lua Programming

Mukul Latiyan
Updated on 19-Jul-2021 12:07:32

402 Views

There are very few sections we will explore the possibility of making use of a semicolon in Lua. Majority of the code doesn’t require it, while there are some cases where we might need them.ExampleLet’s consider a case where the use of semicolon seems necessary. Consider the example shown below − Live Demolocal a, b=10, 20 print(a+b)The above statement in Lua is perfectly valid and it works like a charm.Output30ExampleWhat if we change the code above and put it in the same line? Would it still be a valid Lua code? Let’s try it out. Consider the code shown below −local ... Read More

select() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 12:06:04

6K+ Views

The select function in Lua is used to return the number of arguments that are passed into it as an argument. It can be used in two forms, the first one includes passing an index and then it will return the numbers that are passed after that number into the function as an argument in a list format, the other pattern is if we pass the length operator as a first argument, in that case it simply returns a count of the multiple arguments that are provided.ExampleLet’s explore both the cases in the examples shown below.Case 1 Live Demoprint(select(1, "a", "b", ... Read More

Read-only tables in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 12:04:49

510 Views

While working with tables, we can easily access and modify the values present in the tables provided we know the key. But there are cases where we would like our tables to be in read-only format so that the values present inside the table cannot be modified.There are many benefits for this particular approach as we can make use of such read only tables in storing information that we don’t want anyone to mutate, like storing data of employees.In order to make any table a read only table we make use of the setmetatable() function along with __index and __newindex ... Read More

Passing Lua script from C++ to Lua

Mukul Latiyan
Updated on 19-Jul-2021 12:01:48

463 Views

The idea of passing Lua script from C++ to Lua includes the fact that we will have to load the libraries and header files as Lua is ANSI C, and if we are coding in C++, we will need to enclose the #includes in extern “C”.The old and mostly used approach would be to load the libraries that Lua provides and then simply call the C++ function from Lua.In order to load the script from C++ to Lua, we need to set up and shutdown the Lua interpreter and we can do that with the help of the following code.ExampleConsider ... Read More

math.modf() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 11:58:30

2K+ Views

There are several occurrences when we want to get the integer value of a number and also the fractional value that the number has if any, so that we can use either or both of these values.Lua provides us with a math.modf() function that we can use to find the integer value along with the fractional value if the number has any.Syntaxmath.modf(number)The math.modf() function returns two values when we call the function, the first value is the integer value of the number and the second returned value is the fractional value of the number if it has any.ExampleLet’s consider a ... Read More

math.min() function in Lua

Mukul Latiyan
Updated on 19-Jul-2021 11:56:18

6K+ Views

There are several occurrences when we want to get the min value from a given series of numbers and then use that value later on.The Minimum value from the series of different numbers is the value that is the minimum of all the numbers that are present in that series.Lua provides us with a math.min() function that we can use to find the min value out of different numbers that we pass to it as an argument.ExampleLet’s consider a simple example where we will make use of the math.min() function in Lua − Live Demoa = 10 b = 11 c ... Read More

math.max() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 11:53:24

8K+ Views

There are several occurrences when we want to get the max value from a given series of numbers and then use that value later on.The Maximum value from the series of different numbers is the value that is the maximum of all the numbers that are present in that series.Lua provides us with a math.max() function that we can use to find the max value out of different numbers that we pass to it as arguments.ExampleLet’s consider a simple example where we will make use of the math.max() function in Lua − Live Demoa = 10 b = 11 c = ... Read More

math.floor() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 11:47:04

10K+ Views

There are several occurrences when we want to get the floor value of an integer to round it off and then use that value later on. The floor value of a number is the value that is rounded to the closest integer less than or equal to that integer. Lua provides us with a math.floor() function that we can use to find the floor value of a number.ExampleLet’s consider a simple example where we will make use of the math.floor() function in Lua − Live Demoa = math.floor(3.3) b = math.floor(7.1) print(a) print(b)Output3 7It should be noted that if we try ... Read More

math.ceil() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 11:45:58

5K+ Views

There are several occurrences when we want to get the ceil value of an integer to round it off and then use that value later on.The Ceiling value of a number is the value that is rounded to the closest integer greater than or equal to that integer.Lua provides us with a math.ceil() function that we can use to find the ceil value of a number.ExampleLet’s consider a simple example where we will make use of the math.ceil() function in Lua − Live Demoa = math.ceil(3.3) b = math.ceil(7.1) print(a) print(b)Output4 8ExampleIt should be noted that if we try to find ... Read More

Advertisements