Aller au contenu

Photo

Help with function


  • Veuillez vous connecter pour répondre
4 réponses à ce sujet

#1
Nebril2

Nebril2
  • Members
  • 59 messages
Hi all!!



Im having a problem with a function.
I want to make a script that calls a function that inside the function returns a random string value.

Example: function

string/void? treasure(){string objeo;  int azarss =Random(3)+ 1;   if (azarss ==(1))   return objeo("a");   if (azarss ==(2))   return objeo ( "b");   if (azarss ==(3))
   return objeo ( "c");

//This gave me an error "ERROR: PARSING RETURN STATEMENT"

So when i include the function on a script, i want that every time the function is called it returns a random value inside the function treasure and that value to be saved on a string variable inside the script. 

Any better way to do this?

Thanks!:D

#2
Failed.Bard

Failed.Bard
  • Members
  • 774 messages

Nebril2 wrote...

Hi all!!

Im having a problem with a function.
I want to make a script that calls a function that inside the function returns a random string value.

Example: function

string/void? treasure(){string objeo;  int azarss =Random(3)+ 1;   if (azarss ==(1))   return objeo("a");   if (azarss ==(2))   return objeo ( "b");   if (azarss ==(3))
   return objeo ( "c");

//This gave me an error "ERROR: PARSING RETURN STATEMENT"

So when i include the function on a script, i want that every time the function is called it returns a random value inside the function treasure and that value to be saved on a string variable inside the script. 

Any better way to do this?

Thanks!:D



string treasure()
{
 string objeo;
switch (Random (3) + 1)
    {
     case 1: objeo = "a"; break;
     case 2: objeo = "b"; break;
     case 3: objeo = "c"; break;
    }
 return objeo;
}


Alternately, you can just return the strings directly, as:

   if  (azarss == 1)   return "a";

Modifié par Failed.Bard, 08 novembre 2012 - 01:34 .


#3
henesua

henesua
  • Members
  • 3 878 messages
(1) the label before the function name defines the type of value returned by the function. In your case this should be string
(2) objeo as you have described it is a string variable, not a function which returns a string.

if your function named treasure is to return a string (I assume the string is a resref for a treasure blueprint), then you should do as follows:

string treasure()
{
  string objeo; // this is a variable. it will hold the string that is returned by the function treasure
  int azarss =Random(3)+ 1;
  if (azarss==1)
    objeo = "a";
  else if (azarss==2)
    objeo = "b";
  else if (azarss==3)
    objeo = "c";

  return objeo;
}

[edit: ooops. failed bard got to this first. ]

Modifié par henesua, 08 novembre 2012 - 01:34 .


#4
Failed.Bard

Failed.Bard
  • Members
  • 774 messages
I didn't explain the label part, and likely should have. Also, Henesua, yours uses the if/else if chain, and mine switch/case, so likely both are handy to have as examples regardless.

#5
Nebril2

Nebril2
  • Members
  • 59 messages
Well that was a fast solution :) worked!

Thank you very much guys