H
ello everyone!
Hope you all are well and in the pink of health.
Brief (and boring) Introduction to this article. | |
---|---|
In this article, we'll learn about LUA. LUA is a scripting language developed primarily for embedded applications. Uses: LUA is used in applications which need to be efficient and fast. Important Aspects of LUA: |
Gaming - The Interesting Part!
We all love computer games, don't we? From CS Go to Minecraft to older games like Mario!
Counter Strike - Global Offensive screenshot. | |
Minecraft |
The modern day games pack quite a punch with amazing graphics rendering, and realistic lighting effects. It is really stunning, how they are able to process so much without causing our computer's and laptop devices to break down and hang!
This probably is one of the major challenges of the modern day gaming industry - to build games that have good graphics but at the same time have low processing demands.
Here are some of my personal favorites:
1) R≡NTRY : An Orbital Simulator
(Game Size: ~6 GB) This game is reallly phenomenal, and amazing for all space lovers!! Read more here: https://reentrygame.com Re-Entry : An Orbital Simulator | A view from the game as you sit within the Mercury spacecraft. |
A run of th game: |
2) FlightGear
FlightGear is a very sophisticated flight simulator, open-source and available for free, and developed by proffessionals and enthusiasts. | A run of the simulator, Aircraft = Space Shuttle. |
FlightGear uses it's own LUA-like scripting language called: NASAL (Not Another Scripting Language).
3) MineTest
Minetest is the open-source alternative of Minecraft. While Minecraft is owned and developed by Microsoft and is obviously paid software, Minetest is free and open-source.(http://www.minetest.net)
(Some screenshots from the game)
Minetest uses LUA.
According to Minetest Wiki:Minetest has a scripting API (Glossary: 1) (Application Programming Interface), which is used to program mods (short for "modifications") for the game, extending its features and adding new items. This API is accessed using an easy-to-use programming language called Lua.
Lua Scripted Video Games
(The list is really HUGE)Not just MineTest, but Lua is used a lot to make many-many more game engines...The following are some of them (given by https://lua.org) :
The main reason why Lua is used in game-engines is that it is free, fast, and is cross-platform and portable. More over, it is embedable and can easily be used alongside other programming languages.
I got interested in Lua mainly because of my interest in contributing to the Minetest code, and Mods (Glossary: 2). It's great to play games to which you have contributed! Haha!
So I guess, I have been able to give a sufficiently good and interesting introduction to Lua!
1. "Scripting Language" ?!
When I first heard that Lua was a scripting language, I was very much confused. Never had heard it other than in JavaScript (Glossary: 3)...and then came the next questing, why exactly is even JavaScript (JS) called a "Script" or more precisely a "scripting lang."!!
Back to Basic C-Programming!
What to do if for example, we have the following C-program, and ew want to run it?
#include<stdio.h> int main(void) { printf("Hello world"); }
We'll have to follow these steps:
- Copy the above code to a text editor, and save as a file with
.c
extension. (let's sayhello.c
)- Open the terminal and compile the
hello.c
file using the gcc compiler. This will create an executable for us.- Run the executable.
Alright, enough of C, now let's get to the binaries!! Never heard the word?!
BINARIES : (executables)
Executables are often called "binaries" for the very simple reason that they are essentially the code that the computer understands. They directly relate to the computer's mother-tongue: binary (0's and 1's). Hence, these files can be directly executed by the computer without the need of any external crash-courses!! Haha!
Examples: .exe
files in Windows, .out
or .run
files in Linux and .app
files in MacOS.
NOTE: The singular word "binary" may have different meanings, and may be used to point to any digital file (image, video, executable etc) other than a text file.
So, we saw that some programming languages like C require the user to compile them (convert them to machine code / binary code which the computer can understand) into an executable before the code can be run.
On the other hand, some other languages are compiled executed line-by-line, and do not require explicit compiling. This means that executables need not be created and the code can be run as such. Such languages are called Scripting Languages.
Examples: JavaScript, Lua etc.
NOTE: Scripting languages are also often referred to as "interpreted languages" (as against compiled languages), and will require an interpreter (Glossary: 4) (instead of a compiler) for them to be executed.
2. Lua 101 : The Basics
Now, let's get started with Lua, and see how easy or difficult it is... (According to Lua.org, it is supposed to be easy!)
2.1) Selecting a text-editor.
We'll need a text editor to write our Lua code in. In general it is good to use a text editor which provides proper syntax highlighting as this will go a far way in helping us debug the code.
I use VS Code as it supports a number of different languages and is extensible.
Visual Studio Code
If you are using VS Code, then you can install the Code Runner extension to enable Lua support. (Dowload: https://code.visualstudio.com )
You can also try this IDE (Glossary: 5) recommended by Lua.org : https://studio.zerobrane.com
2.2) Install Lua ?
If you have an IDE that supports Lua, then you should most probably be fine as the IDE itself should have the Lua Interpreter, so there shouldn't be any need to install Lua separately unless you are working on a project with code not running on the IDE.
If you want the code to be executed directly by the PC without the need for interference by the IDE, then you may want to install Lua directly onto your PC.
(Installation instructions : https://www.lua.org/start.html)
2.3) "Scripting" in Lua - 1
"Scripting" definitely means the same as "coding"! It's just that we are using a scripting language...
2.3.1) Commenting
Commenting is probably the first thing one should learn in programming, because a program without proper comments can be a useless mess which no-one can understand. Yet, commenting is the easiest thing one can do in a program!
Let's see how we can add comments in Lua.... (and of-course comments are not interpreted, and have no effect on the program other than making it more readable to other programmers trying to work on your code.)
Syntax:
-- This is a SINGLE-LINE COMMENT.
--[[
This is a MULTI-LINE COMMENT.
]]
2.3.2) Defining variables
Variables definitely are elements that can store values unlike constants.
Defining variables in Lua is quite easy and straight-forward. Here are some examples to show this:
Example-1: Define a variable and store the string 'Martin'
in it.
name = 'Martin'
Example-2: Define a variable and store the integer 100
in it.
marks = 100
Example-3: Define a variable and store the string 'Martin'
in it, following this re-assign the integer 100 to it.
name = 'Martin'
name = 100
io.write(name, "\n")
Output: |
NOTE-1: As we saw above, Lua doesn't give an error upon changing the datatype of a particular variable. Other languages like C, Python and most others will give an error if a similar operation is carried out.
NOTE-2: In Lua, using a semicolon (
;
) to demarcate the end of an executable statement is optional. It is compulsory in some languages like C, and is not allowed in languages like Python.
This would mean that I can write the above code as follows with no visible change in output.
name = 'Martin'; name = 100; io.write(name, "\n");
NOTE-3:
io.write()
is used to write a particular string to the output. "io" is the shorthand for "input-output", many times also written as "I/O".
2.3.3) Mathematical Operators
Mathematical operators are one of the very essential basics of any programming language. So, here is a list of math operators in Lua and their usage:
Operation | Operator | Usage |
---|---|---|
Addition | + | c = a + b |
Subtraction | - | (do we really need usage?!! These are quite trivial! 😄 ) |
Division | / | =DIY= |
Multiplication | * | =DIY= |
Modulus | % | =DIY= |
Mudulus: This is the remainder after integer division, the decimal points are simply truncated.
2.3.4) Conditional Statements
There are of two other types of operators as well :
- Relational Operators: which define math relations between elements (constants or variables)
Operator | Description |
---|---|
> | Greater than |
>= | Greater than or equal to |
< | Lesser than |
<= | Less than or equal to |
== | Equal to |
~= | Not Equal to |
Reveal spoiler
NOTE: Many languages like Python and C use ! as the negation operator, so that the operator for "not equal to" then becomes: != , but in Lua the symbol to be used for negation is ~ , which is generally to the left of 1 on the keyboard. |
2. Logical Operators: which define a logical relation...
Operator | Description |
---|---|
and | If both operands are true , then the condition becomes true |
or | If one of the two operands is true , the condition becomes true . |
not |
We can also try to understand the and
, or
and not
operators using a Truth Table...
Let A and B be two operands, then the truth tables will be as follows:
and
operator
A | B | A and B |
---|---|---|
0 | 0 | 0 |
1 | 0 | 0 |
0 | 1 | 0 |
1 | 1 | 1 |
2. or
operator
A | B | A or B |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 1 |
2. not
operator
A | not A |
---|---|
0 | 1 |
1 | 0 |
NOTE:
- I have knowingly written
true
as a code, because it is a boolean value and can be stored in a variable just like any other data type, and the same goes forfalse
.- In the truth tables above, 0 corresponds to the boolean value
false
, and 1 corresponds to boolean valuetrue
.
Using Relational and Logical Operators to form Conditional Statements
Now, we'll try to form some conditional statements using the above mentioned operators using the if
clause...
Example: Create a variable named "status", if the variable sp < 500
, assign:status = 'Red Fish'
, elseif sp
in range (500,1000], assign string 'Minnow'
to status
. Initialize sp = 340
at the beginning.
sp = 340;
if sp < 500 then
status = 'Red Fish';
elseif (status > 500) and (status < 1000) then
status = 'Minnow';
end;
io.write(status)
Output:
Oops! This article has already become very long...I hope you enjoyed reading it.
In some of the next articles we'll look at Lua and go deeper into the language...
Thank you, and remember everything is possible!About this image: This is an external view of lift-off of Space Shuttle in the FlightGear Simulator. I never imagined that a free simulator could be so sophisticated! |
Credits
All media used in this article are mine unless specified otherwise.
I am thankful to the following sources:
- CSGO Screenshot | Source: Wikipedia | Usage: Fair Use (Low Resolution image used for illustration purposes ONLY)
- Lua Logo | Source: Wikipedia | License: Public Domain
- Minecraft Screenshot | Source: Wikipedia | Usage: Fair Use (Low Resolution image used for illustration purposes ONLY)
- YouTube Video, Rentry | Author: Jeff Favignano
- MineTest Game Screenshots | Source: http://www.minetest.net/#gallery | Public Domain
References
- Wikipedia, Lua : https://en.wikipedia.org/wiki/Lua_(programming_language)
- MineTest Wiki : https://wiki.minetest.net/Main_Page
- FlightGear Wiki : http://wiki.flightgear.org/Main_Page
- Lua Website : https://www.lua.org/about.html
Glossary
- API : (Application Programming Interface)
Is a computing interface which defines interactions between multiple software intermediaries. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conventions to follow, etc. - Mods : (context = Minetest Game)
This is an abbreviation of "modifications" and are modules that can be added to the game to extend its functionalities. - JavaScript : (JS)
Alongside HTML and CSS, JavaScript is one of the core technologies of the World Wide Web. JavaScript enables interactive web pages and is an essential part of web applications. The vast majority of websites use it for client-side page behavior, and all major web browsers have a dedicated JavaScript engine to execute it.
4. Interpreter:
Is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program.
5. IDE:
(Integrated Development Environment) Is a software application that provides comprehensive facilities to computer programmers for software development.
@tipu curate
Hi there!
Thanks for intruducing Lua, it's basics and also how to install it. This post is really resourceful.
Thanks again.🤗
Your post has been curated with @gitplait community account because this is the kind of publications we like to see in our community.
Join our Community on Hive and Chat with us on Discord.
[Gitplait-Team]
Thanks a lot! I hope to see your new community and initiative rise to great heights soon!
(I am on your Discord Server 😉, and your https://gitplait.tech website looks great! Thanks.)
Best of Luck
M Medro
!discovery 35
This post was shared and voted inside the discord by the curators team of discovery-it
Join our community! hive-193212
Discovery-it is also a Witness, vote for us here
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider supporting our funding proposal, approving our witness (@stem.witness) or delegating to the @stemsocial account (for some ROI).
Please consider using the STEMsocial app
app and including @stemsocial as a beneficiary to get a stronger support.