Efficiently coding multiple Display descriptions

Started by mumford, Sun 27/02/2011 19:46:01

Previous topic - Next topic

mumford

Here is some of my sample code for a door:

Code: ags

int m = 0;
function hDoor_Interact()
{
  if(m == 0) Display("You shove at the door in a manly way. It laughs at your muscles.");
  if(m == 1) Display("Undeterred, you begin to pound on the door like a dozen madmen.");
  if(m == 2) Display("Because of your unwavering determination and singleminded concentration, the door..... remains shut.");
  m++;
  if(m == 3) m = 0;
}


These of course, appear in order as you click on the door, but this just seems so.... clunky. I'm going to have to create this type of script every time I want to create a list of ordered things that happen in any interaction. I tried to declare the variable as static inside the function but that didn't work, so I settled for a global.

Is there any way to have the string descriptions inside an XML file, or maybe inside the properties of the hotspot, like in the property schema?

On a similar note, I wanted to create a function like this:

Code: ags

function ShowRandomLine( param string[] arrText )
{
   int x = Random(arrText.Length - 1);
   Display( arrText[x] );
}

But I don't think it's possible to get an array length without passing it manually, and I'm not sure if it's even possible to pass an array in a function parameter.


I tried looking through the FAQ, wiki, and tried running some searches, but I couldn't really dig up anything about this.

Thanks,
Mumford

Khris

Regarding the first issue, there's the MultiResponse module. Basically, you're putting all responses in a single string separated by ">" and the module will automatically use the next one each time.

Something similar can be done to give a random response:

Code: ags
String RandomResponse(String responses) {
  // split responses into multiple strings
  // choose one at random
  return resp[random_value];
}


Usage:
Code: ags
  Display(RandomResponse("Hi there!>Hello!>Good day to you!"));


Here a dynamic array can be used to store the responses; in general, since you can't read the length of an array, just decide on some upper limit, e.g. 20, then create a random value until the element isn't "" or null.

mumford

that's exactly what I was looking for. Thanks very much~

SMF spam blocked by CleanTalk