Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Reid on Sun 07/10/2007 03:46:29

Title: Passing Strings into functions not working
Post by: Reid on Sun 07/10/2007 03:46:29
Hi all, I get the following error:


There was an error compiling your script. The problem was:
In: 'Global script'
Error (line 118): Type mismatch: cannot convert 'const string' to 'const String*'


line 118 is this:


setupConvoSituation("blah");


then a few lines below in the same global script I have:


function setupConvoSituation(const String convoSituation)

 
if (convoSituation == "blah")
  {
    //setup buttons and labels and anything else
    Display( "CONVO SITUATION IS IN THE CONTAINER");
  }
}


This is what I have in my script header:


import function setupConvoSituation (const String convoSituation);


What I want to do is call the function setupConvoSituation and pass in some text. Based on the text, I'll change button labels and dialog responses characters say.

Thanks for any help!

-Reid
Title: Re: Passing Strings into functions not working
Post by: Khris on Sun 07/10/2007 04:47:45
Remove "const" from the function and the import line.
Title: Re: Passing Strings into functions not working
Post by: Reid on Sun 07/10/2007 05:50:44
Thanks, I had tried that before. I get this error msg now:

Quote
In: 'Global script'
Error (line 124): Already referenced name as import; you must define it before using it

and line 124 in my global script is:


function setupConvoSituation(String convoSituation)


Quote from: KhrisMUC on Sun 07/10/2007 04:47:45
Remove "const" from the function and the import line.
Title: Re: Passing Strings into functions not working
Post by: monkey0506 on Sun 07/10/2007 06:21:05
You have to define the function before you call it (even though it has been imported, it still must be defined before it is called (by any script)). You said it all yourself:

Quote from: Reid on Sun 07/10/2007 03:46:29line 118 is this:


setupConvoSituation("blah");


then a few lines below in the same global script I have:


function setupConvoSituation(const String convoSituation)

 
if (convoSituation == "blah")
  {
    //setup buttons and labels and anything else
    Display( "CONVO SITUATION IS IN THE CONTAINER");
  }
}

Simply move the "setupConvoSituation" function somewhere above line 118.

P.S. You really only need to use const when using an old-style string (note the lower-case 's' as opposed to the new-style String) as a function parameter where it might also require/accept a new-style String. If the script you're working on is for your own use only, I would suggest making sure that 'Enforce new-style Strings' is turned on, and then avoiding the old-style string type altogether (meaning you won't need to use const either).
Title: Re: Passing Strings into functions not working
Post by: Reid on Sun 07/10/2007 06:31:38
Oh jeez, thanks for the clarification. Big help.

Title: Re: Passing Strings into functions not working
Post by: monkey0506 on Sun 07/10/2007 06:44:47
Glad I could be of service to you. 8)

Regarding the const word, its usage in AGS can be confusing if you've used other scripting languages before, but it was only implemented for backward compatibility when the new-style Strings were introduced (between old- and new-style strings (attempting to port the "const" keyword to older scripts would actually fail ;))). I'm actually not sure off the top of my head, but I don't think (not 100% sure) you can pass a string-literal (i.e., the text "blah" inside quotes) through an old-style string parameter unless it's been made const, though it will work fine if you use a new-style String parameter (which should not be const).