Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: pslim on Tue 27/06/2006 04:30:06

Title: String problem in 2.71 (SOLVED)
Post by: pslim on Tue 27/06/2006 04:30:06
I'm trying to declare some Strings in my global script in preparation for implementing a journal in my game.

The manual says:

Upgrading to AGS 2.71
AGS 2.71 adds new simple string support to the scripting language. Strings have long been a pain to use in AGS, but this is finally addressed by v2.71.
There's a new String type (that's a capital 'S'). These new strings behave like Java/C# strings in that you can easily assign to and manipulate them.

For example, in 2.7 and previous versions, you had to do this:


string text;
StrCopy(text, "This is my text");

in 2.71, you can now do:
String text = "This is my text";



However, when I put the following code as a test, I got an error message when attempting to compile:

String message = "This is my message";

The error said, "Error in line 3. Cannot assign initial value to global pointer"

I tried doing just

String message;

And that worked but then I'm not sure how to tell it what value the string is supposed to have, and the manual seems to be under the impression I wouldn't have to do it like that anyway.

Clearly I'm missing something obvious, but I'm too inexperienced with scripting to figure out what. Any help would be appreciated.

Title: Re: String problem in 2.71
Post by: Akumayo on Tue 27/06/2006 04:41:49
You say that simply
String message;
compiled fine, right?

Try setting the value in the game_start() script:

message.Text = "This is some text."


I think that'll work.
Title: Re: String problem in 2.71
Post by: pslim on Tue 27/06/2006 04:52:10
Quote from: "Acqua" Akumayo on Tue 27/06/2006 04:41:49Try setting the value in the game_start() script:


message.Text = "This is some text."


Hmm, when I tried to compile after adding that it said "Text is not a public member of String".Ã,  *headscratch*
Title: Re: String problem in 2.71
Post by: Akumayo on Tue 27/06/2006 04:57:23
Try this in your declaration:

String message = String.Format("This is a message.");
Title: Re: String problem in 2.71
Post by: pslim on Tue 27/06/2006 05:01:35
I entered

String journal_1 = String.Format("This is a test entry.");

and got the same "cannot assign initial value to global pointer" error message I got using the example from the manual.

Am I trying to put them in the wrong place?

I'm putting them here:

// main global script file

String journal_1 = String.Format("This is a test entry.");

#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() { // called when the game starts, before the first room is loaded




Thanks for your help so far.
Title: Re: String problem in 2.71
Post by: Gilbert on Tue 27/06/2006 05:20:07
That's because, even though we have "friendly" Strings support, it's still a pointer (a struct actually) which can't be assigned initial value during declaration. So you can do, say:

// main global script file

String journal_1; //DON'T assign value to String during declaration

#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() { // called when the game starts, before the first room is loaded
journal_1 = "This is a test entry."; //No, you don't need to use Format here, really, unless you really want to "format" Strings
}
Title: Re: String problem in 2.71
Post by: pslim on Tue 27/06/2006 05:24:16
Thanks, Gilbot. That worked.


However, I'm thinking the manual entry should be edited to reflect what actually works.


Quote from: The Manualin 2.71, you can now do:
String text = "This is my text";

Quote from: Gilbot V7000ait's still a pointer (a struct actually) which can't be assigned initial value during declaration.

:=
Title: Re: String problem in 2.71 (SOLVED)
Post by: Kweepa on Tue 27/06/2006 06:20:26
To clarify:


// this fails
String globalText = "Square on the proboscis";

function string_manipulate()
{
Ã,  // this works
Ã,  String localText = "Sum of the other two sinuses";
Ã,  ...
}


So the manual is somewhat correct.
Title: Re: String problem in 2.71 (SOLVED)
Post by: Gilbert on Tue 27/06/2006 07:49:46
Yeah, because assign value to String is something different to just a integral value. I think internally the engine just does something like the good old StrCopy(blah, "haha"); when you do blah = "haha";.
So assignment of initial value can't be done outside of functions where you can execute functions.