Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: abstauber on Fri 10/10/2008 08:55:03

Title: What kind of function type is this?
Post by: abstauber on Fri 10/10/2008 08:55:03
I recently modified hacked (as this sound more cool) SSH's QueueSpeech to run with AGS 3. It all works, so I'll be fine, but I've stumbled across these kind functions (I assume, they are functions).


String qAddScore(int points) {
  //StrFormat(str_delay, "add-score %d", points);
  str_delay = String.Format("add-score %d", points);
  squeue.noanim[squeue.insert % SPEECH_QUEUE_SIZE] = 1;
  return str_delay;
}


I actually don't get what's going on here. What kind of string is this? Just a very long, clever looking string? Or some kind of anonymous function?
Could anyone explain this to me?
Thanks!
Title: Re: What kind of function type is this?
Post by: Gilbert on Fri 10/10/2008 09:19:02
I don't know about the module, but I hope I can grasp your question properly.

By asking "what kind of string" do you mean the variable type keyword "String" before the declaration of the function qAddScore()?

That "String" just indicates that the return type of the function is "String".
In AGS, the default return type of a function is "int", if you want a function to return other kinds of variables you just use the variable type keyword instead of "function" to declare it, like "short blah()", "float blah()", "String blah()".
(So, in fact the keyword "function" is a bit obsolete, as "int blah()" is equivalent to "function blah()", it would make all people happy to have the keyword "function" though.)

Hence, what that function does is just to set the value of some array element (squeue.noanim[squeue.insert % SPEECH_QUEUE_SIZE]) of a stuct member and return the String "add-score xxx".
Title: Re: What kind of function type is this?
Post by: abstauber on Fri 10/10/2008 10:16:34
Wow thanks, it's that easy?!

So if I use "function", I can only return integers and if I use a data type instead of the keyword function, I'll get a differnt returning type?

Weird  ;D

Title: Re: What kind of function type is this?
Post by: Gilbert on Fri 10/10/2008 11:12:59
yeah. That's how it works.
Title: Re: What kind of function type is this?
Post by: Khris on Fri 10/10/2008 11:55:36
Weird? This is pretty much the best thing about the script language :)
Remember, function is just a substitute for int, so you're always using a datatype (except if you use void when you don't need to return anything).
Title: Re: What kind of function type is this?
Post by: abstauber on Fri 10/10/2008 12:18:51
I never got this deep into C  :-[
The languages I use to work with have function as a reserved keyword (like procedure or class), not as a substitue for int.
But I'll never stop learning :)
Title: Re: What kind of function type is this?
Post by: monkey0506 on Fri 10/10/2008 18:59:23
At the time the function keyword was introduced AGS wasn't OO, there was no such thing as a pointer in AGS, you couldn't return a string from a function at all, so it wouldn't make sense really to require a return type (the only return type really supported was int anyway).

Since then AGS has changed a lot and the function keyword I suppose remains for backwards compatibility and the comfort of those just learning to script. But as has been mentioned you can return any type of variable (or pointer) from a function if you do specify a return type instead of the keyword. ;)
Title: Re: What kind of function type is this?
Post by: Pumaman on Fri 10/10/2008 20:16:30
Yeah, originally AGS only supported returning ints from functions, so "function" implied that it was going to return an int.

Then support for returning other types was added -- in hindsight maybe it would have been better to have a syntax like:

function String qAddScore(int points) {

but we are where we are ;)
Title: Re: What kind of function type is this?
Post by: zander8778 on Mon 10/12/2012 06:32:35
I am a bit confused about this.  I have the following setup.

In ParserFunctions.asc
Code (AGS) Select

String OfficeBasementParser()
{
  return "";
}


In my room script
Code (AGS) Select

String response = OfficeBasementParser();


When I try to build I get "Type mismatch: cannot convert 'int' to 'String*'"  I am sure that I am making a silly mistake because I am very new to the scripting language.

Thanks in advance.
Title: Re: What kind of function type is this?
Post by: Gilbert on Mon 10/12/2012 06:54:46
I think return values of a function can only be a basic type like int, short, char, bool and float. String is sort of a wrapper on a pointer and this is not supported as a return type.

As a (untested) workaround, you may use an intermediate temporary String to hold the result:
Code (AGS) Select
String OfficeBasementParserResult;
function OfficeBasementParser()
{

  OfficeBasementParserResult = "";

}

Code (AGS) Select
OfficeBasementParser()
String response = OfficeBasementParserResult;


People more knowledgeable to the structure of the text script could correct me or clarify more on this.
Title: Re: What kind of function type is this?
Post by: Crimson Wizard on Mon 10/12/2012 08:34:28
You can return String from function, as well as object pointers (Character*, DynamicSprite*, etc).

What I suspect there is that function is imported wrong way.
Can it be you imported it like:
Code (ags) Select

import function OfficeBasementParser();

The return type must be the same as in function definition:
Code (ags) Select

import String OfficeBasementParser();
Title: Re: What kind of function type is this?
Post by: zander8778 on Mon 10/12/2012 14:12:26
You were correct Crimson Wizard.  I did not have it imported correctly.  Thanks for your help.
Title: Re: What kind of function type is this?
Post by: Knox on Mon 10/12/2012 17:30:39
I use void instead of function, well cause Monkey uses that and he's cool.
Title: Re: What kind of function type is this?
Post by: Khris on Mon 10/12/2012 19:31:53
You can only use void if the function isn't supposed to return a value. "function" is a substitute for "int", not "void".
AGS allows the function to not return anything even if the return type is stated, thus using void isn't prevalent, although it is the correct way in that case.
Title: Re: What kind of function type is this?
Post by: Knox on Tue 11/12/2012 02:41:19
...and it makes you cool.
Title: Re: What kind of function type is this?
Post by: Crimson Wizard on Tue 11/12/2012 07:17:22
Quote from: Khris on Mon 10/12/2012 19:31:53
AGS allows the function to not return anything even if the return type is stated,
Something I reported as bug couple of years ago, because it makes it easy to miss return statement in some conditional branch, which may lead to weird results.