Programming Basics

Programming an In-game computer is very easy. It starts by first making a disc, which is used to store programs the computer can run. The disc may be inserted into the computer by pressing the SHIFT key and right clicking on the computer, while the disc is being held by the player. The disc may also be ejected the same way, if the player is not currently holding an item.

The computer is accessed by right clicking, and exited by pressing the ESC key. The disc should be named, and is done by picking the disc up while accessing the computer, and placing it back down. All programs are automatically saved. There is not currently an easy way to delete a program once it has been created using the NEW button, the only method is to open the .PRG file in a text editor (notepad), and remove the program manually (not recommended), however, a program's name may be edited from within the program by selecting it after pressing the EDIT button. A program's name may only be 8 characters long, and typing a name longer than eight characters from within the program may have unexpected consequences.

After a disc has been inserted into a computer, the NEW button is used to create new programs on the disc. It should be noted, that a program may be written using any style that the programmer wishes. For example, a single program could be created that runs all of the code for the program. However, it is tedious and confusing to deal with a single large program, therefore, in these tutorials, I will be showing you a possible way to break up a program into more manageable parts made of separate programs. This has the effect of allowing code to be easily reused, as well as making the entire thing easier to manage and understand.

There are three basic things involved with programming, Commands, Variables and Programs. We have already briefly covered Programs, which are simple to create using the NEW button, as well as being easy to edit with the EDIT button.

## **Variables**

Variables are the containers for data the computer uses while running a program. All variables are signed integers (signed means they can be negative). Each variable may be named any string of eight characters including special characters (=-+\*/), and MUST be followed by a SPACE if not at the end of a line.

Variables are automatically created, and will not be destroyed until the program stops. All variables are global to all the programs stored on any given disc. This is in order to simplify the process of sharing variables among the different programs without the need to actually pass and return values. This has one drawback, that the programmer must be creative in there naming of variables, as well as careful to not reuse a variable without intending to do so.

Here is an example of creating a variable:

VAR = 1

It is customary to assign a value to a variable immediately after declaring it. An important thing to note about variable creation is that they will not be automatically created if it is the second variable in a statement, for example, the following is valid:

DOG = 0

GOOD = 10

DOG = GOOD

However, the following is not valid:

DOG = 0

DOG = GOOD

This is because the variable GOOD has not been declared. Variables may also be used to return a value from a Command like the following example:

VAR = K.A

Where K.A is a key press command.

Variables can be manipulated using addition, subtraction, multiplication and division. Since all variables are not floating point (they do not have a decimal), division results in a truncated answer. For example:

VAR = 7

VAR / 2

This would make the variable VAR equal to 3, as opposed to 3.5 or 4 (if it rounded). The following is also valid:

TODAY = 14

RESULT + TODAY

This would make RESULT equal to 14, because all variables are initialized with a value of zero, and zero plus 14 is 14.

## **Commands**

The basic commands are considered separate from the Robot API and Graphics API (coming soon), and there are seven total.

* LABEL
* GOTO
* IF
* ELSE
* END
* RUN
* K.A

**LABEL**

The LABEL command is used to create loops. Whenever the LABEL command is found, a corresponding GOTO should also be found. The label name is given after the command LABEL preceded by a space, for example:

LABEL A

**GOTO**

The GOTO command is used to loop a program to any LABEL within a program being ran. The label to go to, is given after the command GOTO preceded by a space, for example:

GOTO A

This would place the program execution point at the command LABEL A. The name of a label or goto can only be eight characters long, and may be the same as variable names without causing problems.

**IF**

The IF command is used to run (or not run) a specific section of code according to the value of a variable. Here is an example:

VAR = 1

IF VAR = 1

GOOD = 10

END

The conditions can be the following:

* = Equal to
* < Less than
* > Greater than
* ! Not equal to

**ELSE**

The ELSE command is used to run a specific section of code, if the previous IF section did not run. This should only be used with a corresponding IF command proceeding it. Here is an example:

VAR1 = 0

IF VAR1 < 1

VAR1 + 10

END

ELSE

VAR1 = 100

END

**END**

If you have made it this far, good job, we are almost done! You may have noticed the command END used with the IF and ELSE commands. The END command tells the computer when the end of an IF or ELSE statement is. It is very important to include this at the end of the IF and ELSE section of code. An IF or ELSE section of code may contain as many lines of code necessary, therefore, if the END is not included, strange results may occur.

**RUN**

The RUN command is the most important command to make it practical to code anything useful. This command will execute any program stored on the same disc. As mentioned earlier, programs are created by pressing the NEW button, and are edited by pressing the EDIT button and selecting it from the list of programs.

As an example, lets say the first program you created on the disc is called START, which is the name I usually use. I use the program START as the main loop for the thing to run. A program is simply an infinite loop of code run until the variable condition is changed. Here is an example START program:

RUN INIT

LABEL A

IF LOOP = 0

GOTO B

END

RUN TICK

GOTO A

LABEL B

Empty lines may also be added to make the code more readable. Here is the program INIT used in START:

LOOP = 1

BOOKS = 0

Here is the program TICK:

IF BOOKS < 10000

BOOKS + 1

END

ELSE

LOOP = 0

END

This program first runs the program INIT, which declares two variables; LOOP and BOOKS, while also setting their initial values. The program TICK is then run as long as the variable LOOP is equal to 1, however, in TICK, the value BOOKS is incremented by one each time the program completes a cycle (frame), until the value of BOOKS is equal to 10,000. When this is true, the IF statement does not run, and therefore allows the ELSE statement to execute its section of code. This makes the variable LOOP equal to zero, which makes the program GOTO the command LABEL B and the program stops. Whew, it should be noted that this programming language is very fast, and counting up to 10,000 on my computer did not take any noticeable time to stop executing, and appeared to occur instantaneously.

**K.A**

And last, but certainly not least, is the mysterious K.A. This is a very simple way to have key pressing detection for your program running on an in-game computer. The 'A' stands for the key being pressed, therefore K.A is for the 'A' key, K.B is the 'B' key etc. The basic idea, is to check for a keypress each frame. This is generally done within a separate program called KEYS or something. Also, if the key is pressed and detected, the variable indicating the keypress should be made zero. This will allow only a single keypress to be registered, instead of many. As usual, here is an example, I will simply modify the TICK program.

IF BOOKS < 10000

BOOKS + 1

END

RUN KEYS

And here is the program KEYS:

KEYT = K.T

IF KEYT = 1

LOOP = 0

KEYT = 0

END

This program will check for a keypress each frame, and if the 'T' key is pressed the variable LOOP will be made zero, and the program will stop running. The variable KEYT is also set to zero, however, in this case it is not strictly necessary, because after the variable LOOP is set to zero, the program stops.

## **Conclusion**

This concludes the Basic programming tutorial. As I mentioned at the beginning, it is very easy to program computers in SIMCUBE™, thanks to the very user-friendly graphical user interface. This programming will primarily be used for two things within the game, the most obvious being the ability to have a robot mine and build automatically. However, the thing I am really excited for, is the Graphics API. This will be very awesome once it is added (very soon).