Scripting, Code & Interaction: Difference between revisions

*>Ashen
*>Ashen
Line 176: Line 176:
This could also be done with strings. For example, if the player's name is stored in the '''global string''' #17, use the following code if you want '''BOB''' to call your character by name:
This could also be done with strings. For example, if the player's name is stored in the '''global string''' #17, use the following code if you want '''BOB''' to call your character by name:


   string my_name;
   String my_name = Game.GlobalString[17];
  GetGlobalString(17, my_name);
   cBob.Say("Hey, '''%s'''! What's goin' down, my man?", my_name);
   DisplaySpeech(BOB, "Hey, '''%s'''! What's goin' down, my man?", my_name);


Or, for AGS V2.7 and above:
Confused? First, we declare a String and name it '''my_name'''. Next, we use '''Game.GlobalString''' to store the string in slot #17 into the String we created. Next, '''Character.Say''' will be used and the '''%s''' is replaced with the string '''my_name'''. Got it?
You can also do this with int variables, e.g. to display numbers on GUI Labels:


   string my_name;
   int my_int = 3;
   GetGlobalString(17, my_name);
   String my_string = String.Format("'''%d"''', my_int);
   cBob.Say("Hey, '''%s'''! What's goin' down, my man?", my_name);
 
Or:
 
  int my_int = player.InventoryQuantity[iCheese.ID];
   player.Say("I have '''%d''' pieces of cheese.", my_int);
 
(The last one could be shortened to: ''player.Say("I have '''%d''' pieces of cheese.", player.InventoryQuantity[iCheese.ID]);'')


Confused? First, we declare a string and name it '''my_name'''. Next, we call the '''GetGlobalString''' command to store the string in slot #17 into the string we created. Next, '''DisplaySpeech''' (or '''Say''' for V2.7+) will be used and the '''%s''' is replaced with the string '''my_name'''. Got it?
'''NOTE:'''
* The ''Game.GlobalString'' array was introduced in V2.71. In earlier versions, you'll need to use ''GetGlobalString''
* ''Character.Say'' was introduced in V2.7, earlier versions use ''DisplaySpeech''
* For more on String formatting codes (like ''%d'' and ''%s''), [http://www.bigbluecup.com/manual/StringFormats.htm read the manual].


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