Lua is not an interpreted language and generates bytecode from another program, such as Java. This program runs on a Lua VM virtual machine. Most of these devices are called stacks, but Lua differs in that it is case-based and one of the first widely used VMs in the gaming field.
The advantage of the registry architecture is that it avoids the large amount of copied data and reduces the volume of instructions for the function. Lua VM has automatic memory management and dumping of unnecessary information, which provides the ideal configuration for creating rapid prototyping scenarios.
Briefly about Lua
Lua is an easy to learn and fast scripting language with syntax like pascal. This allows novice programmers to write their own programs. It is used in many games and applications to extend existing functionality and program user sequences. There are many tutorials on the Internet that help you work in this language, including a lot of information can be found on the Lua homepage.
This programming language was created in Brazil in 1993 by programmers Roberto Jerusalshim, Luis Enrique de Figueiredo and Valdemar Celes at the University of Rio de Janeiro, where the LabLua office is still located. The name comes from Portuguese, which means moon. One of the predecessors was SOL (Simple Object Language). Since the Lua name is a noun, not an abbreviation, it should not be spelled as LUA, namely as Lua.
This is a flexible language, it is used in several ways:
- Wikipedia applies Lua to template scripts.
- The VLC media player works with Lua scripting scripts for quik.
- Lua is used in industrial applications such as Adobe Lightroom.
- Programs for embedded systems, such as the Ginga digital TV middleware.
- Games - the language is leading for scripting in games, for example, is used to create World of Warcraft and Angry Birds.
- Everyone can try using the code online in Lua Demo with an interesting way to learn and use the game engine.
The functionality of this program can be used in LÖVE, Defold and, of course, in Pico-8.
Syntax Basics
Lua uses a very simplified syntax. The user does not need to use semicolons or large spaces, because these characters are themselves interpreted by the program. All variables in Lua scripts for quik are global by default. Therefore, you should always indicate when local values are needed for the current region and pre-declare them. In fact, in practice there are very few cases where global values are actually needed.
You can define a local variable with the local keyword. Moreover, its value is not necessary to declare. Global variables mean the context of the current scenario and therefore are not such for each individual script in the game, which each work independently and independently.
Variables can contain any sequence of numbers, letters, and underscores, but they must not begin with a number. Null represents the keyword when variables are not defined. Undeclared indistinguishable from declared variables that do not matter. Examples of Lua scripts:
Code blocks and if statements
Blocks are defined by specific keywords, followed by “end”. Conditions in if statements must be followed by "then" keywords. If statements use "elseif" rather than "else if". You can create a new empty area with a "do ... end" block.
Lua uses a special data type called a table, designed to create arrays, dictionaries, hash maps, objects of other languages. Tables are just a list of key-value pairs, which can be any data type, even other tables. You can use a boolean value as a key. Since Lua is dynamically typed, you can have any mixture of data types in both keys and user-selected values that you need to decide before installing Lua scripts.
The values of the tables can be defined both internally and separately. There are usually two ways to get and set their values, the dot syntax, for example, myTable.a, and the bracket syntax, for example, myTable ["a"]. When using string keys, you can use only the point syntax, and you cannot use it if the string starts with a number.
Each value in the table is taken into account, nil - if it is not set. This means that it is impossible to determine if a value is declared in the table or is it just zero if it has never been defined before. This means that if the keys in the table are indicated and set to zero, this will not have any effect, or rather, it will be the same as it would be if they had not been determined at all. In Lua, tables with numeric keys start at index 1, not 0. This is what causes a big headache for programmers. You can separate inline table elements with a comma or semicolon.
Complex data type
Tables are considered a complex data type in Lua - this means that the variables in them simply refer to the same object in memory, so you cannot directly compare two values and expect them to do anything. However, strings, numbers, letters, and other primitive data types can be compared directly.
Numbers in Lua use only one form - their own number. There are no such types as int, float, long, double, and others, as in other languages, and this must be taken into account before installing Lua scripts. Technically, all values in Lua are floating point numbers.
Lua has only a “=” operator to define and increment. It does not have “+ =”, “- =”, “++” or anything else. The concatenation operator is "...". You can apply this operation on lines and numbers without problems. However, you cannot combine other data types - logical or tabular. You can get the length of a table or row with the "#" operator and this only works for arrays with numeric indices. For example, code will not work with tables that have string indexes.
Loops Loop Types
There are four types of Loop loops, which means loop or loop.
While (intermediate) is the simplest kind of cycle and functions, as one would expect in other languages, and means that the check is carried out before starting the internal block.
Numeric for loop (digital). The loop applies the local variable to its scope and increases or decreases it. The user declares and defines the variable to be used and followed by the value of the target.
Generic for loop (generated) - uses the iterator function. It is called by repeating each cycle and returns the values that will be applied to it.
Pairs is a global iterator function. It takes a table as an argument, returns the index and value of each variable in the table. The code will iterate even nonzero indexes. She has a “brother” - “ipairs”, which does exactly the same thing, except that it only works with numeric indexes and stops when the nil value is reached.
Repeat loop is similar to while, except that conditions are checked at the end of the block, not at the beginning. Before you open the lua script, you need to consider that the code inside will be run at least once. The repetition loop is usually avoided in developing this language because its use case is limited and difficult to read because it does not use the standard do..end syntax.
Function method
Functions are another complex data type that can be passed as variables, and you can also call them to execute code inside. They are similar to methods in other languages.
There are two ways to define a function in Lua. The first ad is global because there is no local keyword. As a rule, global functions are considered acceptable. Nevertheless, the user can still make them local, if necessary, by setting a "local" front. You can also declare variables before defining functions that are still similar to any other data type. You can pass them, assign them to variables, or accept as many arguments as needed using the syntax "...". The actual "..." object is a "tuple" (tuple), so you need to surround it with curly braces to turn the data into a table.
A tuple is simply a list of names, such as parameters in functions, that can return multiple values, and they, in turn, can be assigned variables. If you place a function in a table, you can call it in two different ways. The standard dot or colon syntax will send an array as the first argument.
Global namespace
Lua provides several tables in the global namespace that contain helper functions, such as string, table, and math. Thus, the user should never specify any of the variables named, since they will redistribute arrays containing useful functions.
There are two tables that are shared by all scripts on the server or client (but not for both) - these are "_G" and "shared". The first is truly global, implemented through separate scripts, so if you need to get or set values from it, use "_G.something".
“Strings” - With this function, Lua can define a string literal and multi-line data in several ways using double square brackets. And you can also place any number of equal signs (=) between square brackets ([=== [...] ===]). They are balanced, may include sequences within, like]]. Lua also has its own set of built-in string manipulation functions and its own extruded version of regular expressions called string patterns.
Game Moon Scenario
These are very simple first steps to writing a program, but they are good starting points for cracking Garry Mod 10 Lua scripts. The first step is to create the actual Lua script file. Open a text or editor - a notebook will work great for this purpose. Then save this file, prescribing, as in the figure.
Where:
- <Steamname> is the password username.
- <Garry's Mod Install location> - Steam directory for Garry's Mod.
The default installation location for Lua scripts for garry s mod is usually "C: \ Program Files \ Steam \ steamapps \\ garrysmod", but may differ on the computer - "autorun \ client". Any script files placed in this directory will be launched when Lua starts on the client, for example, when a new game starts or the user joins the server. Next, create a file of type Hello World, adding to it the script lua mod scripts. This is the easiest option.
This may seem a bit confusing right away. The script returns the contents of the table (which arrays or lists are called) and intercepts the data before the console command to confirm that it is necessary to define a function that takes no parameters and does not return values, but always sets the “end” at the end of its commands.
Functions are modular pieces of code that can be used multiple times. This example shows how to run a Lua script. A list of strings has been created here, but the user can also insert other objects. Tables are associative or hashed. This means that each item in the list can refer to a key.
These commands print all the elements in the table, and the structure is called a loop. Pay attention to the command of the “myTable” pair, which is run once for each element and returns key / value pairs for the values in the list.
A value is an element in a table, and a key is just a way of referencing it. This example shows how to print text and variable values with the same print command. The variables "key" and "value" are set in the loop above. Pay attention to the "..." between strings and variables. The symbol "..." is the Lua concatenation operator. This tells the language that you need to glue the two elements together and treat them as one text string “.concommand.Add”:
- my_test_function;
- myTestFunction.
The record creates a console command that launches the function that has just been initiated. Its first parameter is the name of the control command, in this case: "my_test_function". This is what the user enters into the Garry mod console to run the script.
The second parameter is the function to run, in this case: myTestFunction. This file will subsequently need to be saved. Next, test the script, launch Garry's Mod and a new game. After that, open the console using the tilde (~). If it does not appear, then you need to enable it by entering "my_test_function" in the line at the bottom of the panel. After starting, the screen will automatically fill with the name of the command, press Enter to start it.
Features of Lua-Quick-Try-Out
This console is intended for direct input of Lua code (for example, the command of the Lua command (8 + 9) immediately displays the result 17).
Its functions:
- The main window for the source code with functions for saving and loading it.
- Script debugger with breakpoints, one-step execution and variable view.
- Functions for creating tables.
- Functions for creating charts.
- Functions for creating graphic images.
- Functions for creating vector graphics.
- Online help for building Lua functions.
Breakpoint console points can be set by clicking next to the line number. If debugging mode is enabled, the variable view will be visible on the right side of the main window. Its size can be changed by dragging the left border. Various "on" debugging mode options or off saved and restored when it is turned on or off. You can use the mouse wheel to change the visible size of charts and graphs. The right mouse button can be used to open the context menu for diagrams with functions for their scaling.
SAMP Lua scripts is the lua library for MoonLoader, which adds some commands to simplify the modification of SA: MP, SAMP. Events makes it possible to handle SA: MP inbound and outbound low-level network packets in a very simple way. You can rewrite the data by setting all the arguments in the table in the correct order, or you can interrupt the processing of any packages by returning false, which prevents the user from changing the player’s position. Before installing the Lua script samp, you can add your own package handler.
Lua-WoW distribution
This is the name of the Lua source code distribution, modified to match the World of Warcraft add-ons environment.
His data:
- Administrators: morlando.
- Users: morlando.
- License: MIT / X.
- Language: c, lua 5.
- Tags: translators, testing tools.
- Operating Systems: macros, windows, os independent.
This version of the language is configured to match the Lua World of Warcraft environment. All compatibility parameters 5.0-5.1 are disabled except openlib. The bitlib library is working. Some wow Lua scripts are specific, Lua library functions are implemented.
The program provides global aliases for the Lua.string.format functions, they can use the argument selection from Lua 4.0. The Windows binaries included in this download are statically linked for performance. They may not work as expected by the user when used with some third-party packages.
Version History:
- V1.12 - added scrub, tostringall, the Visual Studio project has been updated to Visual Studio 2008 Visual C ++. Runtime is now statically linked, which should fix some of the problems associated with lua-wow on certain Windows installations.
- Lua scripts v34 c - added strrelpace (string.replace, wow.strreplace).
- V1.10.1 - added msvcr80.dll to the windows project.
- V1.10 - Reorganized readme files. Renamed msvc projects and exe exit names. The msvc project includes the Lua icon and resources for executables.
- V1.9 - Added global versions of trigonometric functions. The program correctly uses degrees .strsplit, it will print the corresponding error message if it overflows the stack, which is now almost impossible with strjoin.
- V1.8 - Updated openlib 5.0 compatibility as WoW does not use libs in any case.
- V1.7 - changed GCPAUSE to 110 on WoW 2.1. Fixed several different compiler warnings.
- V1.6.1 - added #define LUA_LIB for lwowlib & lbitlib to fix compilation problems in Lua scripts for sumps.
- V1.6 - updated bitlib to the latest version. Now it correctly uses 32-bit integers like WoW. The global aliases for the bitlib and table.setn functions have been removed since they do not exist in WoW.
- V1.5.1 - Ignores the byte order sign of UTF-8.
- V1.5 - debugstack added, some minor optimizations and code simplifications made.
- V1.4 - added argument selection in string.format.
- V1.3.1 - fixed problem with Makefile.
- V1.3 - added specific functions, global aliases, css Lua scripts.
- V1.2 - this package can now be used as a replacement for the standard Lua source distribution: the MSVC project, including precompiled Windows binaries, is now in the msvc subdirectory, the rest of the directory structure is not affected.
- V1.1 - added BitLib. Optimized file size Luac.exe. Includes test programs.
Game engine Pico-8
Pico-8 is a fantastic program that emulates a complete game console with limited 8-bit specifications. It has all the necessary tools for programming in the Lua language. The reason why users choose Pico-8 is that it can be used right out of the box, without the need for customization or external editors, all tools are included in the Lua script package for sumps, and the training is pretty simple.
Operating procedure:
- Usually it is customary to call the first program that is created in a new language, the program "Hello world", but for the programming language Lua type "Hello Lua".
- Download Pico-8.
- Install and run.
- After downloading, the ">" prompt will be received.
- To get an overview of the possible commands, press ESC. Now, when the editor is open, enter the codes shown in the figure above.
- Press ESC again to return to the prompt, enter CLS, and then RUN.
- Get the first Lua program on the Pico-8.
- To save the script, use the SAVE: SAVE HELLOLUA command.
- If you need to work with the program, you can use the BTN () function, which provides a response to keystrokes.
- To have more graphical capabilities, they use two special callback functions called UPDATE and _DRAW. The following program allows you to move the circle using the arrow keys.
- Press ESC to go to the code editor, enter or copy and paste the code, as in the figure below.
- Press escape to return to the console, and enter RUN.
Pico-8 is designed only to display uppercase characters, so you can just type the font, there is no need to use Shift or Caps-lock. The editor also has syntax coloring for the code. If you look in the upper right corner of the editor, you can see several icons. These are built-in tools for creating sprites, maps, sounds and music. This example is pretty simple, but gives an idea of the editor and language.
To explore more interesting examples, you can go to the DEMOS directory. To do this, enter: INSTALL_DEMOS. After that, the DEMOS directory opens with several sample programs for learning. One of the main advantages of Lua is its simplicity. Some companies use this language solely because of this, because they think their employees will be able to work better.
Some very simple languages, such as Bash or Batch, will not be strong enough to perform the required tasks, but Lua is both a powerful and simple program. Another important advantage of Lua scripts for css v34 is their ability to be implemented, which is recognized as one of the most important characteristics of this product throughout the entire stage of its development. Games like World of Warcraft or ROBLOX have the ability to embed Lua in their application.