Gah - a solution with more questions. – EntropicLqd

Legacy:UnrealScript For Non-Programmers/Meatier Parts

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

This page is part of the UnrealScript For Non-Programmers tutorial which is broken down into a series of pages. It may not make sense if you haven't been following this series, then again, you never know, you might gain something from it.


After this next section, a look back at Object Oriented Programming Overview will show you how much you are learning by your being able to follow the code samples offered there.

A Deeper Look Into Program Construction[edit]

Here we will examine other parts of the code that makes up a program such as Tests and Loops. This section intends to prepare you to further understand these concepts by examining the UnrealScript Language Reference/Program Structure and then I think you have earned your white-belt and can consider yourself a no0b. =)

Tests[edit]

Conditional Statements such as the tests we will discuss here are how your program is directed as to what it should do next. Keep in mind that if you are writing a program, it only does what you tell it to. Even if it does something that is not what you intended to be the outcome. It is still doing what you told it to do, and the error therefore is yours.

Your program is no smarter than you are. If this program can only do what you tell it do when you write it then you are going to have to give it a way to do more than simply going down a linear list of commands. Try to consider what a program could possibly accomplish with that kind of structure. It could maybe stuff a few variables with numeric values and perform some math functions on them dumping the result into another variable and print that variable's value to the screen. Weeeeeee! But can it check to see if the answer is correct?

You have to help your program act non-linearly. If you know how to make your program perform tests and take particular action(s) depending on the results of those tests then it becomes much more dimensional and capable of much more. For instance, Picture an egg on a fence. It's going to fall one way or the other way. On one side of the fence is a cement sidewalk, hard and unforgiving. On the other side of fence is a water trough full of soft, impact-absorbing water. We can use a conditional statement for our to program to perform specified tasks depending on the outcome.

We know what the outcome of the egg falling one way would be over it falling the other. Let's assume that there is already a function written in our class to assign a random number to our variable we have created to represent the state of the egg. With that variable's value set to be a random number from one to two, we want our program to draw a broken egg at the point that it lands on the sidewalk if our program finds the current value of that variable to be "1". And we want to run an animation of water splashing if it falls into the trough because the variable intEggState has a value of "2".

With intEggState already declared elsewhere and a function that keeps it set to a value that indicates the current state of the egg, how do we tell the program to execute one task depending on one outcome? Here is where a test of intEggState's value will let our program know what it should do next. Our program is an OOP program and we are writing code in a subclass that inherits code from its parent (i.e. a function named "Draw()" ). So all we have to do here is call on the functions that already exist. We will tell our program to do what we want it to do in the case of intEggState by writing an if statement.

"if", "else", and "else if":

(Two forward slashes (//) tells the program that anything else on the rest of this line is only a comment to future readers of the code and to skip everything on this line after the slashes.)

if

// If the egg lands on the sidewalk, it breaks.
if( intEggState == 1 )
    Draw( BrokenEgg );

What was that all about? When the program sees the keyword "if", it will test the condition set within the following parenthesis. Upon finding that the condition is true, it will execute the code that follows on the next line. When finding that the condition is not true the program will exit the whole if statement doing nothing and proceed linearly to the next piece of code. But what if the egg didn't land on the sidewalk but instead in the trough? The test above does not account for that. It would do nothing, but we wanted it to draw a splash animation if it landed in the water. How about this code:

else

// If the egg lands on the sidewalk, it breaks
// but if the egg lands in the trough, it splashes.
if( intEggState == 1 )
    Draw( BrokenEgg );
else
    Draw( SplashAnimation );

When the program finds the condition following the keyword "if" to be true, it will execute the code that follows and then exit the if statement ignoring the "else" keyword as well as the code following. We are satisfied with this because there was only one outcome and therefore we only wanted one thing to happen.

When the program finds the condition of if to be false, it will skip the next code and begin to work from the keyword "else" and will execute the code following and the egg will splash into the trough. Again we would be satisfied with the single action performed in reaction to the single outcome. But what if the egg hadn't fallen yet?

We will have to assume here that when the variable intEggState was declared that it was also initialized to have a value of "0".

(like this)

var int intEggState; //Declared the variable
intEggState=0; //Initialized the variable to a value other than 1 or 2

Now, until the egg falls one way or the other, it remains on the fence and therefore intEggState's value remains "0".

else if

// If the egg lands on the sidewalk, it breaks
// but if the egg lands in the trough, it splashes.
// If it hasn't fallen yet, it does nothing.
if( intEggState == 1 )
    Draw( BrokenEgg );
else if( intEggState == 2 )
    Draw( SplashAnimation );

When the program finds the condition of if to be false, it will skip the next code and begin to work from the keyword "else". Here it finds another if keyword. When it finds the condition of "else if" to be true it will execute the code following the condition and the egg will splash into the trough. But we already know the egg hasn't fallen yet so in this case, the program would find the condition of if to be false and test the condition of else if, finding it false, and exit the whole if statement doing nothing. This would satisfy us, as we don't want anything to happen until the egg falls.


Case Tests 
(In Progress... Might use Case to evaluate the eggs "State".)

Loops[edit]

The For Loop 
(In Progress)
The Do Loops 
(In Progress)

Related Topics[edit]

Discussion[edit]

This tut is going nowhere. I (the author) will not be contributing to it anymore, and it needs much much more. No one else is contributing to it, so it's a waste of space and an eye-sore.[DeleteME]