Difference between revisions of "Programming Crash Course"

From Bitfighter
(Created page with "= Introduction = While scripting in Bitfighter is meant to be as simple as possible, one can not escape the fact that you are writing a computer program. In order to learn, re...")
 
(Functions)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category:Scripting]]
 +
 
= Introduction =
 
= Introduction =
 
While scripting in Bitfighter is meant to be as simple as possible, one can not escape the fact that you are writing a computer program. In order to learn, read, or speak about programming, you have to understand the basic concepts and vocabulary that all programmers use.
 
While scripting in Bitfighter is meant to be as simple as possible, one can not escape the fact that you are writing a computer program. In order to learn, read, or speak about programming, you have to understand the basic concepts and vocabulary that all programmers use.
Line 18: Line 20:
 
</source>
 
</source>
  
Also like in math, we can perform arithmetic on variables, and use them to set the value of other variables:
+
Like in math, we can perform arithmetic on variables, and use them to set the value of other variables:
  
 
<source lang="lua">
 
<source lang="lua">
Line 25: Line 27:
 
</source>
 
</source>
  
After the program has run, <tt>y</tt> will have a value of 3, which is equal to <tt>1 + 2</tt>
+
After this program has run, <tt>y</tt> will have a value of 3, which is equal to <tt>1 + 2</tt>
  
 
Unlike in math, variables in programming can hold things other than numbers. They can hold pieces of text, which are called "strings". In Bitfighter, they can also hold objects, such as a Flag or a Ship. These topics are discussed more in the tutorials.
 
Unlike in math, variables in programming can hold things other than numbers. They can hold pieces of text, which are called "strings". In Bitfighter, they can also hold objects, such as a Flag or a Ship. These topics are discussed more in the tutorials.
Line 38: Line 40:
 
print(y)
 
print(y)
 
</source>
 
</source>
<tt>print</tt> is a function. When we say <tt>print(x)</tt> we're telling the computer to print the value of <tt>x</tt>. In Bitfighter, this will show up on the console (press Ctrl+/ in-game to see it).
+
<tt>print</tt> is a function. When we say <tt>print(x)</tt> we're saying to the computer "print the value of <tt>x</tt>". In Bitfighter, this will show up on the [[Console]] (press Ctrl+/ in-game to see it).
  
 
TODO: function definitions, arguments, return values
 
TODO: function definitions, arguments, return values

Latest revision as of 11:52, 9 August 2013


Introduction

While scripting in Bitfighter is meant to be as simple as possible, one can not escape the fact that you are writing a computer program. In order to learn, read, or speak about programming, you have to understand the basic concepts and vocabulary that all programmers use.

This page is meant to be a light introduction to programming concepts as they apply to Bitfighter scripting.

What is a Program?

People say that computers "speak in ones and zeroes". This is only half true. Humans don't speak ones and zeroes, and computers can only do what a human has told them how to do. For that reason we need a language that both humans and computers can understand, somewhere in between human languages like English and the computer languages of zeroes and ones. We call these "programming languages".

A "program" or "script" is a file written in a programming language by a human, and which is meant to be run by a computer.

A script is run from top to bottom, just like humans normally read. The computer reads one line at a time, and runs each line as it comes to it.

Variables

A variable in programming is related to variables in math. They are used to hold values. Here is a simple example of creating a variable called x and "assigning" it a value of 1:

x = 1

Like in math, we can perform arithmetic on variables, and use them to set the value of other variables:

x = 1
y = x + 2

After this program has run, y will have a value of 3, which is equal to 1 + 2

Unlike in math, variables in programming can hold things other than numbers. They can hold pieces of text, which are called "strings". In Bitfighter, they can also hold objects, such as a Flag or a Ship. These topics are discussed more in the tutorials.

Functions

If you think of variables as the "nouns" of programming languages, then functions are the "verbs". Lets expand on our example above:

x = 1
y = x + 2
print(x)
print(y)

print is a function. When we say print(x) we're saying to the computer "print the value of x". In Bitfighter, this will show up on the Console (press Ctrl+/ in-game to see it).

TODO: function definitions, arguments, return values