Scripting, Code & Interaction: Difference between revisions

*>Ashen
No edit summary
*>Ashen
Line 1: Line 1:
==Working with variables: the basics==
==Working with variables: the basics==
''I need help with variables. What I am doing doesn't seem to be working. What do I do?''
''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 variable 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.
One of AGS's main advantages is its ability to use variables. A variable 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 is to be used only in a single room, its scope will be limited to that room.
Creating your own variables is easy. At the top of the script, or anywhere BEFORE the variable is first used, declare the variable, using the following syntax:
 
  '''vartype''' varname;
 
 
'''vartype''' can be ''String'' (for strings of alphanumeric text), ''bool'' (for values that can be true or false), ''int'' (for whole numbers), ''float'' (for decimal numbers), an [http://www.bigbluecup.com/manual/enum.htm enumerated type], or a [http://www.bigbluecup.com/manual/struct.htm user defined type] (however, structs cannot be imported unless the struct definition is in the script header).
 
An example of variable declaration would be:
 
  '''int''' health;
  '''String''' MyName;
  '''bool''' HasItem;
 
 
Initially, an int variable will have a default value of '''zero''' ('''0'''), a float will have a default value of '''0.0''', a String will have a '''null''' default value, and a bool defaults to '''false'''. If these values are OK, then you've successfully declared your first variable. However, you might need your variables to start with a value assigned to them - and that's pretty easy too.
 
An example of declaring and setting a variable would be:
 
  '''int''' heath '''=''' 50;
  '''float''' Pi '''=''' 3.14;
  '''String''' MyName '''=''' "Richard";
  '''bool''' WearingPants '''=''' true;
 
 
'''Note:'''
* String values must be inside quotation marks - the following code WILL NOT work:
 
  String MyName = Richard;
 
* Strings that're declared outside a function (e.g. at the top of the Global, or Room, script) CANNOT be declared with a value. If they need to start off as someting other than null, you need to set them in a function (e.g. game_start, or one of the ;Player enters room' interactions)
* float values must have a decimal place, even if you want it to start as a whole number. The code to use there would be:
 
  float MyFloat = 3.0;
 
 
 
As seen above, variables are set using '''='''. There are a few other common operators you'll use in handling variables, which are described [http://www.bigbluecup.com/manual/Operators.htm in the Manual]. For example, '''==''' means 'is equal to' and is used to ''compare'' (not set) values, such as:
 
  if (health == 0) Display("Oh no! You are dead!");
 


There are three major steps to using custom variables.
Or


'''''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:
  if (MyName == "Richard") cNpc.Say("Hey, Ricky!");


  '''vartype''' varname;
  varname '''=''' value;


OR
'''!=''' means 'is not equal to', e.g.:


   '''vartype''' varname '''=''' value;
   if (MyName != "Richard") cNpc.Say("Hey, you're not Ricky!");


'''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), '''bool''' (for values that can be ''true'' or ''false'', supported from AGS V2.7 onwards), '''int''' (for whole numbers), '''float''' (for decimal numbers, supported from AGS V2.7 onwards), an [http://www.bigbluecup.com/manual/enum.htm enumerated type] (supported from AGS V2.7 onwards), or a [http://www.bigbluecup.com/manual/struct.htm user defined type] (however, structs cannot be imported unless the struct definition is in the script header).


An example of an '''int''' declaration would be:
And so on. String variables have some [http://www.bigbluecup.com/manual/String%20functions.htm dedicated functions], to make handling them easier.


  '''int''' health;
  health '''=''' 100;


OR
Another important thing to be aware of is the 'scope' of variables. At declaration, variables are '''local variables'''. This means they can only be used in the script they were declared in - a variable declared in a Room script can only be used in that room; if it's declared in a Module script it can only be used in that module; if it's declared in a function, or interaction event, it can only be used by that function or interaction. Variables declared inside functions or interactions will be reset everytime the function or interaction is run - however, variables declared in Room, Global or Module scripts stay the same until you change them (e.g. a Room-based variable will still be stored if you leave the room and come back again).


  '''int''' health '''=''' 100;
A variable that can be used in all rooms, modules, etc, is said to be a '''global variable''', and here's how you make them.


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.
Firstly, global variables can only be made in the Global script, or in Module scripts. They're declared the same way shown above, but then have to be exported, using the export keyword:


A '''string''' would be declared and set like so:
  '''vartype''' varname;
  '''export''' varname


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


OR, from V2.71 onwards:
The manual says the export line should go at the very end of the script - but as long as it comes AFTER the variable declaration, that's not really important.


  '''String''' mynewname '''=''' "Richard";
Next, you have to import the variable to all the rooms you want to use it in. Open the Room script (the '{}' button in the Room Editor) and paste the import at the very top of the script. If you want to use the variable in all rooms, put the import in the Global Headrer (Ctrl-H):


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.
  '''import vartype''' varname;


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:
So, if you wanted to make a global variable called 'health', you'd put this at the top of your Global script:


  '''int''' health;
   '''export''' health;
   '''export''' health;
  '''export''' myname;


NOTE: If you're using V2.7 or earlier, you cannot export/import '''string'''s. You'll need to use the GlobalStrings (limited to 50) or declare the strings as a char[200] array.


'''Step 3'''. For any and all global variables (whether '''int''', '''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:
And this in the Global Header (or Room script):


   '''import int''' health;
   '''import int''' health;
  '''import String''' myname;
If you want your variables to be available in ALL rooms, import them into the Global Header (Ctrl-H).


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.
 
Note that the import command needs the variable name AND VARIABLE TYPE: export only needs the variable name.
Global variables declared in Module scripts should always be imported in the Module Header.
 
'''Final Note:'''
This is based on V2.72, users of older versions need to keep a few things in mind.
* The String type was introduced in V2.71. Before that, you need to use string. (Note the capitalisation.) The string type cannot be set with '''=''' - you need to use the StrFormat command, and as with the String type it has to be inside a function (e.g. game_start). Also, strings can't be made global. Use the GlobalString array, or declare them as a char array instead (e.g. char MyName[200]).
* float, bool and enumerated types were introduced in V2.7.


==Running regular code inside dialog==
==Running regular code inside dialog==
Anonymous user