Scripting, Code & Interaction

From Adventure Game Studio | Wiki
Jump to navigation Jump to search

Working with variables: the basics

I need help with variables. What I am doing doesn't seem to be working. What do I do?

One of AGS's main advantages is its ability to use variables. A variale is an area of memory storage that contains a value, which you can check and change with scripting. Variables are very powerful and useful things in programming languages. If you have an RPG-style stat system in place, you can use variables to store information such as health, magic, strength, etc. You can name variables anything you'd like, as long as they're not names of functions, AGS commands, or already-declared variables, like EGO or a GUI name.

Due to AGS's emulation of the 'C' language, variable scope must also be considered. If a variable will be used throughout the entire game, it will have 'global' scope. If it to be used only in a single room, its scope will be limited to that room.

There are three major steps to using custom variables.

Step 1. If the variable will have global scope (such as health, money, etc.), go to the global script. If the variable will have room scope (such as light_is_on or guard_random_state), go to the room's main script (the button with the braces - { }). Then, at the very beginning before any functions, declare and set the variable, using the following syntax:

 vartype varname;
 varname = value;

OR

 vartype varname = value;

vartype can be string (for strings of alphanumeric text, it's advisible to use the new String type if you're using AGS V2.71 or above), int (for whole numbers), or float (for decimal numbers, supported from AGS V2.7 onwards). An example of an int declaration would be:

 int health;
 health = 100;

OR

 int health = 100;

Initially, an int variable will have a default value of zero (0). Therefore, if a variable starts off with '0' as its value, you do not need to assign a value like above.

A string would be declared and set like so:

 string myname;
 StrCopy(myname,"Richard");

OR, from V2.71 onwards:

 String mynewname = "Richard";

If you're using V2.7 or earlier, you may NOT use direct assignment of string. In other words, you CANNOT use the code myname="Richard"; as this will cause an error with AGS. Use the above code to correctly assign strings to string variables.

If you are using global variables you must do the following.

Step 2. At the end of the global script (or just after all the variable declarations), you must export any and all variables you create, like so:

 export health;
 export myname;

Step 3. For any and all global variables (whether int, string, String or float), you must import them into all rooms in which they will be used. So, in each and every room where you will be using global variables, go into the room's main script (the { } button), and at the very top -- before anything else -- import the variable(s) like so:

 import int health;
 import string myname;

The last two steps are necessary with global variables. Remember! ONLY the import command requires the data type as well as the variable name. The export command ONLY needs the variable name.

Running regular code outside dialog

Placing your code: more basics

Fatal error when running "function DoSomething(1,2,3);"

Using "FaceLocation" doesn't work

Using "GetMP3posMillis()" always returns "0"

Fatal error when using "=" and "==" with strings

Inserting variables into speech/messages

GlobalInts, your friend: what they are and how to use them

Error: "(line xyz): incorrectly terminated character constant"

Slowing down your loops

Creating your own custom functions

Defining custom hotkeys and shortcuts

Having a character continuously animated in the background

Taking away score points.

Changing the names of characters, hotspots, objects, and inventory items in the middle of gameplay

How to play movies and video files (AVI, MPG, WMV, etc.)