Question about using variables within a dialogue message.

Started by themill, Wed 04/06/2008 02:19:07

Previous topic - Next topic

themill

Hi! I apologize in advance if another thread addresses this issue. I was unable to find one, and don't have all day! ;) I am curious about using variables within dialogue messages. Specifically, I want to know whether a variable I set can influence what shows up in a given message, and how I would go about implementing this.

For example:
Say I set a variable to a random number between 1 and 2. If the variable sets to 1, a character's name becomes Bob. If it sets to 2, his name becomes Fred.

What I would want, then, is for the character to say either, "Hi, my name is Bob.," or "Hi, my name is Fred."

Now, I realize that I could create these as two seperate messages and use the value of the variable as a condition for one or the other to appear. However, if I end up using a larger range than two or three names, this becomes extremely cumbersome.

What I want to know is whether I can set up a script within a single message that changes a single word in that message depending upon what the variable is set to.

For clarity, another example would be setting a random number between 1-100, and having a character say, "My lucky number is ___," without having to create 100 seperate messages which operate conditionally depending upon the value of the variable. You see what I'm getting at?

I am not very proficient at scripting, and have been trying to figure this out for a couple of days, so any help would be greatly apprectiated!

GarageGothic

#1
Look up "string formatting" in the help file, and the String.Format function. That should tell you what you need.

Basically, you can insert Strings and int's into other strings by using codes like %d and %s. In your case you could do something like this:

Code: ags
String name;
if (Random(1) == 1) name = "Bob";
else name == "Fred";
cMan.Say(String.Format("Hi, my name is %s.", name));


You'll want to declare the String outside the function though, so you can access it throughout the game. And the randomization could be done at game_start or the first time the player enters the room the character's in.

Gilbert

Unfortunately that'll mean we still need to swap to normal scripting (completely or via run-script perhaps) as the dialog scripting system is a bit limited in function at the moment.

GarageGothic

Oh yeah, sorry - I missed the part about the dialog system. I never use it myself. But using Character.Say in dialog_request triggered by run-script should work.

themill


SMF spam blocked by CleanTalk