Beginning programming may be hard for those with absolutely no
programming experience at all. To others, it isn't as hard because they
have had some experience with it. In this tutorial, I will teach you
the basics of programming. If you don't want to bother reading this
because you know about the basics already, then go on to tutorial 2.
It's always good to refresh your memory though
Now onto the tutorial.
There are basically two things you should know about programming. You use these whenever you write a script.
- Code:
-
1. Variables
2. Methods
First off, let's talk about variables. Variables are very important in
programming. Without them nothing will be able to function. A variable
in short holds information. This information can be displayed in many
different ways. One example is how many times you have hit something,
or collected something. So how do you use them you ask? First of all,
you have to declare them in your script. We'll get into that later in
this tutorial.
There are three main types of variables in programming.
- Code:
-
1. Integers
2. Strings
3. Booleans
Integers are basically numbers. Keep in mind however, that you cannot use
decimals for integers, because there is another term for that. A
decimal is called a double. (We will get into this later). Anyways,
integers must be whole numbers. They can be negative though, so don't
worry. Remember though that integers do not have a set value. There
value can move around. An example of this would be if you start a
mining script. You probably have 0 ores mined when you begin the
script. But as time goes by, you may have 300 ores mined. The same
variable is used, but it just increased as we mined more ores.
So how do we declare integers?
- Code:
-
1. You tell the computer what type it is.
2. You give it a name. (This name should be easy for other developers to
understand. Don't use lgschpd for an integer that declares how many
logs are cut. Try using something along the lines of logsChopped. It makes it a lot easier for other people to read and understand where you were coming from with the name).
3. You say "=" to show that you are setting the value.
4. You state the value. (Give it a number for int, or characters for String)
Here's a quick example to see if you understand so far.
- Code:
-
int logsChopped = 0;
Let's break this down.
1. Int
What this basically means is that it is an integer. int = integer Pretty easy right? Integers hold whole numbers. They can't be negative, otherwise it's not an int.
2. logsChopped
This is the name of the integer. Keep in mind though that names are case sensitive, so be sure to be careful when your using these again, as it may give you an error if you spell it wrong.
3. =
This means that the int logsChopped equals
4. 0;
This is what the variable will equal. The semi colon is there because every
line of executable code will be followed by a semi colon to tell the
computer that the code is finished. (This will not apply to all types
of code).This concludes the session on Integers.
Next I will be talking about Strings.
Strings are basically a string of letters or words. They are declared with quotations. These things: " "
Here's an example of a String.
- Code:
-
String myUsername = "Famous";
That wasn't too hard, was it? Now lets move on to Booleans.
Booleans may be a little hard for you to grasp. I would suggest reading this
over carefully, and maybe even re-read it. Booleans are values that return
True or False. They are used to see whether your script still has money
left in your inventory, or if we need to walk back to the previous location that we were at because we died.
Here's an example of a Boolean.
- Code:
-
boolean gettingAttacked = true;
All this is really saying is that if you are getting attacked, then return true. Now we will talk about how to use these 3 types of variables that you just learned about.
I'm going to give you some lines of code that use each of these variables
that you just learned about to give you an idea of how they are used. I
will try to keep this simple, so bare with me. Don't expect to just
jump right into programming, because it isn't that easy. Like anything, if you want to become good at something, you need to take your time and practice.
Integers - Code:
-
int miningLevel = 0;
int nextMiningLevel = miningLevel + 1;
Strings - Code:
-
String name = "Famous";
String answer = "My name is " + name;
String sentence = "The dog barked.";
String question = "How good is your programming?";
Booleans - Code:
-
boolean attackMonster = true;
boolean eatFood = false;
Well hopefully you understood these examples, and are having fun learning in the process.
Now let's move onto methods.
Methods are fairly easy to write once you learn how to script. They are basically just describing how something is done.
There are two things you should know about Methods.
- Code:
-
1. You have to write the method and tell it what to do.
2. You have to tell the computer where and when to use the Method.
So how do we write methods? Here are some (hopefully) simple instructions.
• You write the first line. This line contains the name of the method that will be called.
• You write the code inside the block, or in between the { }. These are used to tell
the computer where the method starts and where it stops.
To dive into writing methods, we'll use the infamous "Hello, World!" Here is what the base of the code will look like.
- Code:
-
public void helloWorld {
log("Hello, World!");
}
Let's break this down.
- Code:
-
1. Public
This just means whether or not other classes can use it or not. Don't worry about it to much, because I will explain it later.
2. Void
If you remember, all methods return a type of variable. When I put "void" there, that means it does not have to return anything, because it is void.
3. helloWorld
This is just the name of the method that is used.
4. log("Hello, World!");
This tells the script to display the message "Hello, World!"
To conclude, this is only the beginning into the world of programming. The
basics are the hardest, but when you get passed them, it gets much
easier. I promise
So stick with it, and don't give up! Hope you enjoyed my tutorial, and please post your feedback!