Found 82 Articles for Lua

Difference between Python and Lua

Pradeep Kumar
Updated on 10-Aug-2022 07:16:25
There are many different kinds of application-specific scripting languages, some of which are Emacs LISP, MEL (Maya Embedded Language), AutoLISP, and MaxScript. There are also others that are more flexible and are ideal for the development of high-level applications such as Java, OCaml, C#, and so on. Then there is a category of programming languages known as embedded scripting languages, which were developed in order to provide an easy integration with bigger programmes. They provide programmes with new functionality and link together applications that have a complex relationship. These kinds of scripting languages typically provide substantial support for utility packages ... Read More

While loop in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:58:07
A while loop is an indefinite loop that can be modified to run for a finite number of iterations based on the condition we provide.In Lua, the while condition is tested first. If the condition turns out to be false, then the loop ends, otherwise, Lua executes the body of the loop and repeats the process.Syntaxwhile( condition ){ // do this }ExampleConsider the example shown below −a = {1, 2, 3, 4, 5} local i = 1 while a[i] do print(a[i]) i = i + 1 endOutput1 2 3 4 5It ... Read More

Variable number of arguments in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:50:06
There are functions in Lua that accept a variable number of arguments. These are very helpful in cases where we want to run the same function with many different arguments that might vary in length. So, instead of creating a different function, we pass them in a variable arguments fashion.Syntaxfunction add(...) -- function code endIt should be noted that the three dots (...) in the parameter list indicate that the function has a variable number of arguments. Whenever this function will be called, all its arguments will be collected in a single table, which the function addresses ... Read More

Table Type in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:46:45
A table is a data type in Lua, which is used to implement associative arrays. These associative arrays can be used to implement different data structures like queues, maps, lists, etc.An associative array in Lua is an array that can be indexed not only with numbers, but also with strings or any other value of the language, except nil.Tables in Lua don't have any fixed size, and we can insert as many elements as we want in them, dynamically.Tables in Lua are neither values nor variables; they are objects.We can create tables by means of a constructor expression, which in ... Read More

Return statement in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:44:34
There are certain cases where we want to return a value from a given function so that we can use it later. These return values make use of a return keyword which in turn allows a function to return values.There is an implicit return at the end of any function, so you do not need to use one if your function ends naturally, without returning any value.It should be noted that the return statement is optional; if not specified, the function returns nil.Also, only one return statement is allowed in a function.Syntaxreturn expression/valueNow let’s consider an example where we would ... Read More

Numeric for in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:42:21
In Lua, there are two types of for loops − the numeric for and the generic for.SyntaxThe numeric for uses the following syntax −for var=exp1, exp2, exp3 do something endIt should be noted that we can write exp1, exp2, exp3 at the same time or we can omit one of them, and the numeric loop will not result in a compile error, though its functionality will change.ExampleLet’s consider a simple variation of a numeric for loop, where we will try to print numbers from 1 to 10.Consider the example shown below −for i = 1, 10 do ... Read More

Named Arguments in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:36:33
We know that when we pass arguments to a function in any programming language, they are matched against a parameter. The first argument’s value will be stored in the first parameter, the second argument’s value will be stored in the second parameter, and so on.ExampleConsider the example shown below −local function A(name, age, hobby) print(name .. " is " .. age .. " years old and likes " .. hobby) end A("Mukul", 24, "eating")OutputMukul is 24 years old and likes eating The above example works fine if we carefully pass the same argument as for the ... Read More

If-then-else in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:32:59
An if statement in Lua is used to evaluate some code based on some conditions. If those conditions match, then we execute the code that is written inside the code block of an if statement, else we do nothing.In Lua, the if statement tests its condition and if that condition evaluates to true, then it executes its then-part or its else-part.The else-part is optional in Lua.ExampleConsider the example shown below −a = -1 if a < 0 then a = 0 end print(a)Output0 We can also insert an else-part in the above statement to make ... Read More

Global Variables in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:29:49
Global variables in Lua are the variables that don’t need any type of declaration in them. We can simply define the name of the variable and assign any value we want to it, without having to use any keyword with it.Having global variables makes certain programming cases possible, and it is also preferred if we want to create a variable that we want to use in multiple functions. In case we don’t use global variables, we might have to pass that variable to different functions where we want to use it, which is a bit more tedious.SyntaxThe syntax for declaring ... Read More

Generic for in Lua Programming

Mukul Latiyan
Updated on 01-Dec-2021 10:28:47
The generic for in Lua allows us to iterate over the values in an iterator fashion; it is much more powerful even though it looks simple. The Lua library has plenty of iterators, over which we can use the generic for loop.Syntaxfor i, v in pairs(x) do ... ... endThe i in the above syntax denotes the index of the items we are going to iterate over only by one, and the v denotes the actual values of those items. The x is the iterable item over which ... Read More
1 2 3 4 5 ... 9 Next