What is the # (fence) in the Lua language for?

Asked

Viewed 212 times

4

I’m watching this one tutorial about creating a game and came across something I didn’t understand about the language Lua.

There is an excerpt from the code where there is the following expression:

ents.objects[#ents.objects]

What the # (cerkeet) means in that expression?

2 answers

3


It is the operator of length, the length. In this case you’re taking the last element of array ents.objects. Remember that in Lua the arrays start at 1 and not 0. It is accessed by the meta method __len.

  • I’m learning lua now. I found it simpler to write games (in relation to python). And cleaner compared to PHP. This last comparison is said to be unfortunate

  • I like the moon a lot but I’ve never been able to move much.

3

It’s used to take the size of a chart. Remembering that this may not work as expected because the table in lua is a structure that, under the cloths, is implemented in a hybrid way that contains a hash table and an array part. # will take the size of the array part, and you can only be sure that it will return the right size when a table is used as an array, i.e.: a table has to have only numeric indexes, starting from 1, without "holes" in the middle. Otherwise, to obtain the quantity of elements, it is preferable to iterate and count.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.