PDA

View Full Version : Undesired Constant String [NEW problem]



dazappa
11-11-2009, 11:55 PM
Well, this is a new one. Basically, it won't let me have a string longer than 255 chars long, because it is, for some reason, constant.

type
TRoom = Class
public
n,s,e,w: string;
objects: array[0..10] of integer;
objectloc: array[0..10] of string;
title: string;
rdesc: string;
end;

Why would it be considered a constant? And is there any way to change that? Any other code you think would be necessary to see?

(I seem to manage fairly well in learning new languages until these little nuances come up :()

paul_nicholls
12-11-2009, 12:05 AM
Considered a constant? wierd...

Do you have long strings (AnsiStings) off by default in that project?

If so, then the default string is a ShortString (only 255 characters).

Not sure about the constant issue though...

cheers,
Paul

dazappa
12-11-2009, 12:22 AM
Considered a constant? wierd...

Do you have long strings (AnsiStings) off by default in that project?

If so, then the default string is a ShortString (only 255 characters).

Not sure about the constant issue though...

cheers,
Paul

I don't see any options to turn them on/off. Compiling with the latest public release of FPC (2.2.4) from the command line yields the same error, "Error: Constant strings can't be longer than 255 chars"

(Happens when assigning a string to rooms[0].w, to be more exact).

I figured I should also mention that while my first post shows TRoom as being a class, it was originally a record and I've changed it back to a record. (I try random things sometimes, quite often with no real reasoning behind it.)

paul_nicholls
12-11-2009, 12:35 AM
You can use
{$H+}

and

{$H-}

to turn on/off huge-strings respectively in your code :)

I always add in {$H+} at the top of my units/project file to make sure like so:

Unit state_playgame;
{$IFDEF fpc}
{$mode delphi}
{$ENDIF}
{$H+}
Interface

Uses

cheers,
Paul

dazappa
12-11-2009, 01:32 PM
Thanks, but that did not fix the problem. I even tried making a local variable, which gave the same error.

paul_nicholls
12-11-2009, 08:12 PM
I think you need to show us your actual code so we can help you better, like the class, and how it is being used.

cheers,
Paul

dazappa
12-11-2009, 08:39 PM
This is the unit in its entirety: http://pastebin.com/m7e4891a6
Look for procedure LoadArea1

paul_nicholls
12-11-2009, 09:27 PM
Thanks chief :)

I will have a quick look for you :)

EDIT: it compiles just fine for me under Lazarus 0.9.28.2 (fpc 2.2.4) after I quickly added the unit to an existing project...

It also compiles using Delphi 6 too.

Here is the screenshot from Delhi 6:

http://img27.imageshack.us/img27/1712/screenshottp.png

EDIT#2 OOPS! I didn't notice your commented out line at the bottom of the code...sorry!!

Ok, now I get this error (Delphi - seems a bit more descriptive than your fpc error):

"String literals may have at most 255 elements"

In Delphi at least (and guessing fpc too), this means you need to break up string literals (or constants) into 255 or smaller chunks like so:

rooms[0].w := '2[&]2[&]You turn your head to the west. The immense iron bars of the cell door are impassable. The door is locked.[&]You slide the crudely made key into the lock. As you turn the bone key, it snaps in half. '+
'Luckily for you, the door was unlocked just before the key broke.';


I hope this helps :)

BTW, I'm not sure what the "2[&]" parts are for in the string...some sort of commands that you interpret via code?

cheers,
Paul

dazappa
12-11-2009, 11:53 PM
Ok, now I get this error (Delphi - seems a bit more descriptive than your fpc error):

"String literals may have at most 255 elements"

In Delphi at least (and guessing fpc too), this means you need to break up string literals (or constants) into 255 or smaller chunks like so:

rooms[0].w := '2[&]2[&]You turn your head to the west. The immense iron bars of the cell door are impassable. The door is locked.[&]You slide the crudely made key into the lock. As you turn the bone key, it snaps in half. '+
'Luckily for you, the door was unlocked just before the key broke.';


I hope this helps :)

BTW, I'm not sure what the "2[&]" parts are for in the string...some sort of commands that you interpret via code?

cheers,
Paul

Yes! :D Splitting it up like that worked perfectly. Now if only fpc could adopt more descriptive errors like Delphi's xx(

(And yes, I plan on interpreting it via code. See this line: "// room[&]key[&]Descrip[&]Enter key descrip". That's a note to self to remember how it's structured. It's mostly for the door/key system, but I think I'll expand it later as well.)

paul_nicholls
13-11-2009, 12:12 AM
Glad I could help, and good luck with your entry :)

cheers,
Paul

dazappa
15-11-2009, 06:03 AM
After staring at my screen in awed confusion for hours, I have discovered that when passing strings longer than 255 chars to functions, they're cut to 255 chars... :X Is there a solution for this one?

And there I was, thinking writing a text game was going to be extremely difficult...

Edit: Well, I believe the problem is solved now. Changed the variables to ansistrings, left the function parameters as strings and for the time being it's working... xx(

User137
15-11-2009, 08:12 AM
If variables are big it is way more effective to pass only pointers to functions. They don't mind how big the original data is, 1 byte or million bytes ;)

Edit: Usual way to name strings for pointers is PChar.