String command Problem

Started by Ghostlady, Fri 15/06/2007 04:20:50

Previous topic - Next topic

Ghostlady

What is different with the "string" command in V2.72 from V2.71?  I am trying to put conversation text on a gui, the way I did it using the prior version and I am getting this error:

Type mismatch:  Cannot convert 'const string' to 'string'

Global Script:
function SaySpecial(Character *thechar, string message) {
  slabel.TextColor = thechar.SpeechColor;
  slabel.SetText(message);
  thechar.SayAt(0, 0, 0, message); // say text but hidden (width 0), so voice speech plays
  slabel.SetText("");

Script Header:
import function SaySpecial(Character *thechar, string message); //make function useable in room scripts as well

Room Script:
// script for Room: Player enters room (after fadein)
    gGui4.Visible = true;
    SaySpecial(cSarah, "Here at last.");
    gGui4.Visible = false; }   
}
My Games:

Hauntings Of Mystery Manor
Intrigue At Oakhaven
Haunting at Cliffhouse

Gilbert

Try to capitalise the S in string.
Code: ags

function SaySpecial(Character *thechar, String message) {
  slabel.TextColor = thechar.SpeechColor;
  slabel.SetText(message);
  thechar.SayAt(0, 0, 0, message); // say text but hidden (width 0), so voice speech plays
  slabel.SetText("");

monkey0506

AGS 2.71 and later uses the String type instead of the string type (note capitalization (it matters!)). If you still use the old-style strings in your scripts, simply change "string message" to "const string message" in your functions and voila!

If you plan on upgrading to the new String type I would suggest using a String as the parameter as opposed to a const string. It helps enforce the changes (if you're wanting to make them that is), and you can always pass an old-style string through a String parameter like this:

Code: ags
function SomeFunc(String param) {
  // ...
  }

function OtherFunc() {
  string str;
  StrCopy(str, "This is some text!");
  SomeFunc(String.Format("%s", str));
  }

Ghostlady

Capital S made a big difference.  I'll play around with the new formatting of Strings.  Thanks to the both of you. 
My Games:

Hauntings Of Mystery Manor
Intrigue At Oakhaven
Haunting at Cliffhouse

monkey0506

#4
If you want to upgrade, try making sure "Enforce new-style strings" is checked in your General Settings pane. That should throw up lots of things for you to check into. But basically the new String type works like this:

Code: ags
string str;                             -->   String str;
StrCopy(str, "This is some text!");     -->   str = "This is some text!";
StrCat(str, " And this is some more."); -->   str = str.Append(" And this is some more.");
string otherStr;
StrFormat(otherStr, "%d", some_int);    -->   String otherStr = String.Format("%d", some_int);
StrCopy(otherStr, str);                 -->   otherStr = str;
if (StrComp(str, otherStr) == 0) {      -->   if (str.CompareTo(otherStr) == 0) {
                                        --> // (also:) if (str == otherStr) {
  Display(str);                         -->   Display(str);
  }


Something you should note is that the String type is internally defined as a pointer. If you don't understand pointers that's all-right, the only part you really need to know is that if you type:

Code: ags
String str;


Then the variable str will actually hold the value null. You cannot operate on null Strings! Attempting to do so will crash your game. As long as the variable is not global (you are defining it within a function) you can assign it a default value automatically, i.e.:

Code: ags
// inside some function
String str = "This is some text!";

// outside of all functions
//String g_str = "This is some text!"; // this will produce an error, use:
String g_str; // instead and set the initial value inside a function, such as game_start


For a full list of the new functions, simply look up "String functions" in the manual (for AGS 2.71 or later ;)).

[EDIT:]

This may be useful...:o...holy hell I have way too much free time.

Ghostlady

Ok, this is good.  I do understand pointers.

I didn't realize there was another manual other than the one that comes with the software.  This is even a bigger help.
My Games:

Hauntings Of Mystery Manor
Intrigue At Oakhaven
Haunting at Cliffhouse

SMF spam blocked by CleanTalk