|
| 18 Mar 2015 11:52 AM |
Hello guys,
Over numerous forum posts, I have seen people saying they make a "slight" difference. So, I was wandering what's the difference between the following things: 1. Putting a semi-colon at the end of a line; example:
local Part = game.Workspace:WaitForChild("Part");
2. Putting a local in front of a variables; example:
local Part = game.Workspace.Part
3. Putting a local in front of a function; example:
local function()
end
What differences does these things make? Is it worth doing?
|
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 11:57 AM |
Putting local in front of variables and functions defines a smaller scope that the code has to execute "in." It makes the whole process faster, and if you don't need variables outside of the scope you are using then in I would definitely suggest putting local in front of those variables.
Putting semicolons at the end of your lines technically makes the code faster, but hardly so. The primary reason why people put semicolons at the end of their lines is because it is a good habit to get into if you plan on coding outside of ROBLOX, as many languages require you to end you lines with semicolons. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 11:58 AM |
| that the variable has to be used in* |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:00 PM |
| Hmm, ok, thanks for the help. I am hoping to start coding outside of ROBLOX, so I will be trying to get into the habit of putting semi-colons. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:05 PM |
| What about local functions though? You mentioned it, but is it worth putting it in front? |
|
|
| Report Abuse |
|
|
| |
|
|
| 18 Mar 2015 12:09 PM |
1: Not required at all, but other languages like C++ require semi colons to end lines. Lua is much more flexible and does not need these, lines of code can be shoved into one giant line and still run the same!
2: local means that the variable can only be accessed from descendants of the declaration spot. If you have this:
function a() local num = 1337 end
function b() print(num) end
a() b()
.. you will get an error saying num isnt a declared variable or something like that. num could only be accessed from a() and code inside it. It would take too long to explain more, so look at it on the wiki.
3: Same thing as 2. Except that its a function that can only be called by descendant code.
function a() local function b() print("Luna is best pony") end) end
a() b()
.. would call an error because b() is a function that only exists inside a()! Locals are usually used to separate conflicting variables with the same names but in different functions.
|
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:21 PM |
I'm almost positive that making a variable or function local doesn't necessary make it faster. Localization just means that the function can only be called from a certain section of the code. For example..
function Begin() local function Hello() end end
Hello() could only be called within the function Begin(). I've always been told that localization helps more with organization than anything.
Semicolons and colons make no difference at all. I have seen so many argument for and against them, but the fact of the mater: it takes the Lua compiler no extra time determining where the end of a line is because it compiles everything into one big piece of code. Lua doesn't have to be written in lines. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:22 PM |
| Colons make a difference though. A really big difference... |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:25 PM |
| @Half: Access to local variables is faster than access to global ones. It is also generally advisable to keep from cluttering the "global environment." |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:26 PM |
| Ok. Thanks for the help. @Above, how do they make a "big" difference? |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:27 PM |
| I'm also nearly positive that semicolons to speed up the process, however infinitesimally. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:29 PM |
Lua doesn't use lines .-.
What you say about using local variable/functions is partly true, but it's mostly for organization. Any change in speed isn't even practically noticeable. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:31 PM |
@bloo, I didn't mean colons. I was only addressing OP and didn't overlooked what I was writing.
Colons make huge differences.. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:31 PM |
| There really isn't any reason not to use local variables whenever you can. For me it is worth the extra half-second spelling out "local" in order to keep your code clean and more efficient. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:31 PM |
"Putting a semi-colon at the end of a line" It makes no difference.
"Putting a local in front of a variables" It is faster to read/write to them (IIRC) and it is much more "proper".
"Putting a local in front of a function" Functions are variables. There is no difference. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:32 PM |
| Locals are used for organization AND speed! They prevent global clutter and speed up the time it takes to retrieve data; its faster to access a local than to get a global. However, the difference is so insignificant that it wouldnt be noticeable until a script has been either running for a long time or has a gigantic amount of variables! |
|
|
| Report Abuse |
|
|
| |
|
| |
|
|
| 18 Mar 2015 12:35 PM |
The speed of the local keyword is EXTREMELY over-rated. Don't feel bad if you don't make global variables defined as local. Only always make local variables local unless you want to otherwise.
The speed argument is just awful. I've run some bench-marking tests.
The only time local would be necessary is when calling a loop / function thousands upon thousands of times combined with losing a fraction of a second would matter to you. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:36 PM |
Yes, but locals are also garbage-collected so it saves memory.
And it is more "proper". Name a language outside of Lua that uses non-local variables by default. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:37 PM |
"Colons make huge differences.."
Yes, in switch statements which aren't in Lua!
Semi-colons are not just optional, they're only included for consistency for people who use other languages a lot or think it looks better. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:38 PM |
| In conclusion, however, locals and globals dont usually make a difference. It ALL depends on how a variable is used, and its the scripters job to decide how a variable, function or anything else should be initialized. Summary: 11th grade Pre Calculus is really boring and Im not learning anything in school. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:41 PM |
Yes but local variables are more "proper".
Also, there is a limit (200 I think?) on how many local variables you can use. |
|
|
| Report Abuse |
|
|
|
| 18 Mar 2015 12:57 PM |
| Besides, we are getting off topic. Its all about access throughout the script. |
|
|
| Report Abuse |
|
|