×
Create a new article
Write your page title here:
We currently have 277 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 deleted Content added
    m finished setup part, except the text editor list
    m revised quiz
     
    (15 intermediate revisions by 2 users not shown)
    Line 17: Line 17:


    === Learning to code ===
    === Learning to code ===

    ==== Getting started ====
    While you may be tempted to start writing code right away you'll need to set up an environment for this first. This is what's called a '''development environment'''.
    While you may be tempted to start writing code right away you'll need to set up an environment for this first. This is what's called a '''development environment'''.


    As you'll just be writing some basic [https://luau-lang.org luau] (a dialect of the [https://lua.org/ lua]) throughout this tutorial, this will consist of just a '''text editor''' (and something to run your code with). A text editor can be as simple as just a notepad program so you have somewhere to jot down your code, or can be more complex and specialized for writing code. These types of text editors (code editors) often offer useful features such as automatic code completion and syntax highlighting (it colour-codes smaller bits of code, conveying their meaning or data type). A few suggestions for a code editor are listed below, complete with pros, cons, images, and links.
    As you'll just be writing some basic [https://luau-lang.org luau] (a dialect of [https://lua.org/ lua]) throughout this tutorial, this will consist of just a '''text editor''' (and something to run your code with). A text editor can be as simple as just a notepad program so you have somewhere to jot down your code, or can be more complex and specialised for writing code. These types of text editors (code editors) often offer useful features such as automatic code completion and syntax highlighting (it colour-codes smaller bits of code, conveying their meaning or data type). A few suggestions for a code editor are listed below, complete with pros, cons, images, and links.


    * (the list)
    * (the list)




    Once you've set up a code editor, there's one more thing to do before you can start coding: setting up a '''runtime environment'''. As lua is a '''scripting''' language - a programming language in which the instructions to the computer are stored as '''source code''' (the code as you wrote it, in textual format) - it must be translated to the computer in real time because computers can only understand binary (0s and 1s). This is what a runtime environment is for (in this case); it allows for the execution of '''scripts''' (source code) by translating them to '''machine code''' (instructions understandable to the computer) on the go.


    Once you've set up a text editor, there's one more thing to do before you can start coding: setting up a '''runtime environment'''. As lua is a '''scripting''' language - a programming language in which the instructions to the computer are stored as '''source code''' (the code as you wrote it, in textual format) - it must be translated to the computer in real time because computers can only understand binary (0s and 1s). This is what a runtime environment is for (in this case); it allows for the execution of '''scripts''' (source code) by translating them to '''machine code''' (instructions understandable to the computer) on the go.
    In this case, you'll need something that can run luau code. This guide recommends that you use Roblox Studio for these purposes. Using Studio to test your code isn't exactly the best, but studio is approachable for a beginner such as yourself. Besides, there aren't really any other options anyways.


    In this case, you'll need something that can run luau code. This guide recommends that you use Roblox Studio for these purposes. Using Studio to test your code isn't exactly the best, but studio is approachable for a beginner such as yourself. Besides, there aren't really any other options anyways. Later on, you'll switch from using Studio to testing code on [[Microcontroller|Microcontrollers]] in-game.
    For now, do the following:



    To set up Studio for testing your code, do the following:


    * Install Roblox Studio if necessary.
    * Install Roblox Studio if necessary.
    Line 40: Line 45:
    ** If you don't have an output window, click the 'View' tab at the top of your screen and then click the 'Output' button to turn the window on
    ** If you don't have an output window, click the 'View' tab at the top of your screen and then click the 'Output' button to turn the window on
    * Resize the output window to be a bit bigger than by default if you have the screen space for it.
    * Resize the output window to be a bit bigger than by default if you have the screen space for it.
    * In the Explorer, hover over the 'ServerScriptService' and click the plus (+) icon beside it. Then, add a script to it (a regular script: make sure it's not a LocalScript or ModuleScript).
    * In the Explorer, hover over the 'ServerScriptService' and click the plus (+) icon beside it. Then, add a script to it (a regular script: make sure it's not a LocalScript or ModuleScript).<br />





    Line 55: 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 - 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!
    - Use this as an introduction to basic code flow



    When you started reading your language for the first time, you needed to learn in what order to read the words before you. Code is read from left to right, line by line.

    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" line="1">
    print("Hello world!")
    print("Hello world!")
    </syntaxhighlight>


    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("Hi world!")
    </syntaxhighlight>Run this code. The output should be <code>Hello World!</code> and <code>Hi World!</code>; one after the other.

    This should give you a good idea of how code flows if you haven't already figured it out.

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

    ==== Variables, scope ====

    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.

    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 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?

    The solution is to also label the box into which you place the slip of paper with a name specific to it: the entire box including its label is what's called a '''variable:''' a symbolic name (what you labeled the box with) associated with a storage location (the actual box itself).

    The information inside the box is the '''value''' of the variable. This value can be whatever, and can change with time.

    It's key to note that the box in this analogy is opaque. You can only know what's inside the box if you open it.


    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" line="1">
    local x = 3
    print(x) --prints out 3
    </syntaxhighlight>
    Let's break this down line by line:

    * <code>local x = 3</code> defines some variable x to be equal to 3
    ** <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>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>--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
    local a = 5
    print(x) -- 4
    print(a) -- 5
    end

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

    * <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>
    *** 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>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(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> 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'''. <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.



    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">
    {Which of the following defines <code>a</code> to be equal to the number 4; in the local scope?
    |type="()"}
    - a = 4
    || This defines <code>a</code> globally - not locally (or reassigns a to some other value).
    - do a = 4 end
    || This defines <code>a</code> globally (not locally), and within a scope block.
    + local a = 4
    - local a = "4"
    || This defines <code>a</code> locally, but as "4" (a string), not 4 (a number).

    {Defining variables in the local scope is always preferred
    |type="()"}
    + TRUE
    || Yup! Always defining variables in the local scope is less memory intensive and makes you less prone to mistakes
    - FALSE
    || Always defining variables in the local scope is less memory intensive and makes you less prone to mistakes

    {What will the following code print out? <syntaxhighlight lang="lua" line="1">
    local hello = "Hello world!"
    print(hello)

    </syntaxhighlight>
    |type="()" coef="2"}
    - hello
    || <code>hello</code> is a variable, so what's actually being fed into the print function is its value - "Hello world"
    + Hello world!
    - "Hello world!"
    || "Hello world" is indeed what is fed into the print function, but not what is printed out
    - 'hello'
    || <code>hello</code> is a variable, so what's actually being fed into the print function is its value - "Hello world"

    {Consider the following code: <syntaxhighlight lang="lua" line="1">
    local b = 2
    local p = 3

    do
    b = 5
    print(p)
    end

    print(b)
    </syntaxhighlight>
    The code will print out:
    |type="()" coef="2"}
    - 5, then 2
    || You seem to have mixed up the variables.
    - 2, then 3
    || You seem to have mixed up the variables.
    + 3, then 5
    - 3, then 2
    || Scope isn't relevant here as it's just reassigning the value of a variable - and b is defined in the top scope. To have the code print this out, <code>b = 5</code> would have to be replaced with <code>local b = 5</code> (because when we have that local at the beginning - <b>scope becomes relevant</b>)

    { Complete the missing lines:<syntaxhighlight lang="lua" line="1">
    local c = 32
    local s = c

    do
    c = 54
    do
    ...
    ...
    ...
    print(s)
    end
    end
    </syntaxhighlight>
    If the output of the code was '''32''', then '''54''', then '''24'''.


    |type="{}" coef="4"}
    - variables, global vs local
    First missing line: { print(s) }
    Second missing line: { print(c)|s=24| s = 24|local s=24|local s = 24 }
    Third missing line: { s=24| s = 24|local s=24|local s = 24|print(c) }
    </quiz>


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

    Recent changes

  • Jo857294 • 9 days ago
  • 116.111.185.163 • 21 days ago
  • 116.111.185.163 • 21 days ago
  • 116.111.185.163 • 21 days ago
  • Cookies help us deliver our services. By using our services, you agree to our use of cookies.