×
Create a new article
Write your page title here:
We currently have 276 articles on Waste Of Space Wiki. Type your article name above or create one of the articles listed here!



    Waste Of Space Wiki

    Programming/Staging: Difference between revisions

    Content added Content deleted
    m (and -> then)
    m (whoops)
    Line 124: Line 124:
    print(a) --prints out 3
    print(a) --prints out 3
    local x = 4
    local x = 4
    a = 5
    local a = 5
    print(x) -- 4
    print(x) -- 4
    print(a) -- 5
    print(a) -- 5
    Line 133: Line 133:
    </syntaxhighlight>
    </syntaxhighlight>


    * <code>local a = 3</code> sets a to be equal to 3, as you should know by now
    * <code>local a = 3</code> sets <code>a</code> to be equal to 3, as you should know by now.
    ** <code>local</code> sets the scope of the variable to local. This means that the variable's binding is only valid within its scope block or in scope blocks within it's scope block. Imagine scope blocks as the folders on your computer (but not quite). Say you have a home folder and a documents folder, both with some files inside of them. If you're in the home folder, you can only access the files inside of it. You'll only be able to see the files within the documents folder if you go into it. However, unlike with real folders, if you're in the documents folder in this case you'll also be able to see everything within the home folder. If scope still confuses you, just know that you should always define variables by using <code>local (...) = (...)</code>
    ** <code>local</code> sets the scope of the variable to local. This means that the variable's binding is only valid within its scope block or in scope blocks within it's scope block. Imagine scope blocks as the folders on your computer (but not quite). Say you have a home folder and a documents folder, both with some files inside of them. If you're in the home folder, you can only access the files inside of it. You'll only be able to see the files within the documents folder if you go into it. However, unlike with real folders, if you're in the documents folder in this case you'll also be able to see everything within the home folder. If scope still confuses you, just know that you should always define variables by using <code>local (...) = (...)</code>
    *** The other option is setting the scope of a variable to global. To set a variable's scope to global, just don't use <code>local</code> before it. This is equivalent to defining a local variable at the top scope (<code>local a = 3</code> in the above example). Local variables should be preferred whenever possible.
    *** The other option is setting the scope of a variable to global. To set a variable's scope to global, just don't use <code>local</code> before it. This is equivalent to defining a local variable at the top scope (<code>local a = 3</code> in the above example). Local variables should be preferred whenever possible.
    Line 139: Line 139:
    * <code>print(a)</code> prints out the value of a. As <code>a</code> already had its value assigned in a higher scope, <code>a</code> is (also) equal to 3 here.
    * <code>print(a)</code> prints out the value of a. As <code>a</code> already had its value assigned in a higher scope, <code>a</code> is (also) equal to 3 here.
    * <code>local x = 4</code> ...
    * <code>local x = 4</code> ...
    * <code>a = 5</code> sets the value of <code>a</code> to 5. As the variable <code>a</code> was already defined, <code>local</code> is no longer necessary (the value is just being changed out).
    * <code>local a = 5</code> sets <code>a</code> to be equal to 5. This is '''<u>NOT</u>''' the same as assigning <code>a</code> a new value (<code>a = 5</code>). This overrides the other <code>a</code> with a new <code>a</code>; equal to 5: not 3, but only in its local scope.
    * <code>print(x)</code> prints out 4 as this is what x was defined as, at least '''in the current scope'''.
    * <code>print(x)</code> prints out 4 as this is what x was defined as, at least '''in the current scope'''.
    * <code>print(a)</code> now prints out 5, as a was defined to be 5 '''in this scope.'''
    * <code>print(a)</code> now prints out 5, as <code>a</code> was defined to be 5 '''in this scope.'''
    * <code>print(a)</code>, now outside the scope block, prints out 3 because <code>a</code> is only equal to 5 within that one specific scope block. Outside of it, <code>a</code> is equal to 3: what it is defined to be in that scope block - the top scope block.
    * <code>print(a)</code>, now outside the scope block, prints out 3 because <code>a</code> is only equal to 5 within that one specific scope block. Outside of it, <code>a</code> refers to the original <code>a</code> and is equal to 3: what it is defined to be in the top scope block.
    * <code>print(x)</code> prints out '''nil. nil''' represents no value - not as in 0, but as in the '''complete absence of anything'''. This is for the same reason why printing <code>a</code> before this gave 3: <code>x</code> is only defined to have a value within its scope block; outside of it, it lacks one.
    * <code>print(x)</code> prints out '''nil. nil''' represents no value - not as in 0, but as in the '''complete absence of anything'''. <code>x</code> is equal to nil for the same reason why printing <code>a</code> before this gave 3: <code>x</code> is only defined to have a value within its scope block; as it wasn't defined in any way outside of it, it lacks one.




    Line 192: Line 192:
    - 2, then 3
    - 2, then 3
    || You seem to have mixed up the variables.
    || You seem to have mixed up the variables.
    - 3, then 5
    + 3, then 5
    - 3, then 2
    || <code>b</code> is equal to 5 only in the scope block; not outside of it.
    || Scope doesn't apply here as it's just reassigning the value of a variable - and b is defined in the top scope.
    + 3, then 2


    { Complete the missing lines:<syntaxhighlight lang="lua" line="1">
    { Complete the missing lines:<syntaxhighlight lang="lua" line="1">
    Cookies help us deliver our services. By using our services, you agree to our use of cookies.

    Recent changes

  • 116.111.185.163 • 3 hours ago
  • 116.111.185.163 • 3 hours ago
  • 116.111.185.163 • 3 hours ago
  • 116.111.185.163 • 4 hours ago
  • Cookies help us deliver our services. By using our services, you agree to our use of cookies.