1 of 6 messages

Started by PsychicHeart, Fri 08/07/2005 07:35:06

Previous topic - Next topic

PsychicHeart

how can i make it so that after a certain button is pressed (i dont need help with that), one of 6 messages is shown, randomly?
Formerly known as Flukeblake, Flukezy etc.

monkey0506

#1
Code: ags
/* code for button press -- not sure if you mean GUI or keyboard button, but you say you have it figured out... */ {
  int rand = Random(5);
  /* DisplayMessage(rand); // -- room message */
  /* DisplayMessage(rand + 500); // -- global message */
  /* struct mystr {
        char s[200];
        };
      mystr strs[6];
      StrCopy(strs[0].s, "message 0");
      StrCopy(strs[1].s, "message 1");
      StrCopy(strs[2].s, "message 2");
      StrCopy(strs[3].s, "message 3");
      StrCopy(strs[4].s, "message 4");
      StrCopy(strs[5].s, "message 5");
      Display(strs[rand].s);
      // -- locally declared string (simple struct version) */
  /* string str0;
      string str1;
      string str2;
      string str3;
      string str4;
      string str5;
      StrCopy(str0, "message 0");
      StrCopy(str1, "message 1");
      StrCopy(str2, "message 2");
      StrCopy(str3, "message 3");
      StrCopy(str4, "message 4");
      StrCopy(str5, "message 5");
      if (rand == 0) Display(str0);
      else if (rand == 1) Display(str1);
      else if (rand == 2) Display(str2);
      else if (rand == 3) Display(str3);
      else if (rand == 4) Display(str4);
      else if (rand == 5) Display(str5);
      // -- locally declared string (complex string version) */
  }


There...that should just about do it!  One of these should work...as long as I understood what you meant.

Edit:  BTW, the I'd probably personally use one of the first three, that last one is kind of nasty...six variable declarations, and then you have to test the value to be able to display the correct one.  Number three is the nice alternative if you want to declare the strings locally (i.e., inside of a custom function).

Edit:  The parameter to Random should be 5 (I fixed it) because the range is between 0 and MAX (the parameter) inclusively, so using 6 would produce 7 potential values.  I thought it was between 0 and MAX - 1...

Ashen

#2
Rather than declaring and formating 6 strings, wouldn't it be easier to use:
Code: ags

  int rand = Random(5);
  if (rand == 0) Display("Message 1");
  else if (rand == 1) Display("Message 2");
  else if (rand == 2) Display("Message 3");
  else if (rand == 3) Display("Message 4");
  else if (rand == 4) Display("Message 5");
  else if (rand == 5) Display("Message 6");


Unless, of course, you're using globaly declared strings for the messages. Can't see a reason to use locally declared ones, though.
I know what you're thinking ... Don't think that.

monkey0506

Oh...yes... I don't think of these things at...well I don't know it was really early or really late...one of the two.

Play_Pretend

I just tried out Ashen's code and it worked beautifully, that's going to really liven up my responses.  ;D   Logically, other than not wanting to have to type forever, is there any actual limit to just how many random responses you could have for an interaction click, as long as you increased the Random() integer accordingly and added the right amount of Else if statements?  Will the game's space requirements constrict something like that?

InCreator

eh?
Integer? 32767 (if I remember correctly) is maximum that integer can be. That's your limit and more than enough.

Play_Pretend

Heh, hadn't thought of it like that.  Yeah, seriously doubt I'll want 32,000 responses for "touch yourself".  Thanks. :)

Ashen

Actually:
Quote from: The Manual
short      16-bit integer, can store numbers from â€"32,768 to 32,767 
int          32-bit integer, can store from â€"2,147,483,648 to 2,147,483,647 

So, in theory I guess you could have 65,000-odd responses with a short and 4,300,000,000-ish with an int.
I know what you're thinking ... Don't think that.

SMF spam blocked by CleanTalk