What kind of function type is this?

Started by abstauber, Fri 10/10/2008 08:55:03

Previous topic - Next topic

abstauber

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).

Code: ags

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!

Gilbert

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".

abstauber

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


Gilbert


Khris

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).

abstauber

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 :)

monkey0506

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. ;)

Pumaman

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 ;)

zander8778

I am a bit confused about this.  I have the following setup.

In ParserFunctions.asc
Code: AGS

String OfficeBasementParser()
{
  return "";
}


In my room script
Code: AGS

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.

Gilbert

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
String OfficeBasementParserResult;
function OfficeBasementParser()
{

  OfficeBasementParserResult = "";

}

Code: AGS
OfficeBasementParser()
String response = OfficeBasementParserResult;


People more knowledgeable to the structure of the text script could correct me or clarify more on this.

Crimson Wizard

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

import function OfficeBasementParser();

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

import String OfficeBasementParser();

zander8778

You were correct Crimson Wizard.  I did not have it imported correctly.  Thanks for your help.

Knox

I use void instead of function, well cause Monkey uses that and he's cool.
--All that is necessary for evil to triumph is for good men to do nothing.

Khris

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.

Knox

--All that is necessary for evil to triumph is for good men to do nothing.

Crimson Wizard

#15
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.

SMF spam blocked by CleanTalk