Found 82 Articles for Lua

How to Call a Lua function from C?

Mukul Latiyan
Updated on 20-Jul-2021 14:15:47

2K+ Views

Calling a Lua function from C is something that requires a series of steps and a mastery in the Lua library functions. Lua provides several library functions that we can use whenever we want to call Lua functions from C or the opposite.Some of the most commonly used Lua library functions for calling a Lua function from C are −luaL_dofile(L, "myFile.lua");lua_getglobal(L, "add");lua_pushnumber(L, a);and much more.We will make use of these functions when we will call a Lua function from C.The first step is to shut down the Lua interpreter and for that we need to write a code in C.ExampleConsider ... Read More

How does Garbage Collection work in Lua Programming?

Mukul Latiyan
Updated on 20-Jul-2021 14:12:54

228 Views

Lua provides automatic garbage collection that is very helpful in providing safe memory management. It basically means that you do not need to worry about the newly created object or how to allocate memory.Lua is running a garbage collector to collect all dead objects (that is, objects that can not be accessed in Lua) to perform automatic memory management.Lua also provides us with different functions that we can use to interact with the garbage collector, these functions are −collectgarbage ( "collect") − returns a number that denotes whether the collector does a full garbage collection cycle.collectgarbage ( "count") − returns ... Read More

How do you copy a Lua table by value?

Mukul Latiyan
Updated on 20-Jul-2021 14:11:34

1K+ Views

Copying a table means that we want all the values or pairs that are present in one table in another table. In Lua, there’s no standard library function that we can use to create such a table but we can create our own function to do so.Let’s create a function in Lua that will take a table as an argument and will create a new table that will be the exact copy of the table passed as an argument to the function.ExampleConsider the example shown below as reference − Live Demoa = {} a["name"] = "mukul" a["age"] = 23 a["isWorking"] = ... Read More

Differences between Javascript and Lua programming

Mukul Latiyan
Updated on 20-Jul-2021 14:08:31

177 Views

We know that there’s a huge gap in the popularity and use-cases of JavaScript and Lua. Besides this gap in popularity and use-cases, these languages have many differences at code level.The following table highlights some of the most notable differences between JavaScript and Lua.KeyJavaScriptLuaImplicit conversion when comparingJavaScript does implicit conversion when it is comparing any two objects with either the == or != comparison operators.Lua doesn’t convert between the types when it uses the comparison operators.Operator precedenceIn JavaScript, the ==, ===, != and !== operators are of lower precedence than >, >=,

Difference between . and : in Lua programming

Mukul Latiyan
Updated on 20-Jul-2021 14:07:09

5K+ Views

The .(dot) operator in Lua is used to invoke the method of an object, and it is a widely used operator in Lua.The :(colon) operator in Lua is used when you want to pass an invisible parameter to the method of an object that you are calling.Let’s consider an example where we will have an object in which two functions are present and we will try to access these functions, first by making use of the dot operator and secondly by making use of the colon operator.ExampleConsider the example shown below − Live DemoreturnX = {foo = function(x, y) return x ... Read More

Concatenation of tables in Lua programming

Mukul Latiyan
Updated on 20-Jul-2021 14:05:05

6K+ Views

We can append two tables together in Lua with a trivial function, but it should be noted that no library function exists for the same.There are different approaches to concatenating two tables in Lua. I’ve written two approaches that perform more or less the same when it comes to complexity.The first approach looks something like this −function TableConcat(t1, t2)    for i=1, #t2 do       t1[#t1+1] = t2[i]    end    return t1 endAnother approach of achieving the same is to make use of the ipairs() function.ExampleConsider the example shown below −for _, v in ipairs(t2) do   ... Read More

Concatenation of strings in Lua programming

Mukul Latiyan
Updated on 20-Jul-2021 14:03:40

2K+ Views

Concatenation of strings is the process in which we combine two or more strings with each other, and in most of the programming languages this can be done by making use of the assignment operator.In Lua, the assignment operator concatenation doesn’t work.ExampleConsider the example shown below − Live Demostr1 = "tutorials" str2 = "point" will throw an error s = str1 + str2 print(s)Outputinput:7: attempt to add a 'string' with a 'string'Hence, the most straightforward way is to make use of the concatenation keyword which is denoted by .. (two dots)Let’s consider a few examples of the concatenation keyword in Lua.ExampleConsider ... Read More

Command Line arguments in Lua

Mukul Latiyan
Updated on 20-Jul-2021 14:00:08

4K+ Views

Handling command line arguments in Lua is one of the key features of any programming language. In Lua, the command line arguments are stored in a table named args and we can use the indices to extract any particular command line argument we require.Syntaxlua [options] [script [args]]The options are −-e stat− executes string stat;-l mod− "requires" mod;-i− enters interactive mode after running script;-v− prints version information;--− stops handling options;-− executes stdin as a file and stops handlingoptions.ExampleLet’s consider an example where we will open a Lua shell in interactive mode and we will pass the script as dev/null and then we ... Read More

Code indentation in Lua Programming

Mukul Latiyan
Updated on 20-Jul-2021 13:53:46

2K+ Views

Lua codes are not like Python when it comes to indentation. So, most of the code you will write will work even if it falls on another line, and you don’t necessarily need to have the nested code to be intended by a certain tab size.The code indentation in lua is more about making the code look much better and more readable. If your entire code is on one line or worse, if it is like multiple lines then I am afraid that your code isn’t very readable.While we can use the code editor’s indentation packages to do the indentation ... Read More

Array Size in Lua Programming

Mukul Latiyan
Updated on 20-Jul-2021 13:52:36

4K+ Views

It is a general convention that the size of the array is the number of elements that are present inside the array before the nil. In many cases, nil is not allowed in the array, but for some applications, it is not an issue to have nil inside them.If we allow nil values inside the array, then we must have some functions to set the size of the array explicitly.Lua does provide us with two functions to manipulate the size of the array and these are −setngetnThe setn function is used to set the size of the array explicitly and ... Read More

Advertisements