Lesson 3 - Variables, functions and objects

Lessons and tutorials about mods and maps
Post Reply
User avatar
Feralidragon
Godlike
Posts: 5489
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Lesson 3 - Variables, functions and objects

Post by Feralidragon »

So far in the past lessons we have been doing fairly simple things to start from, however this lesson will be a more theoretical overview of the following:
- variables
- functions
- objects


What is a variable?
A variable is nothing more than a small bucket you can store things to, like numbers, text, objects, lists, anything at all.
For example, every time you login into a website, the page requests your a user and password, and when you type them, your're writing that info into a "user" and a "password" variables respectively, so they can be read and validated.


What is a function?
While the variable is "storage" of data, a function represents a set of "actions" to take with or without that data.
Just like the login web page, when you press Enter or click in the submit button to confirm your credentials, you're actually calling a function to take the "action" to send that information over the network.

Functions can return new data or not return any data at all.
For example: in the case of the web page, it returns feedback in whether your login was successful or not.

So, in short: a variable is to store data and a function is the action to take (generally based on that data).


What is an object?
An object is something which holds a set of variables and functions within it. The web page itself is an object: it has the fields for you to fill in with data (user and password) and the button to send that data over (the submit function).

When variables are contained within objects they are also called: members, properties or attributes.

When functions are contained within objects they are also called (depending on their purpose):
- methods: when they do not return a value;
- getters: when their purpose is to return the value of one of that object variables;
- setters: when their purpose is to store a value in one of that object variables;
However, when they return new data, their correct name is still function.

You should start getting familiar with these terms, so whenever you read something on coding, you know what's being talked about.


Example:
- let's say you want to do a sum like 658 + 363 using paper alone.
You take the paper (the object), and fill it with the numbers (the variables):

Code: Select all

  658
+ 363
------
Then you do the math operation with all the needed steps to reach a conclusion (the function(s)):

Code: Select all

   1             11             11  
  658            658            658 
+ 363     >    + 363     >    + 363 
------         ------        ------
    1             21           1021 
And finally you can return the result: 1021

Now here's the same example, but translated into UnrealScript:

The variables and functions within the object:

Code: Select all

class Paper extends Object;

var int FirstNumber;
var int SecondNumber;

function int getSum(int A, int B)
{
local int result;

    result = A + B;
    return result;
}
So that's basically our paper, and now to perform the operation itself we have to execute the function:

Code: Select all

getSum(FirstNumber, SecondNumber);
And if we want to keep the result stored in another variable (like sumVar for example):

Code: Select all

sumVar = getSum(FirstNumber, SecondNumber);

Homework:
Based in what you learned so far, create a new mutator which can count the number of player respawns during the whole match, and in each player respawn, broadcast a message to all players with that number.
[ The code with the solution will be posted in the next lesson under spoiler tags, however it is highly encouraged to do it without looking at the solution. ]


From here onwards, all the lessons will be about UnrealScript itself and its structure as a coding language, or in other words: the warm up is finally over, and it's time to cover how the language and Unreal Engine works for real from now on.

Also, I will be posting more of these "homeworks" from now on so you can start to develop your own programming skills in order to understand things more independently.

________________________________________________________________________________________________________________________

i_remember_my
Posts: 2
Joined: Fri Mar 28, 2014 5:22 am

Re: Lesson 3 - Variables, functions and objects

Post by i_remember_my »

Hello everyone, I'm doing a course in c + +, I am part of the string recently, but hey, I have two questions about this:

1_ there a way to check that this code works, as for example in c + +, using sprintf.
2_ in the part that says:
:wth:
So that's basically our paper, and now to perform the operation itself we have to execute the function:

Code: Select all
getSum(FirstNumber, SecondNumber);

And if we want to keep the result stored in another variable (like sumVar for example):

Code: Select all
sumVar = getSum(FirstNumber, SecondNumber);

I do not understand, that would go down

sorry i noob
User avatar
_naruto_999
Average
Posts: 68
Joined: Sat May 24, 2014 12:07 pm

Re: Lesson 3 - Variables, functions and objects

Post by _naruto_999 »

One thing that I've learned from C:

functions have memory addresses and there are pointers to functions much like pointers to variables. So, no need to store the returned value of a function in a var if there is no need to.
Post Reply