iceman72
|
  |
| Joined: 08 Jun 2008 |
| Total Posts: 786 |
|
|
| 06 Jun 2015 10:21 PM |
Ok, so #1 Whats the best way to completely clear an array so when you use #myArray it returns 0
We are going strong! #2 Can you easily add on to an array without knowing its size or is myArray[#myArray+1] = "stuff" the best way to do it?
I think we got this! #3 In pretty sure I know this already, but in lua we don't need to define how large an array is before we use it do we?
Yea I knew we could do it! Thank you very much! |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
|
| 06 Jun 2015 10:22 PM |
1.
local array = {}
2. table.insert(array, "blah")
3. no |
|
|
| Report Abuse |
|
|
|
| 06 Jun 2015 10:22 PM |
1. arrayName = {}
2. table.insert(arrayName,data)
3. You don't have to define how large it will be. |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
| |
|
|
| 06 Jun 2015 10:23 PM |
i blame my broken right pinky.
broke it while playing basketball
cri |
|
|
| Report Abuse |
|
|
iceman72
|
  |
| Joined: 08 Jun 2008 |
| Total Posts: 786 |
|
|
| 06 Jun 2015 10:31 PM |
| Thank you very much rayk999 and LegitimatlyMe! |
|
|
| Report Abuse |
|
|
|
| 06 Jun 2015 10:31 PM |
> local array = {}
Don't use this to clear an already existing array. |
|
|
| Report Abuse |
|
|
|
| 06 Jun 2015 10:33 PM |
| And by that I mean, don't include the word "local". |
|
|
| Report Abuse |
|
|
|
| 06 Jun 2015 10:33 PM |
| Got a better solution? I use that method all the time. Or were you simply referring to the scope? |
|
|
| Report Abuse |
|
|
|
| 06 Jun 2015 10:34 PM |
| Oh got it, my bad, thought there was an "official" way to clear tables or something. |
|
|
| Report Abuse |
|
|
|
| 06 Jun 2015 10:38 PM |
| Local causes it to only clear within that scope. So unless you're clearing it in the same scope it was originally defined, it won't clear the actual table, and thus will cause annoying behavior. |
|
|
| Report Abuse |
|
|
iceman72
|
  |
| Joined: 08 Jun 2008 |
| Total Posts: 786 |
|
|
| 06 Jun 2015 10:43 PM |
| Hmm, really? that's kinda odd if it just clears it in that scope. I thought roblox tables where just pointers and that there was only one possible piece of data for each slot in a table no matter the scope. |
|
|
| Report Abuse |
|
|
|
| 06 Jun 2015 10:45 PM |
local thing = {} thing[#thing+1] = "CORNDOG"
This is the code that is inside table.insert() |
|
|
| Report Abuse |
|
|
|
| 06 Jun 2015 10:53 PM |
It doesn't have anything to do with the arrays. It has to do with the way Lua variables work.
What local does is it creates a new variable that is accessible only within the scope it was created and 'child' scopes. To set an already existing variable, you use the name *without* a local and it will search for the most recently created (lowest scope possible) variable with that name, creating a global variable if it doesn't exist. But if you *do* add "local", Lua will create a new local variable anyway and disregard any other variables. From that scope on, it is only possible to get that variable using that name, you can't get the outer one (without getfenv). In essence, you actually can have 2 variables with the same name, but one of them won't be accessible.
The same thing happens here. I actually wrote it wrong- it doesn't clear anything, it creates a new empty table. And once the scope it was created in ends, it will go right back to the way it was before.
local tab = {"Item", "Item2"} if true then local tab = {} --This creates a new table, but the first one isn't overridden because of scope print(tab[1]) end print(tab[1])
> nil > Item
Getting rid of the 'local' when resetting will make it work properly, because then it knows to find the already existing one before creating a new variable. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 06 Jun 2015 10:54 PM |
| It's not actually clearing it, in case no one mentioned that, but its creating a new one :) (the GC clears the old one up so who cares) |
|
|
| Report Abuse |
|
|
iceman72
|
  |
| Joined: 08 Jun 2008 |
| Total Posts: 786 |
|
|
| 06 Jun 2015 11:05 PM |
| O cool! The more you know, Thanks guys, It makes scene! |
|
|
| Report Abuse |
|
|
|
| 07 Jun 2015 12:21 AM |
| this has probably already been said, but putting "local" in front is just redefining the variable in that scope |
|
|
| Report Abuse |
|
|