4xw
|
  |
| Joined: 01 Dec 2013 |
| Total Posts: 21 |
|
|
| 21 Jul 2014 09:09 PM |
i have seen people use in their scripts do --something end what does "do" do? isnt it the same as not declaring "do" in the first place? |
|
|
| Report Abuse |
|
|
Xephyric
|
  |
| Joined: 26 Mar 2009 |
| Total Posts: 2270 |
|
|
| 21 Jul 2014 09:11 PM |
while blah do
for blah do
Do can not be used by itself. It is used for some loops. |
|
|
| Report Abuse |
|
|
nomer888
|
  |
| Joined: 13 Feb 2010 |
| Total Posts: 551 |
|
|
| 21 Jul 2014 09:12 PM |
"do" can be used to create enclosures for local variables that you may want to protect from the global scope.
do local var = 5 function getvar() return var end end
print(getvar()) -> 5 print(var) -> nil |
|
|
| Report Abuse |
|
|
|
| 21 Jul 2014 09:12 PM |
@X
although kinda pointless, do can be used by itself, as a scope |
|
|
| Report Abuse |
|
|
Xephyric
|
  |
| Joined: 26 Mar 2009 |
| Total Posts: 2270 |
|
|
| 21 Jul 2014 09:13 PM |
Oh, really! Never seen that. Learned something today xD |
|
|
| Report Abuse |
|
|
4xw
|
  |
| Joined: 01 Dec 2013 |
| Total Posts: 21 |
|
|
| 21 Jul 2014 09:14 PM |
| thank you everyone for your help |
|
|
| Report Abuse |
|
|
|
| 21 Jul 2014 09:14 PM |
| Creating a scope using do can be incredibly useful in controlling variables. |
|
|
| Report Abuse |
|
|
Casualist
|
  |
| Joined: 26 Jun 2014 |
| Total Posts: 4443 |
|
|
| 21 Jul 2014 09:18 PM |
The difference is that do
-- something end
Creates a new scope/block. This can be useful for creating local variables that aren't stored in the main scope.
An example would be using temporary values to calculate something but will never use again. Instead of having to nil these values after they are done being useful you can let the language deal with freeing up memory when the block ends.
(function(...) --something end)(args)
Does something similar, but you can pass arguments to your new scope/block. |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 21 Jul 2014 09:19 PM |
| Maybe not the best use but I sometimes use it to group things together, so I can click the arrow thing to collapses it to get it out of my way, I usually remove them when the script is finished though |
|
|
| Report Abuse |
|
|
HuntHello
|
  |
| Joined: 19 Jan 2011 |
| Total Posts: 577 |
|
|
| 21 Jul 2014 09:21 PM |
| @128 just don't declare local variables in it if it will be treated as a global one in "do". |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 21 Jul 2014 09:38 PM |
@Hunt I made variables local whenever they can be I just comment out the do's before testing then take them out completely when the script is dkne |
|
|
| Report Abuse |
|
|
HuntHello
|
  |
| Joined: 19 Jan 2011 |
| Total Posts: 577 |
|
| |
|