Jump to content

[QUESTION] EMPTY TABLE


Furzy

Recommended Posts

What do you mean? Why is the variable initialized with an empty table? That's so that the indexing operator [] works. It doesn't work on any other value than a table (except custom classes with a metatable).

example[1] = 7 -- error: attempt to index a nil value

-- so we initialize it because [] doesn't work on nil values

example = 1
example[1] = 7 -- error: attempt to index a number value

-- but [] doesn't work on numbers either. it only works on tables, 
-- so we must initialize a table, and because we don't want anything 
-- inside it, we initialize it as an empty table

example = {}
example[1] = 7 -- works

 

Edited by MrTasty
Link to comment
3 minutes ago, MrTasty said:

What do you mean? Why is the variable initialized with an empty table? That's so that the indexing operator [] works. It doesn't work on any other value than a table (except custom classes with a metatable).


example[1] = 7 -- error: attempt to index a nil value

-- so we initialize it because [] doesn't work on nil values

example = 1
example[1] = 7 -- error: attempt to index a number value

-- but [] doesn't work on numbers either. it only works on tables, 
-- so we must initialize a table, and because we don't want anything 
-- inside it, we initialize it as an empty table

example = {}
example[1] = 7 -- works

 

i mean , why use this, i see in some scripts

 

element = {}

 

function xd()

element[source] = createObject

end

 

i wanna understand why use the table?

 maybe to get object in others functions?

Edited by Furzy
Link to comment

It is a method of keeping references to created objects for example. Let's say you want to allow each player to place one object only. What you'll want to have is a table objects with the player as key and object as value. That way, you can easily check if the player already placed an object (i.e. objects[player] is not a nil value), or destroy it easily (i.e. destroyElement(objects[player])). It associates the key (player) to the value (his/her object), keeping a reference you can use elsewhere in the script. The inverse is not as efficient however, to find the key under which it exists (or player to whom it belongs), you'd have to iterate through the table until you find the value referencing the object you want, and then save its key (player) to a variable. Sometimes you'll find scripters use a two tables, one associating the player to the object and another mirror-image table associating objects to players (but if not careful, these two tables can diverge easily and not have the exactly mirrored pairs)

Edited by MrTasty
  • Thanks 1
Link to comment
13 minutes ago, MrTasty said:

It is a method of keeping references to created objects for example. Let's say you want to allow each player to place one object only. What you'll want to have is a table objects with the player as key and object as value. That way, you can easily check if the player already placed an object (i.e. objects[player] is not a nil value), or destroy it easily (i.e. destroyElement(objects[player])). It associates the key (player) to the value (his/her object), keeping a reference you can use elsewhere in the script. The inverse is not as efficient however, to find the key under which it exists (or player to whom it belongs), you'd have to iterate through the table until you find the value referencing the object you want, and then save its key (player) to a variable. Sometimes you'll find scripters use a two tables, one associating the player to the object and another mirror-image table associating objects to players (but if not careful, these two tables can diverge easily and not have the exactly mirrored pairs)

Thanks ?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...