Passing Strings into functions not working

Started by Reid, Sun 07/10/2007 03:46:29

Previous topic - Next topic

Reid

Hi all, I get the following error:

Code: ags

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:

Code: ags

setupConvoSituation("blah");


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

Code: ags

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:

Code: ags

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

Khris

Remove "const" from the function and the import line.

Reid

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:

Code: ags

function setupConvoSituation(String convoSituation) 


Quote from: KhrisMUC on Sun 07/10/2007 04:47:45
Remove "const" from the function and the import line.

monkey0506

#3
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:

Code: ags

setupConvoSituation("blah");


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

Code: ags

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

Reid

Oh jeez, thanks for the clarification. Big help.


monkey0506

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

SMF spam blocked by CleanTalk