Lua Tutorial #5 – How to use Variables in Lua

variables in lua

In programming, a variable is a container or placeholder that stores a value, such as a number or a string. Variables are used to store data that can be manipulated or used by a program. They provide a way to refer to a value by a name or identifier, rather than having to repeat the value itself throughout the program.

In Lua, there are three main types of variables: Global variables, Local variables and Table Fields

Global Variables

Global variables are variable that can access everywhere in the code after it has been declared.

They are declared simply by assigning a value to a variable without any keyword. Here’s an example of declaring a global variable named myVar with a value of 42:

myVar = 42

This will create a global variable named myVar that can be accessed from anywhere in the Lua program. However, it’s important to note that using global variables can lead to issues with code maintainability and readability, and it’s generally recommended to use local variables wherever possible.

Local Variables

A local variable is a variable is that can can only be access within scope. For example if a local variable is declared inside a function, then it can only be used inside that function.

You can declare a local variable using the “local” keyword followed by the variable name. For example:

local myVar = 10

This declares a local variable “x” with the value of 10. The scope of this variable is limited to the block of code it is declared in. Once the block of code is exited, the variable is no longer accessible.

For example:

local new_variable = 90

function some_function() 
 local other_variable2 = 12
  
end -- end of function

print(new_variable)

print(other_variable2)

This code declares a local variable called new_variable and assigns the value of 90 to it. Then it defines a function called “some_function” that declares a local variable called other_variable2 and assigns the value of 12 to it.

The print function is then used to display the value of new_variable which is visible throughout the program. However, when it tries to display the value of other_variable2, which is a local variable of the function, an error occurs because it is only visible within the scope of the function.

Tables

Finally, Table is a data structure that can hold multiple values and is created using curly braces {}. Table fields are the values that are stored within a table, and they can be accessed by their corresponding keys.

For example, consider the following code snippet:

-- Creating a table with some fields
my_table = {name="John", age=30, city="New York"}

-- Accessing a field in the table
print(my_table.name) -- Output: John

In this example, we create a table called my_table with three fields: name, age, and city. We can access the value of a field by using its corresponding key with the dot notation, as in my_table.name ( indexing). In this case, the output would be John.

<< Lua Tutorial #4 – How to Use The print() Function
Lua Tutorial #6 -How to Use String >>

Share

techmugen

© 2024 techmugen