Programming 101
Lecture 1: In the Very Beginning
Topic list:
- General project outline and getting things rolling.
- Wtf the project files are and what are they meant for.
- Let's get it all running for a moment and print a pretty "HELLO WORLD".
- The main function, what it is, what it does.
- VARIABLES
- What they are. (The core of programming.)
- How to declare, name.
- What values can they be given?
- Let's ignore objects for now, primitives only.
- Numbers. Strings. null.
- Operations on variables. Maths! Literally. Maths.
- FUNCTIONS
- Code is executed SEQUENTIALLY.
- Functions are the same as they are in maths. Including parameters!
- Let's do some debugging magic with introducing a new function.
- Return statement and values.
- Implement some basic maths functions with parameters and return statements.
- Don't worry about stateful code for the time being. Next lecture, when we get to objects, we'll also get to worry about state.
- DM reference manual plug. There are many functions that already exist made ready for you!
- CONTROL STATEMENTS
- if-else-if-else.
- What classifies as truthy? TRUE/FALSE, null, "", 0.
- if-eles-if-else vs if if if.
- Control statements with return. So early returns.
- Make some kind of data validation function that later does maths. Eg. only add if data is in range of [A, B]. Return error otherwise and print shit appropriately.
- if-else-if-else.
Lecture 1: Homework: Making a Calculator
Objective: We make a calculator!
The calculator, as a minimum, should be able to:
- Take two inputs from the user.
- Execute one of the following binary mathematical operations, based on user input:
- Multiplication (a * b);
- Division (a / b);
- Addition (a + b);
- Deduction (a - b).
- Output the result to world (via world << somestuff).
I've taken the liberty of dividing the task up into sections. This should (hopefully) help you better digest it! I've also hidden some spoilers/advice for things that I perceive difficult. If you get stuck, feel free to expand and see if they help out! If not, feel free to pester me via Discord. Or ask in the #lectures channel in discord.
Section 1: User Input
Something we didn't learn how to do was to get input from the user. But we did learn about functions! As I said during the lecture, DM comes with a lot of ready-made functions. And the one we use to get input from the user is called, you'll never guess it, input()!
Here's a usage example:
/proc/main() var/a = input("Please input a number for A") as num
The "as num" component will ensure that the input functions returns a number, otherwise you might get a piece of text, and we all remember what happened when we tried to do "42 + "a b c"". (Bad things.) The DM reference article describing the usage of input can be found here.
Beyond numbers A and B, there's one more thing the user must select: what operation to do. This will still require the input() proc, but we need to decide how the user will communicate their wishes to us. The easiest way is to use a number, eg: 0 means add, 1 means deduct, etcetera. But you can also use text strings! Or any other trick you can find in the reference manual or come up.
The final thing for this section is validation! Make sure to validate your inputs! If you allow for 0 - 3 to be equations, then the user inputting 69 should surely print an error! During an error, you should also communicate the error to the user! And obviously simply exit the main function using return.
Tips:
- Compose a list of invalid inputs per calculation. Document it somewhere and validate accordingly.
- Read the DM reference article about input. It actually explains a large deal, apparently.
Bonus:
- Make validation of input arguments a separate proc, that takes 3 parameters: a, b, and the action.
- Use a list of valid strings or numbers with the input proc. This should generate buttons, instead of a text prompt.
Spoilers: Example validation
/proc/main() var/a = ... var/b = ... var/action = ... //!< Valid [0, 3], indicates which operation to perform. if (action < 0 || action > 3) world << "Invalid operation specified." return if (action == 2) // 2 => division if (b <= 0) world << "Cannot divide by 0" return
Section 2: Calculating
Now that we have our numbers and the actions, we should do some calculation! The 4 basic arithmetic calculations are easy enough to do: a + b, a - b, a / b, a * b. So all you need to do is to calculate the output accordingly, and then print it out. If your validation is on point from the previous chapter, then this should be a pretty smooth section.
But this is also where you can start doing more. There are more arithmetic that can be done, for example, discrete/boolean arithmetic.
Bonus:
- Implement more arithmetic actions.
- Save the sum and allow the user to do the next action with the sum.
- ???
Lecture 2: More? What Do You Mean There's MORE Than Just Variables and Functions?!
Subtitle: how I learned to live with OOP.
Topic list:
- CONTROL STATEMENTS (cont.)
- Looops.
- Simple for-loops. While-loops.
- Looops.
- LISTS
- What's a list?
- How to create one.
- How to access one (index based).
- What's association?
- What the actual fuck are BYOND lists. (Key based access.)
- Iteration over lists.
- Copying-cutting-etc.
- Pass-by-reference semantics.
- Lists are passed by ref, they can be modified and this modification shows outside.
- What's a list?
- OBJECT ORIENTED CODING
- So uh, what's an object?
- Objects are a way to group code and variables.
- They (should) simplify code!
- A list as a simple object.
- Single-responsibility principle somewhere here.
- Object instantiation.
- Object variable access and passing.
- Objects are pass-by-reference, always!
- Object deletion.
- Why the GC hates you and your family.
- Inheritance!
- Why it's useful?
- What happens to variables?
- What happens to procs?
- So uh, what's an object?
Setup
Setting up Visual Studio Code
Use Matt's Guide to Contribution and Coding to set up Visual Studio Code and related plugins. Specifically, follow the first half of the second post. All the way up until it starts talking about the admin config.
Downloading the Base Project
The base code for this project is hosted on Github, accessible via this repository. A link to directly download the repository as a .zip file is here. To set up the project, unzip the folder onto your desktop or somewhere else. It can now be opened with Visual Studio Code: simply right click one of the internal folders (labelled "setX") and select "Open With Code".
Project Structure & Compiling
In Visual Studio Code, all projects should contain at least 3 files:
- world.dm - Do not touch this file for now, it does some background magic to make the setup work.
- course.dm - This is the file you should be modifying to make stuff work! It'll contain a function called /proc/main(), which is what you should modify to make stuff happen.
- course.dme - This is the DM Environment file for the project. It's basically what ties the entire project together and makes it able to compile.
After you've made any changes to the code, you have to press Ctrl-Shift-B to compile the code. Compilation is required to produce the .dmb file which we will be hosting with DreamDaemon.
Hosting
After compilation, we need to host the project to join and see what our code does!
This is done using a program called DreamDaemon. It's installed in your program files, right besides DreamMaker.exe and BYOND.exe. Find it, open it. And then follow the pictorial guide to success:
Helpful Resources
Here's a list of helpful resources for programming DM specifically: