Scripting hints: Difference between revisions

From Adventure Game Studio | Wiki
Jump to navigation Jump to search
(Couldn't resist. ;))
m (Shortened Thread links)
Line 1: Line 1:
[[Category:Tidbits and Snippets]]
[[Category:Tidbits and Snippets]]


* '''Keys not listed in the ASCII Code Table have no guarantees about their keycode numbers''' and may well overlap keys that are listed. ([http://www.bigbluecup.com/yabb/index.php?topic=21763.msg265903#msg265903 Thread])
* '''Keys not listed in the ASCII Code Table have no guarantees about their keycode numbers''' and may well overlap keys that are listed. [http://www.bigbluecup.com/yabb/index.php?topic=21763.msg265903#msg265903]


* There is '''no point in using a short rather than an int as a local variable'''. In fact, ints are faster because the CPU is better at reading/writing 32-bit chunks of memory than it is at 16-bit chunks. ([http://www.bigbluecup.com/yabb/index.php?topic=21214.msg259479#msg259479 Thread])
* There is '''no point in using a short rather than an int as a local variable'''. In fact, ints are faster because the CPU is better at reading/writing 32-bit chunks of memory than it is at 16-bit chunks. [http://www.bigbluecup.com/yabb/index.php?topic=21214.msg259479#msg259479]


* There is '''no fixed limit on the size of global arrays''', but as scotch says they use up memory so don't go silly with creating massive ones unless you really need to. ([http://www.bigbluecup.com/yabb/index.php?topic=21191.msg258882#msg258882 Thread])
* There is '''no fixed limit on the size of global arrays''', but as scotch says they use up memory so don't go silly with creating massive ones unless you really need to. [http://www.bigbluecup.com/yabb/index.php?topic=21191.msg258882#msg258882]


* The workaround for '''displaying player-entered strings''', in case anyone wants to know, is simply to do the following, since that way, the player string will not be parsed for special tokens.
* The workaround for '''displaying player-entered strings''', in case anyone wants to know, is simply to do the following, since that way, the player string will not be parsed for special tokens: [http://www.bigbluecup.com/yabb/index.php?topic=12051.msg157449#msg157449]


  Display("%s", playerString);
  Display("%s", playerString);


([http://www.bigbluecup.com/yabb/index.php?topic=12051.msg157449#msg157449 Thread])
* You have to place the export after the variable's declaration, so you '''don't necessarily have to put the exports at the end of scripts'''. [http://www.bigbluecup.com/yabb/index.php?topic=18972.msg231473#msg231473]
 
* You have to place the export after the variable's declaration, so you '''don't necessarily have to put the exports at the end of scripts'''. ([http://www.bigbluecup.com/yabb/index.php?topic=18972.msg231473#msg231473 Thread])


* '''Parameters to functions can be made optional''' by adding a default value in the import of the function. This even works if you are not actually exporting the function.
* '''Parameters to functions can be made optional''' by adding a default value in the import of the function. This even works if you are not actually exporting the function.
Line 20: Line 18:


* '''Optional parameters only work with int and enum''' parameters, you can't do it with strings or floats.
* '''Optional parameters only work with int and enum''' parameters, you can't do it with strings or floats.
:'How about '''S'''trings?'
:''How about '''S'''trings?''


* Basically, to be consistent with Java/C#, the '''protection level always goes first when using protected functions''':
* Basically, to be consistent with Java/C#, the '''protection level always goes first when using protected functions''': [http://www.bigbluecup.com/yabb/index.php?topic=19369.msg226163#msg226163]


  protected import static function get_slots();
  protected import static function get_slots();


([http://www.bigbluecup.com/yabb/index.php?topic=19369.msg226163#msg226163 Thread])
* It's not until a room is loaded that the '''current viewport size''' is known. So better not rely on system.viewport_width or system.viewport_height in game_start. [http://www.bigbluecup.com/yabb/index.php?topic=12051.msg170758#msg170758] ([http://www.bigbluecup.com/tracker.php?action=detail&id=470 Tracker])
 
* It's not until a room is loaded that the '''current viewport size''' is known. So better not rely on system.viewport_width or system.viewport_height in game_start. ([http://www.bigbluecup.com/yabb/index.php?topic=12051.msg170758#msg170758 Thread]) ([http://www.bigbluecup.com/tracker.php?action=detail&id=470 Tracker])


* You can use ''return;'' to abort the rest of the script.
* You can use ''return;'' to abort the rest of the script.
Line 36: Line 32:
* '''RunInventoryInteraction doesn't get run immediately''' - instead, it gets run when the calling script finishes. Applies only if the interaction runs a script. Interaction editor commands are executed immediately.  
* '''RunInventoryInteraction doesn't get run immediately''' - instead, it gets run when the calling script finishes. Applies only if the interaction runs a script. Interaction editor commands are executed immediately.  


* The '''number of game loops speech text stays for''' is:  
* The '''number of game loops speech text stays for''' is: [http://www.bigbluecup.com/yabb/index.php?topic=14364.msg174988#msg174988]


  int gameloops = ((StrLen(text) / game.text_speed) + 1) * GetGameSpeed();
  int gameloops = ((StrLen(text) / game.text_speed) + 1) * GetGameSpeed();
([http://www.bigbluecup.com/yabb/index.php?topic=14364.msg174988#msg174988 Thread])


* '''Functions cannot return structs or arrays''' - they can only return primitive types. This includes Strings in AGS v2.71.
* '''Functions cannot return structs or arrays''' - they can only return primitive types. This includes Strings in AGS v2.71.


* The character's '''global inventory variables are integers''' and can be used to keep track of how many items the character has.
* The character's '''global inventory variables are integers''' and can be used to keep track of how many items the character has.

Revision as of 05:14, 8 December 2005


  • Keys not listed in the ASCII Code Table have no guarantees about their keycode numbers and may well overlap keys that are listed. [1]
  • There is no point in using a short rather than an int as a local variable. In fact, ints are faster because the CPU is better at reading/writing 32-bit chunks of memory than it is at 16-bit chunks. [2]
  • There is no fixed limit on the size of global arrays, but as scotch says they use up memory so don't go silly with creating massive ones unless you really need to. [3]
  • The workaround for displaying player-entered strings, in case anyone wants to know, is simply to do the following, since that way, the player string will not be parsed for special tokens: [4]
Display("%s", playerString);
  • You have to place the export after the variable's declaration, so you don't necessarily have to put the exports at the end of scripts. [5]
  • Parameters to functions can be made optional by adding a default value in the import of the function. This even works if you are not actually exporting the function.
import function myfun (int a, int b=5);  // optional int b
  • Optional parameters only work with int and enum parameters, you can't do it with strings or floats.
How about Strings?
  • Basically, to be consistent with Java/C#, the protection level always goes first when using protected functions: [6]
protected import static function get_slots();
  • It's not until a room is loaded that the current viewport size is known. So better not rely on system.viewport_width or system.viewport_height in game_start. [7] (Tracker)
  • You can use return; to abort the rest of the script.
  • Local room scripts have their data segment saved when the room is destroyed, so all variables retain their values (for rooms 1-300 only). This is useful for storing the condition of light switches, for example.
  • RunInventoryInteraction doesn't get run immediately - instead, it gets run when the calling script finishes. Applies only if the interaction runs a script. Interaction editor commands are executed immediately.
  • The number of game loops speech text stays for is: [8]
int gameloops = ((StrLen(text) / game.text_speed) + 1) * GetGameSpeed();
  • Functions cannot return structs or arrays - they can only return primitive types. This includes Strings in AGS v2.71.
  • The character's global inventory variables are integers and can be used to keep track of how many items the character has.