auto, double, int, long, break, else, switch, case, enum, register, typedef, char, extern, return, union, const, float, short, unsigned, continue, for, signed, void, default, goto, static, volatile, do, if, while.
In order, here's how they work:
●auto
This is used to declare a local-style variable, but it has no real use in Game-Editor. Don't bother!
Script-Editor Syntax:
- Code: Select all
auto int varname;
●double
Declares a variable that can store a high amount of decimal places. High accuracy real/floating point number.
Script-Editor Syntax:
- Code: Select all
double varname;
●int
Declares an integer. Integers can't store decimal values, so a number can't be 1.25, it will instead be set as 1.
Script-Editor Syntax:
- Code: Select all
int varname;
●Long
Placed before a variable type declaration, it will allow that variable to store a twice as high as normal value.
Script-Editor Syntax:
- Code: Select all
long int varname;
●break
Used to "break" out of loops, and to finish a switch() statement.
Script-Editor Syntax:
- Code: Select all
int i;
for(i=0; i<100; i++)
{
if(i == 10)
{
break;
}
}
//this code would normally stop when i = 100, but the break; causes the code to stop when i = 10.
●else
An "else" statement is placed after an "if" statement. It's triggered whenever the "if" statement fails.
Script-Editor Syntax:
- Code: Select all
if(var1 == 1)
{
//stuff
}
else
{
//different stuff
}
●switch
A conditional statement preformed on only integer or character type variables. Can test multiple conditions and provide an option if none of them match ("default:")
Script-Editor Syntax:
- Code: Select all
switch(varname)
{
case 1: varname++;
break;
case 2: varname+=2;
break;
}
The initial variable passed to Switch MUST be a integer.
cases can only be whole numbers, and can not be passed a variable.
●case
Used in a switch() statement for each test. See "switch" for usage.
●enum
Creates a list of special values that can be used as constants in code.
Script-Editor Syntax:
- Code: Select all
enum week {monday=1, tuesday=2, wensday=3 , thursday=4, friday=5, saturday=6, sunday=7};
It sort of acts like a list of "#defines", simply type the keyword wherever in the code you want.
These can not be set after the enum!
●register
As part of C code, stores a variable in a special register instead of the computer's RAM, making access to the variable quicker. I don't recommend using it, as there's no immediately apparent usage in Game-Editor.
Script-Editor Syntax:
- Code: Select all
register int varname;
●typedef
typedef is used to define new data type names to make a program more readable to the programmer.
- Code: Select all
typedef number int;
//this will allow you to create a temporary integer using the new keyword "number" like so:
number myvar;
●char
Stores a single character. When used as a pointer (char *), it can be used as a string of characters easily.
Script-Editor Syntax:
- Code: Select all
char * str; // Pointer version
or
Script-Editor Syntax:
- Code: Select all
char Chr; // Just a single letter
●extern
Grabs a variable outside of the scope of a pair of brackets. Not very useful, as variables are already available from outside of scopes.
Script-Editor Syntax:
- Code: Select all
int a=25;
if(varname<5)
{
int extern a; // this = 25
//more stuff
}
●return
Returns a value from a function and ends the function's execution. Go to the "how to make functions" section to learn more about this.
Script-Editor Syntax:
- Code: Select all
int funct()
{
return 5;
}
●union
A single piece of memory in the computer's RAM will store more than one variable, for instance a "char" and a "integer", but only one can have a value at any time. Setting one will overwrite the other.
- Code: Select all
union {
int a;
char b;
};
// Sets "a" to 5
a = 5;
// Sets "b" to the letter 'g', and unsets A.
b = 'g';
●const
Placed before a variable to make it so it can't be changed.
Good when programming functions or placeholder value variables so you don't accidentally change the variable while programming.
Script-Editor Syntax:
- Code: Select all
const int a = 5; // This variable will always be 5, you can't do "a = 7;".
●float
Creates a variable that can store decimal places, like double, but with less precision.
●short
Reduces size of an int, double or float. Can only store a value have as large as normal.
●unsigned
Disables the use of negative values on a variable. Allows you to go twice as high as normal with a positive value in exchange.
●continue
Skips to the next go of a loop.
Script-Editor Syntax:
- Code: Select all
int i;
for(i=0;i<100;i++)
{
if(i==10)
{
continue;
}
}
// This code would normally stop when i = 100, but the continue; causes the code to reset when i = 10.
// Note, don't use this code, it causes an infinite loop and will crash your game...
●for
Let's say, if you wanted to set a variable for numbers 1 - 10 in order, what could we do?
We could do something like this:
- Code: Select all
int count=1;
sprintf(text,"%d\n", count++);
sprintf(text,"%d\n", count++);
sprintf(text,"%d\n", count++);
sprintf(text,"%d\n", count++);
sprintf(text,"%d\n", count++);
sprintf(text,"%d\n", count++);
sprintf(text,"%d\n", count++);
sprintf(text,"%d\n", count++);
sprintf(text,"%d\n", count++);
sprintf(text,"%d\n", count++);
But that's terrible on the hands and very inefficient, we use loops like "for" to do it for us.
- Code: Select all
int count=1;
for(count=1; count<1000; count++)
{
sprintf(text,"%d\n",count);
}
This code goes up to an amazing 1000 numbers, instead of just 10.
Each "for" loop has three sections to it. Before the first semicolon is the "initialization", where you should set your variable to the first value. The second is the condition that must be false for it to stop looping, and the third is what happens every time the loop goes back around.
●signed
Very useless, because most variables, besides "char" are by default a signed variable.
●void
Used to declare a function without a return value.
Script-Editor Syntax
- Code: Select all
void function()
{
//stuff
}
Tutorial on how to create functions near the bottom of this post.
●int/double/float/char
ALSO used to declared a function that returns a value, that value type is defined by how the function is declared.
Script-Editor Syntax:
- Code: Select all
double Limiter(double MinV, double V, double MaxV);
{
return max(MinV,min(V,MaxV));
}
//Handy script that will limit a value;
This script takes three variables, a lower limit, a variable to be limited, and an upper limit.
In computes the lower value between the max and the current, then the higher value between the lower limit and the current value.
You can take the value it returns like this:
- Code: Select all
int i;
i = 11;
i = Limiter(0, i, 10);
// It will return 10, because before "i" was a higher value than that upper limit.
●default
A type of "else" for use in a switch() condition statement.
Script-Editor Syntax:
- Code: Select all
int myvar = 5;
switch(myvar)
{
case 3:
// stuff..
break;
default:
// If myvar is anything other than 3, this would be called.
}
●goto
This isn't recommended for use in general, but it has its few uses.
It will automatically skip to a marker anywhere in your code, but it's very hard to debug / fix.
- Code: Select all
int a;
if(a==10)
{
goto label1;
}
a++;
// It will skip right over "a++" and to to this label
label 1:
●static
Like auto, but can declare global variables in a normal script.
This doesn't do anything valuable in Game-Editor, so I don't recommend it.
- Code: Select all
static int varname;
●volatile
(C Programming Reference Description)
●do / while
The do and while keywords act similarly, but:
"do" runs the code ONCE without being checked, and repeats after IF the expression is true.
while will ONLY run IF the expression is true, and continues to repeat if the expression is true.
Syntax (do):
- Code: Select all
do
{
// Content
} while( expression );
Syntax (while):
- Code: Select all
while( expression )
{
// Content
}
(C Programming Reference Description)
●if
Compares two variables or expressions.
- Code: Select all
if(var1 == var2)
{
//stuff
}
you can compare values with these operators:
- == equal to
- >= greater or equal to
- > greater to
- <= inferior or equal to
- < inferior
- != not equal to
●Operators
Heres the list:
= Equals - moves one set of data to another EX:
- Code: Select all
var=var2;//var2's data has been copied to var one
*= Multiply - multiplies one set of data by another EX:
- Code: Select all
var*=var2;//var now equals var times var2
/= Divide. - divides one set of data by another EX:
- Code: Select all
var/=var2;//var now equals var divided by var2
% Modulus. - returns the remainder of a division EX:
- Code: Select all
var=var2%2;//var will equal 1 if var2 impair, and 0 if var2 pair
+= Add. - adds one set of data to another EX:
- Code: Select all
var+=var2;//var now equals var plus var2
-= Subtract. - subtracts one set of data from another EX:
- Code: Select all
var-=var2;//var now equals var minus var2
Skipping bit operators, look it up in a C programming reference :3
note: These can be used without the ='s as well for more complex calculations...
●Extra notes:
-- and ++
Oka, so we know that "a = a + 1;" is perfectly fine, right? But! We can also use "a++;" to simply add one to a value.
You can also use "++a;", the difference being that a++ will add one AFTER what happened is calculated, such as "var1 = a++;" while "var1 = ++a;" will add 1 to a before "var1" equals "a".
●"?" Operator
The "?" Operator is used as a replacement for an if/else statement
- Code: Select all
int myvar = 4;
int myvar2 = 5;
myvar = myvar2 == 5? 6: 7;
// Variable = [Conditional statement] ? [if true] : [if false];
Is equal to the following code:
- Code: Select all
int myvar = 4;
int myvar2 = 5;
if(myvar2 == 5)
{
myvar = 6;
}
else
{
myvar = 7;
}
●TypeCasting
This allows you to convert one data type to another, may cause errors if not used cautiously.
- Code: Select all
double myvar;
int myvar2 = 5;
// now then, to make the double equal to the int without truncation, we use typecasting.
myvar = (double)myvar2;
// this makes the integer act like a double on that line.
●#define
This allows you to assign a keyword to a piece of code, letting you clean up your code and make things quicker.
- Code: Select all
#define kill DestroyActor("Event Actor")
//note, you don't need a ; at the end of a #define line.
//We can now type "kill;" anywhere in the code, and it will run "DestroyActor("Event Actor")"
●The sprintf() Function
This function allows you to insert integers, double/real's, and strings into another string 'dynamically'.
In order to insert another variable, you need to know it's type, and use a "specifier" to match that. If you wanted to insert a integer you would type the specifier "%d" or "%i".
- Code: Select all
int myvar = 54;
sprintf(text, "The variable myvar equals %d", myvar);
// After the string with specifiers, you list the variables your using in order, according to the order of the specifiers.
The specifiers that can be used are:
- %d & %i - Integer variables.
- %s - String variables.
- %c - Single character. (usually you would assign a single character value like so:)
- Code: Select all
char mychar = 'k';
- %% - Puts a % into the string.
●Controlling cloned actors
It can save you a lot of trouble and time creating games if you know how to use clones effectively.
Normally, when you clone an actor, they'll both act exactly the same way, but using the following functions and keywords, you'll be able to treat them like you would a normal actor and better.
The functions and keywords in Game-Editor that are used for controlling clones are:
- getclone() - Allows you to control a clone's actor variables if you know the clone's exact name.
- Code: Select all
getclone("myactor.4")->x=52;
Note that it requires a "->" to get the variable instead of a "." - clonename - Gets the name plus the clone index of the current actor.
- Code: Select all
//(This code would have to be on a text actor to use it!)
strcpy(text, clonename);
If the actors name was "myactor" and he was, say, the 7th clone, this would make his text be "myactor.6" (6 because the first actor would be 0 and count up from there) - cloneindex - This will take only the number of the current clone.
- Code: Select all
//(This code would have to be on a text actor to use it!)
textNumber = cloneindex;
And again, if this was on the 7th clone of myactor, his text would now say "6".
I will be using a very handy function called "cloneID" by Feral for this somewhat short tutorial on how to Check/Control All Clones of a Actor with a For Loop.
If you are recreating this tutorial in Game-Editor, you will need the following code in Global Code. (get there by clicking Script and then Global code).
- Code: Select all
char buffer[50];
char * cloneID(const char *cname, short unsigned int cindex)
{
sprintf(buffer, "%s.%d", cname, cindex);
return buffer;
}
Now, using getclone() we can control a cloned actor of course, but to do this for all the clones of a actor is time consuming, and what if we don't know how many clones there are?
Using a for() loop and ActorCount() we can check and modify all the clones of a actor, without knowing how many there are, and we don't have to write the code over for every clone.
- Code: Select all
int i; // Create a temporary variable for the for() loop.
for(i=0; i<ActorCount("myactor")-1; i++)
{
//i=0 starts the loop
//i<ActorCount("myactor")-1 will keep it going until we run out of clones. (-1 because actor count doesn't start at 0 like cloneindex)
//i++ will add 1 to i every time the code is run, so we go through each clone one by one.
getclone(cloneID("myactor", i))->x = i*5; // This will grab the current clone designated by the loop, and make it's
// x value whatever clone it is times 5. (Just for fun, you can do whatever you want with whatever variable you feel like!)
}
●Creating functions using Global Code
(when making a function, you have to place it in Global Code [accessible through the main menu bar -> Scripts -> Global Code] or else you won't be able to use it everywhere.)
When writing code, you can find yourself typing the same code over and over again, with minor adjustments. This is unnecessary because we can create functions.
For example, you could write:
- Code: Select all
moveto(0, 0);
lineto(width, 0);
lineto(width, height);
lineto(0, height);
lineto(0, 0);
Every time you wanted to draw around the edges of a canvas.. but wouldn't it be so much easier to be able to just write:
- Code: Select all
drawEdges();
To create a function, we need to know what the type of function will be.
First off, will it return a value? For example, will it be able to do this:
- Code: Select all
myvar = myfunction();
If we aren't trying to return a value, we would use void to declare the function, example:
- Code: Select all
void myfunction()
{
// We can't set a variable using this function.
}
To use it, you would simply type myfunction();
But, if we ARE trying to set a variable from this.. we would use one of the variable declarations, Ex: int, char, float, or double.
Declaring the function with one of these instead of void lets us set variables of that type with this function, like so:
- Code: Select all
int myfunction()
{
return 5;
// return sets what value it will return, and stops the function.
}
int myvar = myfunction();
// myvar now equals 5.
Using char instead will let you return letter / string values, and so on.
Parameters in a function will let you have small (or large) alterations to the function according to the user input, such as, in one of my functions it's declared like so: void HPDraw(double value, double max_value, char colour) .
Of course I could have not had any parameters, but with the parameters, I can let the user of the function choose the colour, and what numbers the hpbar is drawn from.
You can also accept a actor as a parameter, to do this, you must put:
- Code: Select all
Actor * myactor;
As one of the parameters. To take or set a value from the inputted actor value, you must use:
- Code: Select all
myvar = myactor->x;
// or
myactor->x = 5;
When using the function, you have to append a & to the beginning of the actor name:
- Code: Select all
myfunction(&player);
I'm aware this tutorial is rough around the edges / pretty bad, so if you have any input feel free to reply!