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: why use local variables?

Previous Thread :: Next Thread 
robloxman303030 is not online. robloxman303030
Joined: 24 Oct 2012
Total Posts: 171
22 Feb 2017 08:40 PM
why use local? when i dont use it it still works ?


Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
22 Feb 2017 08:41 PM
scope limitation, memory management, quicker to access
they're better than globals in every way


Report Abuse
morash is not online. morash
Joined: 22 May 2010
Total Posts: 5834
22 Feb 2017 08:42 PM
To protect scope. Of course, if you want to trample all over your global namespace, that is fine too.
Report Abuse
Revisional is not online. Revisional
Joined: 02 Aug 2014
Total Posts: 318
22 Feb 2017 08:44 PM
Local variables allow multiple functions to reference a variable with the same name without affecting the same instance.
Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
22 Feb 2017 08:45 PM
"Local variables allow multiple functions to reference a variable with the same name without affecting the same instance."

but if that's desired then you can use forward declaration to mimic globals


Report Abuse
EvanHolt is not online. EvanHolt
Joined: 06 Sep 2008
Total Posts: 1264
22 Feb 2017 09:42 PM
Because.

[ George Orwell taught me math! 2 + 2 = 5 ]
Report Abuse
Bobby_Darin is not online. Bobby_Darin
Joined: 05 Jan 2013
Total Posts: 5587
22 Feb 2017 09:54 PM
Simply put, local variables are encapsulated by scope and so they may be reused when the scope ends, so in most cases it is favorable to use local variables over global variables.

Additionally, they are also faster to read and write to as they are stored on the Lua stack (along with function parametetrs), where as global variables are stored in the enclosing function's environment, which is a regular Lua table.

Lua has an default maximum number of 200 local variables per scope.


You should also read the page at this link:
lua
.org/
pil/4.2
.html
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