Lua questions #1

Im making multiple post asking questions so that any questions don’t get cluttered.

Please Consider checking out Lua Questions 2


Whats the difference from using assert() and .__index?

What is “garbadge” and “garbadgeCollecting” in lua? and why is it important to games?

What is an enviournment?

What is a scope?

How does getfenv() work, what can I use it for?

Whats the difference from

local table = {"1", 1, "10", 5, "hello", myNameIs, "StarJ3M"}

and

local Table = {
   hello = "hi"
   numb = 5
}

And why does the script at the top sometimes use boxes like this?

local Table = {
   [hello] = "hi"
   [numb] = 5
}

What does next do, is it important?

Whats the difference of pairs and ipairs?

Are there other uses for

in

Instead of it primarily being used for pair loops?


3 Likes
local array = {"1", 1, "10", 5, "hello", myNameIs, "StarJ3M"}

is an array (a table that have numerical indexes that start from 1 and go higher) while

local dictionary = {
   hello = "hi"
   numb = 5
}

is a dictionary (a table where you specify the index of an object).

Keep in mind and that dictionaries and arrays are both tables. It’s just that tables in Lua supports them both, in fact, you can create hybrid tables.

local hybrid = {
"string1",
[1] = "string2",
["Test"] = 50,
24
}
1 Like

What’s a hybrid table? Also, why did you use [] for some indexes?

And what’s the difference from specifying index in a dictionary and array, because can’t you just list them in order for them to be basically the same thing?

1 Like

That’s how you set an index, and because doing

local dictionary = {
	1 = 1,
	"Test" = 10
}

gives an error.

1 Like

.__index is a metamethod used for metatables that are connected to other tables. It gets fired whenever the code tries to search for something in the table connected to the metatable that actually isn’t in the table. It won’t error but instead then is able to search in the metatable for the same value and return it that way. assert() on the other hand just checks if something is false or nil and then just runs an error message. ipairs is index pairs and is used to keep the order of an array from 1 to your array length. pairs doesn’t keep track of order and is used for dictionaries. the keyword “in” doesn’t get used for anything else. getfenv() gets the current environment. I can’t really explain how environments works but a function is a new environment for example. If you were to assign a local variable inside an environment it would only be saved inside that environment and can only be accessed from that environment and not outside of it. Garbage collection gets rid of data that doesn’t get used in the script anymore at all. Luckily lua automatically does this else it would be 10x harder to code without the memory filling up. And garbage are just values that don’t get used anymore in anything.

1 Like

You can’t specify indexes for arrays.


You actually can.

Making a dictionary and indexing them numerically will allow you to iterate through it in order.

local dictionary = {
	[1] = 5,
	[2] = 10,
	[3] = 12
}

for _,v in ipairs(dictionary) do
	warn(v) -- 5, 10, 12
end

But if you replace the indexes with something that isn’t a number, order will be lost and you will have to use pairs which is used for dictionaries (while ipairs is used for arrays)

local dictionary = {
	["test1"] = 5,
	["test2"] = 10,
	["test3"] = 12
}

for _,v in pairs(dictionary) do
	warn(v) -- 12, 5, 10 (there is no order)
end
2 Likes

Would a order in a table be considered an “index”?

like

Table = {1, 2, 3, 4, 5, 6,, 7, 8, 9, 10}
1 Like

Index is just the position in the table of a value. Or if it’s a dictionary I call it a key.

1 Like

Nope, that’s the value. Arrays automatically order the indexes of values based on their position.

1 Like

But would the value be the same as the index in the way I ordered it? If not, how do arrays order indexes?

1 Like

The value that you put in your array has a position called “index”, if it’s the third value in your array then it will have index 3. If you want to access this data then just do Table[3] and it will return the value on that position in your array.

1 Like

Let me visualize it for you.

local array = {
	5,
	7, 
	10,
	"string",
	"cool"
}
local array = {5, 7, 10, "string", "cool"}

In both examples, 5 would be the first index, 7 would be the second index, and so on and so forth.

2 Likes

I think a better way to explain this is that 5 is the value at index 1, 7 is the value at index 2, etc. Might confuse him to think that the array starts at index 5

4 Likes