Scripting, Code & Interaction: Difference between revisions
→Inserting variables into speech/messages
Line 125: | Line 125: | ||
==Inserting variables into speech/messages== | ==Inserting variables into speech/messages== | ||
''Hey, how would one go about inserting a variable (either integer or string) into a message (text-boxed or speech) using code? For example, I want to display how many gold coins the player has using the Display() command.'' | |||
Simple! when using any of the '''Display...()''' commands, inside the string that contains the message you must insert either a '''%d''' (for integers) or '''%s''' (for strings) and '''%f''' (for floating point numbers, only available for AGS V2.7 or above). This is a placeholder; it holds the place for the string or number to be inserted. After the string, instead of closing off the command with an ending parenthesis, add a comma, then the variable you want to insert into the message. | |||
For example, to display how many coins (let's say it's inventory item #24) the player has. You would insert the following code: | |||
Display("You currently have '''%d''' gold coins in your purse.", player.inv[24]); | |||
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; | |||
GetGlobalString(17, my_name); | |||
DisplaySpeech(BOB, "Hey, '''%s'''! What's goin' down, my man?", my_name); | |||
Or, for AGS V2.7 and above: | |||
string my_name; | |||
GetGlobalString(17, my_name); | |||
cBob.Say("Hey, '''%s'''! What's goin' down, my man?", my_name); | |||
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? | |||
==GlobalInts, your friend: what they are and how to use them== | ==GlobalInts, your friend: what they are and how to use them== | ||
==Error: "(line xyz): incorrectly terminated character constant"== | ==Error: "(line xyz): incorrectly terminated character constant"== |