returning strings (SOLVED)

Started by xelanoimis, Fri 22/10/2004 15:37:59

Previous topic - Next topic

xelanoimis

Hi!
I want to know if I can create functions that return strings.
If possible, what if the returned string is declared inside the function?
like:
function GetSomeName ()
{
string str = "Alex";
  return str;
}

What about arrays (that are declared inside the function) ?

Scorpiorus

#1
Hello, and welcome to the forums! :)

QuoteI want to know if I can create functions that return strings.
If possible, what if the returned string is declared inside the function?
It's not possible to return strings or arrays that are declared within the function.

To return string data you must declare a string outside a function and then pass it as an out parameter, so that a function can fill it with the data you need:

function GetSomeText(int someParam, string buffer) {

Ã,  Ã,  if (someParam) StrCopy(buffer, "some text");
Ã,  Ã,  else StrCopy(buffer, "nothing");
}

function repeatedly_execute() {


Ã,  Ã,  string buf;Ã,  // declaring a string

Ã,  Ã,  GetSomeText(1, buf); // getting string through that out parameter;

Ã,  Ã,  SetLabelText(GUI, LABEL, buf); // using it;
}


QuoteWhat about arrays (that are declared inside the function) ?
To return an array of data you have to declare a global array and write into it in a server function and then read from in a client function:

int glArray[10];

function GetArrayData(int someParam) {

Ã,  Ã,  if (someParam)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  glArray[0 ] = 0;
Ã,  Ã,  Ã,  Ã,  glArray[1] = 1;
Ã,  Ã,  Ã,  Ã,  glArray[2] = 2;
Ã,  Ã,  }
}


function on_some_event() {


Ã,  Ã,  GetArrayData(1);

Ã,  Ã,  int x = glArray[0 ];
Ã,  Ã,  int y = glArray[1];
Ã,  Ã,  int z = glArray[2];
}


You can also use GlobalInts as a global array (see SetGlobalInt/GetGlobalInt in the manual). But keep in mind that arrays are not officially supported and that there is no check carried out if you write outside of array bounds:

int array[2];

...

array[10] = 1; // no error generated!!


SMF spam blocked by CleanTalk