×
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
    (added variables section (mind the alt; forgot to transfer my password manager's vault file again))
    (finished variables and scope section)
    Line 59: Line 59:
    This means everything's working! You're now ready to start coding. If this isn't the case, double check you followed all of the instructions to the letter.
    This means everything's working! You're now ready to start coding. If this isn't the case, double check you followed all of the instructions to the letter.


    What just ran is the print function - one of the simplest '''functions''' there is. You'll learn more about what a function is later, but for now know that it's basically a black box that does something with whatever you put between its parentheses - this is like its mouth into which you feed it values. In this case, the value you're feeding it is some text - a '''string''' (again, you'll learn more about these later). What the print function does is that it prints out (displays in the output) the text you gave it. In this case - Hello world!
    What just ran is the print function - one of the simplest '''functions''' there is. You'll learn more about what a function is later, but for now know that it's basically a black box that does something with whatever you put between its parentheses - which are like the function's mouth into which you feed it values. In this case, the value you're feeding it is some text - a '''string''' (again, you'll learn more about these later; for now remember the name and that you create one by putting some text in between quotation marks, e.g. "text"). What the print function does is that it prints out (displays in the output) the text you gave it. In this case - Hello world!




    Line 67: Line 67:
    The code before you currently only occupies a single line. Copy it, press enter, then paste it.
    The code before you currently only occupies a single line. Copy it, press enter, then paste it.


    You should now see the following two lines of code:<syntaxhighlight lang="lua">
    You should now see the following two lines of code:<syntaxhighlight lang="lua" line="1">
    print("Hello world!")
    print("Hello world!")
    print("Hello world!")
    print("Hello world!")
    Line 73: Line 73:




    Next, modify the code on the second line so it prints out <code>Hi World!</code>:<syntaxhighlight lang="lua">
    Next, modify the code on the second line so it prints out <code>Hi World!</code>:<syntaxhighlight lang="lua" line="1">
    print("Hello world!")
    print("Hello world!")
    print("Hi world!")
    print("Hi world!")
    Line 81: Line 81:


    You're now ready to move on onto the first real lesson: variables.
    You're now ready to move on onto the first real lesson: variables.
    ==== Variables ====


    ==== Variables, scope ====
    Imagine you want to store some information for future use: say your friend needs it later so they can do something. Say you write that information on a slip of paper.


    Say you want to store some information for future use; let's say that your friend later needs it so they can do something and you can't tell them it directly. Assume that you decide to write that information on a slip of paper.
    Also imagine that you have an almost limitless number of really small boxes at your disposal. This is the computer's memory in this analogy.


    Now imagine that you also have an almost limitless number of really small boxes at your disposal. This is the computer's memory in this analogy.
    You then decide to place your slip of paper into the nearest currently unused box.

    You can't just place your slip of paper anywhere as it'd get lost, so you decide to place your slip of paper into the nearest currently unused box.


    But how can your friend know which of those boxes to check for the slip of paper?
    But how can your friend know which of those boxes to check for the slip of paper?
    Line 100: Line 101:
    Let's see how that translates into code; it turns out it's really simple.
    Let's see how that translates into code; it turns out it's really simple.


    Delete any code you have in the script you're using to test your code, then add the following:<syntaxhighlight lang="lua">
    Delete any code you have in the script you're using to test your code, then add the following:<syntaxhighlight lang="lua" line="1">
    local x = 3
    local x = 3
    print(x)
    print(x) --prints out 3
    </syntaxhighlight>
    </syntaxhighlight>
    Let's break this down line by line:
    Let's break this down line by line:
    Line 109: Line 110:
    ** <code>x</code> is the name of the variable
    ** <code>x</code> is the name of the variable
    ** <code>=</code> assigns the value of the variable to be whatever is on the other side of it
    ** <code>=</code> assigns the value of the variable to be whatever is on the other side of it
    ** <code>3</code> is the value you're assigning to the variable
    ** <code>3</code> is the value you're assigning to the variable. In this case, it is a number.
    * <code>print(x)</code> prints the value of the variable
    * <code>print(x)</code> prints the value of the variable
    * <code>--prints out 3</code> is a comment - an annotation to source code. It's always at the end of a line and is created by using two dashes (-). It is ignored when running the code.



    Try playing around a bit with this code by changing the value of the variable: perhaps by changing it to another number, or perhaps to a string ("Hello world!", for example).

    You may notice that what <code>local</code> does wasn't defined. <code>local</code> sets the '''scope''' of a variable. '''Scope''' (or '''visibility''') is a property of variables which defines where (that is in which '''scope blocks''') their name binding is valid (so, if <code>a</code> is equal to 1, where <code>a</code> being equal to 1 is the case). Let's look at an example to make this clearer, and break it down line by line as before:<syntaxhighlight lang="lua" line="1">
    local a = 3

    do
    print(a) --prints out 3
    local x = 4
    a = 5
    print(x) -- 4
    print(a) -- 5
    end

    print(a) -- 3
    print(x) -- nil
    </syntaxhighlight>

    * <code>local a = 3</code> sets a 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>
    *** 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.
    * <code>do .. end</code> creates a new '''scope block'''. <code>do</code> starts it and <code>end</code> ends it.
    * <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>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>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 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(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.




    Feel free to play around a little with what you've just learned. Learning by doing is the best way to learn. If anything's been left slightly unclear, try going over everything again once more; perhaps slower. Once you feel you're ready, please take the following quiz to check your comprehension:
    <quiz display=simple>
    {Quiz test
    |type="()"}
    + The correct answer.
    - Wrong or misleading answer.
    - Wrong or misleading answer.
    - Wrong or misleading answer.
    </quiz>


    Try playing around a bit with this code by changing the value of the variable: perhaps by changing it to another number, or perhaps a string ("Hello world!", for example).


    You may notice that what <code>local</code> does wasn't defined.


    - types
    - types
    Cookies help us deliver our services. By using our services, you agree to our use of cookies.

    Recent changes

  • Axenori • 4 days ago
  • Voivsone • 4 days ago
  • Axenori • 4 days ago
  • Axenori • 5 days ago
  • Cookies help us deliver our services. By using our services, you agree to our use of cookies.