Updated April 19, 2023
Introduction to Lua require
Lua require is one of the function and it is mainly denoted as the high-level function for to load and run the lua script libraries. It will mainly target the high level functions and keywords, packages loaded on the script with the help of require keyword. It is also similar to the dofile keyword it mainly supports the file which is required for to search the path on the location and second thing it mainly focuses on to control whether the file is already running on the script or not. It is used to avoid the duplication on the user task so that require is the main focus on the lua script to load the libraries.
Syntax:
The Lua script have some set of libraries which includes the functions, keywords and variables to implement the application in more sophisticated. It also works with default functions, keywords for to access and satisfied the logic of the application. If we need to use the conditional statements, loops based on the user requirements.
require "module-name" // the module depends upon the user and application like file module etc.
function function-name()
{
----some lua script logic codes ----
}
Above codes are the basic syntax for utilising the “require” keyword on the lua script. It depends upon the user and application requirement because for each type of application like games, image-processing etc the module has main cover of that so by using require keyword we will import it.
How require Function Works in Lua?
- The “require” keyword is used for to import the other modules like file, image etc these modules are built-in the application. It requires and includes some packages that accepts some global variables and functions. It also accepts customized methods and scripts which has been created by the end user. The Lua require() function is more ever similar to the dofile() method it is used for to allows and load the libraries. But it needs and requires some more differences like require to search the paths like files.
- It also included other libraries using find keyword we can find the files and datas. It also supports and it does caching datas. For each function it requires to loads the module for packaging the modules, tables like package.loaded for to load the tables for to determine whether the table module is already loaded on the script or not if its loaded it will shows on the console screen.
- And also it will return the value using require at the value stored at the package.loaded [module_name] on the script otherwise it will load and validated the file via using the loader module if the file is exist it will throw the error and also the syntax of the filename is on the string type and it is provided the way is to require option on the lua script it replaces the characters in the filename with string parameters and path separators like ‘/’ on macOS, Linux and ‘\’ character on windows os. So based on the operating system the special character is require on the script.
Examples of Lua require
Given below are the examples of Lua require:
Example #1
Code:
First.lua:
first = {}
function first.FirstExample()
print "Welcome to my domain its a first example for to execute the lua script depends upon the java code!"
end
return first
Second.lua:
require "one.first"
first.FirstExample()
sample.java:
package one;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;
public class sample {
public static void main(String[] args) {
Globals gl = JsePlatform.standardGlobals();
LuaValue lu = gl.loadfile("one/second.lua");
lu.call();
lu = gl.load("first.FirstExample()");
lu.call();
}
}
Output:
In above example we used lua script embedded with java language. We imported the default package of the luaj by using the Luaj-jse-3.01. jar. We created two lua files first.lua file will have the function to perform the activity and the second.lua file we use require keyword for to include the first.lua logic like calling the method. With the help of java codes we can call and include the lua packages like default classes and its methods. The Globals and LuaValue are the basic and default classes for this script.
Example #2
Code:
First.lua:
first = {}
function first.SecondExample()
print "Welcome to my domain its a second example for to execute the lua script depends upon the java code!"
str = "Thanks for choosing the Lua Scripts"
print(string.find(str,"Execute the Lua scripts"))
revstr = str.reverse(str)
print("The new string is",revstr)
end
return first
Second.lua:
require "one.first"
first.SecondExample()
sample.java:
package one;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;
public class sample {
public static void main(String[] args) {
Globals gl = JsePlatform.standardGlobals();
LuaValue lu = gl.loadfile("one/second.lua");
lu.call();
lu = gl.load("first.SecondExample()");
lu.call();
}
}
Output:
In the second example we used same lua packages included like first example. Here we used string reverse logic in the lua script. We can pass the string data type values like string, characters, alphabets both upper and lower case letters that can be used to perform the string operations in the scripts.
Example #3
Code:
First.lua:
function max(n1, n2)
if (n1 > n2) then
result = n1;
else
result = n2;
end
return result;
end
print("The maximum of the two numbers is ",max(112,421))
print("The maximum of the two numbers is ",max(13,8))
print("The maximum of the two numbers is ",max(11,8))
print("The maximum of the two numbers is ",max(12,8))
Second.lua:
require "one.first"
first.max(112,421)
first.max(11,8)
first.max(12,8)
first.max(13,8)
sample.java:
package one;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;
public class sample {
public static void main(String[] args) {
Globals gl = JsePlatform.standardGlobals();
LuaValue lu = gl.loadfile("one/second.lua");
lu.call();
lu = gl.load("first.max(11,8)");
lu.call();
lu = gl.load("first.max(112,421)");
lu.call();
lu = gl.load("first.max(13,8)");
lu.call();
lu = gl.load("first.max(12,8)");
lu.call();
}
}
Output:
In the final example we can implement the logic like maximum of two numbers in lua script. This logic will be achieved by using the java codes. The above screenshot is the output of the example here we passing the integer values on the max() method the value range is more than that of the default values on the data type so that the exception is occurred on the output screen.
Conclusion
In general lua script used with other object oriented languages like java, c++ etc. Here we used some logics will be implemented on both lua script and java based codes. With the help of “require” keyword we can include the other lua code files. So it must be necessary for performing the dynamic applications.
Recommended Articles
We hope that this EDUCBA information on “Lua require” was beneficial to you. You can view EDUCBA’s recommended articles for more information.