Creating variables

From Game Editor

Jump to: navigation, search

There are two ways to create variables in gameEditor. The two types of variables are self declared variables, in script editor, or ones created by the variables button, found in script editor. The big difference between the types is how the variables themselves work. Learn how to use variables.

Variables

When in Script Editor, the third button called Variables allows you to create ints, doubles, and strings. One of the most prominent uses for this method is the ability to make actor variables. These variables are similar to that of internal struct variables, in this case found within the actor. Actor* use can access actor variables. Basically, they are common variables found in all actors, but each actor holds their own value. So each actor can be individualized, a common use being HP. Each actor would be able to hold different values for HP, but with the use of only 'one' variable. The other type is global, which are the same as those made in script editor.

When you click the Variables button you should see this.

Image:Add_New_Variable.png

It gives you the choice to Add, Edit, and Remove. Click Add. At this point it gives you

  • Name:
  • Integer/Real/String
  • Global/Actor
  • Array
  • Size:
  • Save group:

Name will give the name of the variable, for reference, this will be used to actually access the variable, so you should name it something memorable and easily understood. The name cannot be the same as any actors, other variables, including built in variables.

Integer/Real/String determines the variable type. These are basic descriptions of the variable types. int holds a number with no decimal. Real holds a number with decimal values. String is a char array, which is essentially a string. It can be no bigger than 255 characters in length.

Global/Actor Variables, as stated previous, global are easily accessible to any actor/script. Actor variables, allow each actor to have their own value for a variable. To access these values for other actors, either use actorname.variablename, or use Actor*s.

Array determines if the variable is an Array.

Size determines size of array, if yes.

Save group is the name of the variable save group. This field is required for gameEditor's saving method.


After selecting all the variable specifications you want, click add. After adding, you can edit all factors of the variables you made except for name.

Script Editor

This form of declaring variables is that of C. To declare a variable, you simply put the variable type, followed by the name, and if you wish for an array, add the [] with the desired array size in the brackets.

  • int
  • double
  • char

These are just three here, but there are many more. You can create types also, and there are Pointers. These are also variables, but really hold paths that point to the contents, instead of holding them. For globally recognized variables, place the declarations into Global Code. If you put it within a function, or an actor's script events, then the variable is a temp variable in which it is only seen in that environment, and cannot be accessed elsewhere. Usually these die with the run through of the script, but gameEditor can sometimes hold on to variables declared within an event's script, be warned that it will drop the variable after the frame if too many things are happening. Another thing to watch for is that variables can only be declared in the beginning of a script or function declaration.

The common term for these types of variables is LOCAL. This means they are local to the script they were called in, and dumped from memory after that. This is important for conserving memory when writing complex scripts and loops.

It is also important to prevent trampling on other variable names; for instance, all loops (and all scripts and tutorials you will find on loops) use int i; to run the loops.

If you had a global variable i, this could cause problems in certain situations, as we use i only for local purposes, making it global would be bad.

When declaring these variables, you will use the same method to call them no matter how you are using them. For instance, in a draw actor script,

int i;
int j;

or

int myx=x+100;
int myy=y+100;

If you are creating a function, and using ints for input, it would appear like so:

void dosomething(int xx, int yy){
int cx=xx;
} 

Notice that when you declared int xx in the constructor, you do not have to declare it again. And as you see, xx and cx are both initialized in the same manner.