[SOLVED] How do I return a string from a function?

Started by bx83, Sun 11/04/2021 12:13:26

Previous topic - Next topic

bx83

I feel stupider even saying this, but how is this possible?

Code: ags
function GetString()
{
  String s;
  switch(Random(2)) {
    case 0: s="this is text"; break;
    case 1: s="This is also some text"; break;
    case 2: s="some more text"; break;
  }
  return s;
}


"Error (line 9): Type mismatch: cannot convert 'String*' to 'int'"

I'm missing something. Do people actually using callback arguments for this?

Gilbert

AFAIK, the keyword 'function' is the same as 'int', so the returned value can only be int.
I'm not sure whether String can be used as return type, but try 'String GetString()' instead. and see whether it works.

Crimson Wizard

#2
Quote from: Gilbert on Sun 11/04/2021 17:25:56
AFAIK, the keyword 'function' is the same as 'int', so the returned value can only be int.
I'm not sure whether String can be used as return type, but try 'String GetString()' instead. and see whether it works.

Anything can be used as a return type; except regular arrays because they cannot be assigned.
It's just that "function" is an alias to "int" for historical reasons.

So yes, you just declare your function with "String" instead of "function".


PS. Also apparently this is still not explained in the manual, so I'll have to add this.

bx83


SMF spam blocked by CleanTalk