|
| 21 Aug 2016 05:44 PM |
I understand that it's best to use local variables whenever possible, but I'm a little confused on if you should use them if they're at the top of the script. Since the variable wouldn't be in a function or anything, wouldn't it be able to be accessed from anywhere? Does making it local make a difference?
I'm mostly asking this because when you don't make a variable local when it's in a function, it gets a blue underline and tells you to consider making it local. If making it local when it's not in a function makes a difference, why doesn't it get underlined?
|
|
|
| Report Abuse |
|
|
|
| 21 Aug 2016 05:46 PM |
Local variables are only available after they are declared and their value will stay the same depending on what scope you use it in
local moop = "hi"
function set(text) local moop = text print(moop) end
set("Hello")
print(moop)
--any time you declare a local variable, it will only be usable in the area that it is inside |
|
|
| Report Abuse |
|
|
2eggnog
|
  |
| Joined: 08 Nov 2008 |
| Total Posts: 1351 |
|
|
| 21 Aug 2016 05:47 PM |
Local variables are faster to read/write and it's harder for exploiters to steal the values of local variables.
|
|
|
| Report Abuse |
|
|
|
| 21 Aug 2016 05:53 PM |
I was talking about performance: http://wiki.roblox.com/index.php?title=Performance#Use_Local_and_avoid_globals
If I were to make a simple script like this:
num = 5
function add(x) return x+1 end
print(add(num))
Would making 'num' local be better, or not make a difference at all?
|
|
|
| Report Abuse |
|
|
|
| 21 Aug 2016 05:54 PM |
would have no difference unless you used something that called num beforehand
and it will be easier to change via exploits |
|
|
| Report Abuse |
|
|
| |
|
2eggnog
|
  |
| Joined: 08 Nov 2008 |
| Total Posts: 1351 |
|
|
| 21 Aug 2016 05:59 PM |
It would make a difference: It'd be slightly faster.
|
|
|
| Report Abuse |
|
|
platin_m
|
  |
| Joined: 25 Jul 2016 |
| Total Posts: 169 |
|
|
| 21 Aug 2016 06:03 PM |
@2eggnog
the difference isn't important though? as long as you place the variable before your chunk of code (that uses that particular variable), nothing is going to change. |
|
|
| Report Abuse |
|
|
2eggnog
|
  |
| Joined: 08 Nov 2008 |
| Total Posts: 1351 |
|
|
| 21 Aug 2016 06:06 PM |
The difference CAN be important. In situations where performance matters, such as math-intensive calculations, there can end up being a noticeable difference. Just use local variables.
|
|
|
| Report Abuse |
|
|
platin_m
|
  |
| Joined: 25 Jul 2016 |
| Total Posts: 169 |
|
|
| 21 Aug 2016 06:08 PM |
@2eggnog
i mean, if someone that performance-hungry really needs that small inch of speed, sure. |
|
|
| Report Abuse |
|
|
2eggnog
|
  |
| Joined: 08 Nov 2008 |
| Total Posts: 1351 |
|
|
| 21 Aug 2016 06:16 PM |
It's more than a small inch of speed. There are plenty of situations your code could easily run twice as fast using local over global variables.
Using local variables can only help you.
|
|
|
| Report Abuse |
|
|