Modme Forums

ABNORMAL202 SCRIPTING TUTORIAL 3: VARIABLES

Game Modding | Call of Duty: Black Ops 3 | Scripting


Abnormal202:

ABNORMAL202 SCRIPTING TUTORIAL 3: VARIABLES

See ALL Tutorials

DESCRIPTION

Variables can be a lot of different things in .gsc. Frankly I'm not even sure if the term "variable" is the appropriate term in this language, but it's the one I've heard used the most.

Variables can store:
- strings
- numbers
- objects
- booleans
- and maybe some others I can't think of off the top of my head

defining a variable is as simple as this:

my_variable = WHAT_IM_SETTING_IT_TO;

and in this tutorial I'm gonna go over the various things we can set it to and how we can manipulate the variables

GENERAL RULES

  • you must define a variable before you use it

  • a variable can only be used in the function it is defined - unless we pass it somehow, which I'll talk about later

  • you can effectively overwrite a variable by simply defining again as something different


STRINGS

strings are pretty simple. They store text, and you create them using quotation marks "". Strings are mostly used for either setting text you want to see in-game (such as in a hintstring or print), or for describing or getting something in script.

to store a string in a variable it's like this:
jblundell_quote = "fog rolling in";


now because we've stored the string inside that variable, we can use that variable in place of where we would normally use a string. for example in the IPrintLnBold() function:
jblundell_quote = "fog rolling in";
IPrintLnBold(jblundell_quote);


note this is the same as:
IPrintLnBold("fog rolling in");



NUMBERS

Just like what you learned in Algebra, numbers can be letters too! yay!

let's define a variable as a number:

push_ups_performed = 0;


if we want to do math operations on a variable, we can do it like this:

push_ups_performed = push_ups_performed += 3; //adds 3
push_ups_performed = push_ups_performed -= 3; //subtracts 3
push_ups_performed = push_ups_performed * 3; //multiplies by 3
push_ups_performed = push_ups_performed / 3; //divides by 3

notice we have to type it twice, because we want to define it as itself (whatever it used to be) plus the 3, not just as 3.

there is shorthand however for simply adding 1 to it or subtracting 1 from it:
push_ups_performed ++; //adds 1
push_ups_performed --; //subtracts 1


POP QUIZ: What number will this print to the screen?
push_ups_performed = 5;
push_ups_performed --;
push_ups_performed = push_ups_performed * 4;
push_ups_performed = push_ups_performed - 7;
push_ups_performed = push_ups_performed / 3;
push_ups_performed ++;

IPrintLnBold(push_ups_performed);

if you said

4

then you are correct!

OBJECTS/ENTITIES

Perhaps the most useful use of a variable is storing an object/entity. Using certain functions allows use to get an actual in-game/in-radiant entity that we can store in script. For example one of the most important Engine Functions:

entity GetEnt(,,[ignore spawners])


notice how when we look up the engine functions in the Modme ScriptDocs, the word entity appears before it. This means that using this function will return us an entity, which we can store in a variable. So using this function would look like this:

watermelon_model = GetEnt("watermelon","targetname");

assuming that in radiant we have a script_model with the KVP of targetname: watermelon. And preferably it looks like a watermelon.

You won't be able to print a variable with an entity stored in it, like you can with a string, number, or boolean, because well, what would it print? that variable is a representation of a physical radiant entity, not a word or number.


BOOLEAN

A boolean is basically something that can have 1 of 2 values: etiher true or false.

we can set a variable as true simply like this:
ur_gay = true;


please note that true and false do not go inside quotation marks "", or else you would be setting it equal to a string that simply is the word "true".

Also important to note is that true and false can also be represented by 1 and 0, respectively.

booleans don't mean much to us yet because we don't know what if statements or while loops are. For now, just remember we're allowed to store them in variables like this.


PASSING VARIABLES

there are many ways we can pass variables, so we're not limited to using it in the same function we define it in.

these ways consist of:
- setting parameters/using arguments
- returns
- self

PARAMETERS/ARGUMENTS

You may have noticed when looking through the Modme ScriptDocs that most of the engine functions ask for something, or multiple things. These are called parameters and they can either be mandatory (you must pass them if you want the function to work) or optional (you can pass them, but the function is capable of working without them).

for example let's look back at that engine function, GetEnt():
entity GetEnt(<value>,<key>,[ignore spawners])</key></value>


the first parameter is required and it is the name you gave the key of the entity in radiant. The second is also required and it is the key you are using of the entity in radiant.
the third parameter is not required (I can tell because it uses [] instead of <>) and that is asking whether or not to include spawners in the search for the entity.

So in order to use this function, I need to pass at least two arguments to it.
value = "watermelon";
key = "targetname";
include_spawners = false;
watermelon_model = GetEnt( value, key, include_spawners);


notice that I don't have to give the arguments the same exact names as the parameters being asked for.

So, knowing that, when we create function we can have them ask for specific parameters. For example:

function calculate_bmi( weight, height )
{
    bmi = weight / height / height;

    IPrintLnBold("your BMI is " + bmi);
}


In this example the function is asking for two parameters: a weight, and a height.

it then calculates bmi by dividing the variable weight by height, and then dividing it by height again.

because I used the word weight and the word height as my parameters, any arguments that get passed to this function will be stored in a variable called weight and a variable called height, which I use for the calculation.

so if I wanted to use this function, I could do this:

calculate_bmi( 70, 1.8 ); //weight is in kg and height is in m 


SELF

using self is very similar to passing a variable using parameters/arguments. It is basically a shortcut.

You can only pass one self per function and you do it by simply putting the variable before calling or threading the function. like this:

watermelon_model thread fly_away();


provided we've already defined the watermelon model above, most likely using GetEnt() as I talked about before.

now to use the variable watermelon_model in our function fly_away() we just type self where we want to use it:
function fly_away()
{
    self MoveZ(1000, 10, 5);
}

this will make the watermelon fly upward by 1000 units in 10 seconds.


RETURNS

Finally, one other way we can pass data back is with returns. a return passes back data to where it was called from, and is stored in a variable there.

for example the function GetEnt() returns the entity we are looking for, so we can then use it in the same function we called GetEnt() from.

Let's modify the bmi function to return the bmi value instead of print it:

function calculate_bmi( weight, height )
{
    bmi = weight / height / height;

    return bmi;
}


it's as simple as putting the word return before the value we want to return. Now when calling this function, in order to receive the bmi value we must set a variable for it to be stored in:

my_bmi = calculate_bmi( 60, 1.5);

now we can use the variable my_bmi which should have stored in it the calculated value using the data 60 for the weight and 1.5 for the height.

See ALL Tutorials