Updated April 7, 2023
Introduction to Lua wait.
The Lua wait is a function used when multiple processes simultaneously and want the primary process to stop for a given time. It is a method useful for stopping or delays the parent process until the child process is not working completely. It helps to reduce complications of the multiple processes at a time and work orderly. This function helps to stop or sleep parent thread for a minute, second, and millisecond and child thread within time. It helps to stop the parent thread for the child thread procedure and start the parent thread process after finish the child thread process. This method gives sleep time to work multiple thread procedures together without any critical issues.
Syntax of Lua wait
Given below is the syntax of Lua wait:
function wait(unit of time)
Lua source codes for wait function…
end
wait(required time)
Explanation:
- The “wait (unit of time)” is a method used to give the unit of time like a minute, second, and millisecond to stop the working procedure.
Example:
Code:
function wait ( millisecond )
end
- The source code assigns the time to thread procedure for multiple operations.
- The wait function delays the time in the millisecond using syntax example.
- The wait (required time) is a function using forgiven time as per the user’s requirements.
Example:
Code:
wait( 1 )
- The wait function is to stop the time to parent thread for one (1) millisecond.
How does wait Function Work in Lua?
Step 1: Lua programming IDE Environmental Setup.
The Lua text editor, Lua compiler, and Lua interpreter install in your computer as per the operating system and software version.
Or
It does not have software; then, you can use Lua Online IDEs for coding and start Lua programming.
Step 2: Create the Lua File.
The Lua file creates with the .lua extension and writes a source code.
File name: Luacomment.lua
Step 3: Creates the wait function with the time unit argument.
Code:
function wait(millisecond)
write a code for the Lua wait function…
end
Step 4: Create a variable with the operating system time.
Code:
local ostime_vrbl = os.time()
Step 5: Print the running time of the system.
print( "timing of Lua wait function: ", ostime_vrbl );
Step 6: Create a Lua wait method with the given time.
Code:
wait(0.5);
Combine the procedure of the Lua waits for demo example.
Code:
function wait(millisecond)
local ostime_vrbl = os.time()
print( "timing of Lua wait function: ", ostime_vrbl );
end
wait(0.5);
Examples of Lua wait
Given below are the examples mentioned:
Example #1
The Lua wait function with millisecond example and output.
Code:
function wait(millisecond)
local ostime_vrbl = os.time() + millisecond
print( "timing of Lua wait function: ", ostime_vrbl );
end
wait(0.5);
wait(1);
wait(1.5);
wait(2);
wait(2.5);
wait(3);
Output:
Explanation:
- The example is using a millisecond unit of time for function.
- The parent threads delay for 0.5 milliseconds using the method.
Example #2
Function with the second example and output.
Code:
function wait(second)
local ostime_vrbl = os.time() + second
print( "timing of Lua wait function: ", ostime_vrbl );
end
wait(1);
wait(1.5);
wait(2);
wait(3);
wait(4);
wait(5);
Output:
Explanation:
- The example is using the second unit of time for function.
- The parent threads delay from 1 to 5 second using the method.
Example #3
Function with millisecond and second Example and output.
Code:
function wait(second, millisecond)
local ostime_vrbl = os.time() + second, millisecond
print( "timing of Lua wait function: ", ostime_vrbl );
end
wait(1, 2);
wait(1, 0.5);
wait(2, 1);
wait(3, 0.7);
wait(4);
wait(5, 0);
Output:
Explanation:
- The example is using the second and millisecond unit of time for function.
- The parent threads delay for 1, 0.5 (second, millisecond) using the method.
- If we use two units of time, then the user either uses both time units or a single time unit.
Example #4
Lua wait function with “do while” condition example and output.
Code:
function wait(second, millisecond)
local ostime_vrbl = os.time() + second, millisecond;
while os.time() < ostime_vrbl do end
print( "timing of Lua wait function: ", ostime_vrbl );
end
wait(1, 2);
wait(1, 0.5);
wait(2, 1);
wait(3, 0.7);
wait(4);
wait(5, 0);
Output:
Explanation:
- The example is using the second and millisecond unit of time for the Lua wait function.
- The Lua wait function is using the “do while” condition for comparison time.
- If the operating system time is less than the given time, then the wait function works.
Example #5
Lua wait function with “do while” condition example and output.
Code:
function wait(second, millisecond)
local ostime_vrbl = os.time() + millisecond;
repeat until os.time() < ostime_vrbl
print("timing of Lua wait function: ", ostime_vrbl);
end
wait(1, 2);
wait(1, 0.5);
wait(2, 1);
wait(3, 0.7);
wait(4, 1);
wait(5, 0);
Output:
Explanation:
- The example is using the second and millisecond unit of time for the Lua wait function.
- It is used milliseconds to return the time value.
- This function is using the “repeat” condition for comparison time.
- If the operating system time is less than the given time, then the wait function works.
Example #6
Function with compares time and variable example and output.
Code:
function wait(second, millisecond)
local ostime_vrbl = os.time() + second, millisecond;
while os.time() > ostime_vrbl do end
print( "timing of Lua wait function: ", ostime_vrbl );
end
wait(1, 2);
wait(1, 0.5);
wait(2, 1);
wait(3, 0.7);
wait(4);
wait(5, 0);
Output:
Explanation:
- The example is using the second and millisecond unit of time for the Lua wait function.
- The Lua wait function is using the “do while” condition for comparison time.
- If the operating system time is greater than the given time, then the wait function works.
Example #7
Lua waits with zero second example and output.
Code:
function wait(second, millisecond)
local ostime_vrbl = os.time() + second, millisecond;
while os.time() < ostime_vrbl do end
print( "timing of Lua wait function: ", ostime_vrbl );
end
wait(0);
Output:
Explanation:
- The example is using the second and millisecond unit of time for the function.
- The wait method does not use empty or nil values until an error occurred.
- The user must give zero (0), millisecond, second, or minute inside for the wait method.
Conclusion
The Lua wait function helps to avoid complications of multiple operations. This function makes easy, elegant, and sophisticated applications. This function makes a fast, error-free application.
Recommended Articles
We hope that this EDUCBA information on “Lua wait” was beneficial to you. You can view EDUCBA’s recommended articles for more information.