SurKipper
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 435 |
|
|
| 25 May 2016 12:10 PM |
Is this:
local function DoStuff() end
Different from this?:
function DoStuff() end |
|
|
| Report Abuse |
|
|
|
| 25 May 2016 12:18 PM |
The local keyword tells the game that the function should only be accessible from the current scope. So if you define a function as local inside of an if block, nothing outside that if block will be able to call the function, however code inside the if block will be able to call the function.
The purpose of this is to make code more organized and prevent overlap and scope leaking. |
|
|
| Report Abuse |
|
|
|
| 25 May 2016 12:19 PM |
Yes. Local variables are different. Functions are just variables, so it applies.
Local variables are faster to read from and write to, and they are also more "Proper". Most languages use this kind of variable by default.
It doesn't work outside of the scope it was declared in: #code if 1 == 1 then local Result = "Yay, 1 = 1!" end print(Result) --> nil
You get around this by forward-declaring: #code local Result -- Now the code outside of the if statement will have access to it. if 1 == 1 then Result = "Yay, 1 = 1!" -- This changes the variable we declared earlier. end print(Result) --> Yay, 1 = 1!
|
|
|
| Report Abuse |
|
|
|
| 25 May 2016 12:20 PM |
My mistake, functions are data types, not variables. They are usually assigned to variables though.
Also, local variables are garbage collected better, making it even more efficient.
|
|
|
| Report Abuse |
|
|
SurKipper
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 435 |
|
|
| 25 May 2016 02:11 PM |
| I knew about local variables and scopes, I just had never seen anyone use a local function until I looked at the classic rocket launcher code. |
|
|
| Report Abuse |
|
|
2eggnog
|
  |
| Joined: 08 Nov 2008 |
| Total Posts: 1351 |
|
|
| 25 May 2016 02:18 PM |
You would also use them if you want to dynamically create functions. For example, if you were making a simple iterator to use in a for loop, you'd want it to create a local function with its own local counter variable that cannot be used anywhere else.
|
|
|
| Report Abuse |
|
|
|
| 25 May 2016 03:30 PM |
"Also, local variables are garbage collected better, making it even more efficient." Local variables are not GC'd, they're automatically freed when the function ends. However what the variable references isn't, I highly doubt it's any different from setting a global variable to nil. |
|
|
| Report Abuse |
|
|
|
| 25 May 2016 03:31 PM |
"However what the variable references isn't" Me implying things like userdata, tables, closures, etc. Obviously stuff like numbers/booleans will cease to exist |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 25 May 2016 03:33 PM |
Its really just about the scope. If you want to use a function anywhere in the script, keep it global, else just use a local function within a function if you dont need to use it any where else in the script
function hi() end
function hello()
end
hi() hello()
rather than
function hi() local function hello() end end
hello() will error because it isnt in the same scope
|
|
|
| Report Abuse |
|
|
|
| 25 May 2016 03:35 PM |
"scope. If you want to use a function anywhere in the script, keep it global, else just use a local function " No. You keep it local but just don't put it inside another function like a buffoon. |
|
|
| Report Abuse |
|
|
2eggnog
|
  |
| Joined: 08 Nov 2008 |
| Total Posts: 1351 |
|
|
| 25 May 2016 04:02 PM |
It's generally a good idea to keep variables and functions local. Local objects are stored on the stack, which can be accessed faster than global objects stored in the memory.
|
|
|
| Report Abuse |
|
|
|
| 25 May 2016 04:03 PM |
local variables are stored on the stack, not local objects. That implies the object the variable is referencing is on the stack when it's not going to ever be (of course not including numbers,nils,booleans since they're not objects).
|
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 25 May 2016 04:06 PM |
@cntkillme
there is no need to use a local function is the global scope. lol
that also looks ugly as f and terribly wrong
|
|
|
| Report Abuse |
|
|
2eggnog
|
  |
| Joined: 08 Nov 2008 |
| Total Posts: 1351 |
|
| |
|
|
| 25 May 2016 04:09 PM |
"there is no need to use a local function is the global scope. lol" There is, because it's good practice. There's a good rule of thumb everyone should follow when programming: "never use global variables unless you know what you're doing." I'm not going to use this as part of my argument because it's insignificant, but it's faster to access locals than it is globals. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 25 May 2016 04:10 PM |
"its good practice"
I dont know where tf you learned to program but its definitely not good practice to use local functions on the global scope.
|
|
|
| Report Abuse |
|
|
2eggnog
|
  |
| Joined: 08 Nov 2008 |
| Total Posts: 1351 |
|
|
| 25 May 2016 04:11 PM |
yes it is
Even the Lua website does that.
|
|
|
| Report Abuse |
|
|
|
| 25 May 2016 04:13 PM |
| TimeTicks you're way too insecure about yourself to be foruming here. I feel like a hundred people already told you that on the Slack though so no point in trying to knock some sense into your stubborn self. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 25 May 2016 04:13 PM |
i love how you insist on defending flux. its pointless to keep debating because you'll never be convinced.
|
|
|
| Report Abuse |
|
|
2eggnog
|
  |
| Joined: 08 Nov 2008 |
| Total Posts: 1351 |
|
|
| 25 May 2016 04:14 PM |
'you'll never be convinced'
lol
This is hilarious.
|
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 25 May 2016 04:16 PM |
@cntkillme possibly, but thats a really naive assumption to make. I could also say a hundreds things about you which could also be false because I don't know you and neither do you
|
|
|
| Report Abuse |
|
|
|
| 25 May 2016 04:19 PM |
I see. Just throwing this out there because I think it's funny and 'proof-enough' puu dot sh/p54kC/eb5ba20edc |
|
|
| Report Abuse |
|
|
2eggnog
|
  |
| Joined: 08 Nov 2008 |
| Total Posts: 1351 |
|
| |
|
KapKing47
|
  |
| Joined: 09 Sep 2012 |
| Total Posts: 5522 |
|
|
| 25 May 2016 05:47 PM |
TimeTicks, say that when u need efficiency the most :P Also, how the crap is it NOT good practise, of COURSE it is :P
Also, to those that put a local function inside another function, listen to me... stop... just STOP... why in the world would u do that!? Dumb af... I keep ALL the function local AND on the global scope, and I never really have problems with that :P |
|
|
| Report Abuse |
|
|
|
| 25 May 2016 05:50 PM |
@Kap
And... The idiot spotlight turns to you :^(
If someone needs a local function, why the heck are you telling them not to? Not everything belongs in the global scope? |
|
|
| Report Abuse |
|
|