generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Few questions about arrays

Previous Thread :: Next Thread 
iceman72 is not online. 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 is not online. 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
LegitimatlyMe is not online. LegitimatlyMe
Joined: 30 Oct 2013
Total Posts: 1108
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 is not online. rayk999
Joined: 18 Feb 2011
Total Posts: 4705
06 Jun 2015 10:23 PM
haha ninja'd
Report Abuse
LegitimatlyMe is not online. LegitimatlyMe
Joined: 30 Oct 2013
Total Posts: 1108
06 Jun 2015 10:23 PM
i blame my broken right pinky.

broke it while playing basketball

cri
Report Abuse
iceman72 is not online. iceman72
Joined: 08 Jun 2008
Total Posts: 786
06 Jun 2015 10:31 PM
Thank you very much rayk999 and LegitimatlyMe!
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
06 Jun 2015 10:31 PM
> local array = {}

Don't use this to clear an already existing array.
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
06 Jun 2015 10:33 PM
And by that I mean, don't include the word "local".
Report Abuse
AlexanderLua is not online. AlexanderLua
Joined: 08 May 2015
Total Posts: 92
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
AlexanderLua is not online. AlexanderLua
Joined: 08 May 2015
Total Posts: 92
06 Jun 2015 10:34 PM
Oh got it, my bad, thought there was an "official" way to clear tables or something.
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
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 is not online. 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
MrJoeyJoeJoey is not online. MrJoeyJoeJoey
Joined: 20 Aug 2011
Total Posts: 20787
06 Jun 2015 10:45 PM
local thing = {}
thing[#thing+1] = "CORNDOG"

This is the code that is inside table.insert()
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
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 is not online. 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 is not online. 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
ForeverDev is not online. ForeverDev
Joined: 04 Oct 2008
Total Posts: 13300
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image