medro-martin cross-posted this post in GitPlait 4 years ago


Gaming and Lua Scripting - 1

in StemSocial4 years ago (edited)

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:
  • Speed.
  • Extensibility
  • Portability
  • 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.
    Minecraftimage.png

    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) RNTRY : An Orbital Simulator

    https://reentrygame.com/

    Re-Entry : An Orbital Simulator (Game Size: ~6 GB)
    This game is reallly phenomenal, and amazing for all space lovers!!
    Read more here: https://reentrygame.com

    A view from the game as you sit within the Mercury spacecraft.
    A run of th game:

    2) FlightGear

    (https://flightgear.org)
    image.png


    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

    (http://www.minetest.net)
    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.

    (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.

    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) : Lua Scripted Video Games
    (The list is really HUGE)

    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:

    1. Copy the above code to a text editor, and save as a file with .c extension. (let's say hello.c)
    2. Open the terminal and compile the hello.c file using the gcc compiler. This will create an executable for us.
    3. 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
    (Dowload: https://code.visualstudio.com )
    image.png
    If you are using VS Code, then you can install the Code Runner extension to enable Lua support.


    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:image.png

    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:

    OperationOperatorUsage
    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 :

    1. Relational Operators: which define math relations between elements (constants or variables)
    OperatorDescription
    >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.image.png

      2. Logical Operators: which define a logical relation...

    OperatorDescription
    andIf both operands are true, then the condition becomes true
    orIf 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:

    1. and operator
    ABA and B
    000
    100
    010
    111

       2. or operator

    ABA or B
    000
    101
    011
    111

       2. not operator

    Anot A
    01
    10

    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 for false.
    • In the truth tables above, 0 corresponds to the boolean value false, and 1 corresponds to boolean value true.
    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:
    image.png

    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...

    image.png

    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:

    1. CSGO Screenshot | Source: Wikipedia | Usage: Fair Use (Low Resolution image used for illustration purposes ONLY)
    2. Lua Logo | Source: Wikipedia | License: Public Domain
    3. Minecraft Screenshot | Source: Wikipedia | Usage: Fair Use (Low Resolution image used for illustration purposes ONLY)
    4. YouTube Video, Rentry | Author: Jeff Favignano
    5. MineTest Game Screenshots | Source: http://www.minetest.net/#gallery | Public Domain

    References

    1. Wikipedia, Lua : https://en.wikipedia.org/wiki/Lua_(programming_language)
    2. MineTest Wiki : https://wiki.minetest.net/Main_Page
    3. FlightGear Wiki : http://wiki.flightgear.org/Main_Page
    4. Lua Website : https://www.lua.org/about.html

    Glossary

    1. 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.

    2. Mods : (context = Minetest Game)
             This is an abbreviation of "modifications" and are modules that can be added to the game to extend its functionalities.

    3. 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.


    10% of the Earnings of this article will be Donated to:
    The DNA Project 🍄🐼🦋 (@dna.org)


    Sort:  

    Congratulations @medro-martin! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

    You distributed more than 5000 upvotes. Your next target is to reach 6000 upvotes.

    You can view your badges on your board And compare to others on the Ranking
    If you no longer want to receive notifications, reply to this comment with the word STOP

    Do not miss the last post from @hivebuzz:

    The Hive community is in mourning. Farewell @lizziesworld!

    Grettings. Exellent.

    Im use Lua Scripting for GameHacking, with the Cheat Engine interface... do you know about that software?

    Thanks. Sorry, I don't know about the Cheat Engine interface.

    Jeje... Cheat Engine is the most powerfull cheating software that exists...

    The software have a debugger (Very good), supports Assembly Code, Lua Scripting, Trainer Generator (Programable or using a template self-generated), have a powerfull memory scanner that supports Binary, Byte, WORD, DWORD, QWORD, Array of Byte, Text, Unknow initial value and Float point scanner... A pointer scanner... DLL Inyector... Auto-Assemble code...

    Is very powerfull but the allmost Online games detectes it and get banned jaja

    Sounds interesting. Thank you!