- Home /
How to update variables in a string when displayed?
Hello everyone,
I have a lot of strings I want to use for character dialogue. Right now I store them in a dialogue.js as public variables and grab them when needed (public var greeting : String = "Hello, mate!";) and then I update the GUI with the needed string. But there's a big problem with this method: any variables in the strings themselves (the player name, or number of items the player is carrying, etc.) remain at the same value as when the game started.
So how can I format this so it doesn't bake the variable into the string until the dialogue is actually put on screen?
Thanks!
Show an example of the code for player # of items and where you are getting that - once in Awake()/Start(), via a GetComponent reference, etc.
Something like this:
Dialogue.js
var character: CharacterScript; //linked to character through inspector
var Greeting : String = "Hello, mate!";
var youHave : String = "You have " + character.coconuts + " coconuts!";
CharacterScript.js
var name: String = "Bradley";
var coconuts: int = 0;
GUI.js
GUI.Box (Rect (80,60,874,648), activeText);
Talking.js
function Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
GUIScript.activeText = Dialogue.youHave;
}
}
Consider this solved! I used an adapted version of akauper's solution. I created a function that reassigns the string variables when the player starts dialogue. Thanks, everyone!
Answer by akauper · Apr 14, 2014 at 01:28 AM
//Dialogue.js
var character : CharacterScript;
var greeting : String = "Hello, mate!";
//CharacterScript.js
var name : String = "Bradley";
var coconuts : int = 0;
//GUI.js
var activeText : String = "";
GUI.Box(Rect (80,60,874,648), activeText);
//Talking.js
var dialogue : Dialogue; //set a reference here however u want(inspector is fine)
var character : CharacterScript; //set a reference here as well.
function Update()
{
if(Input.GetKyeDown(KeyCode.Space))
{
GUIScript.activeText = dialogue.greeting + " You have " + character.coconuts + " coconuts!";
}
}