SOLVED Null string referenced?

Started by alkis21, Sun 06/05/2007 18:19:06

Previous topic - Next topic

alkis21

A certain action makes my game crash for no apparent (to me) reason. Here is my code:

Code: ags
  // script for Hotspot 2 (bookcase): Use inventory on hotspot
if (player.activeinv==1) {
   if (StrCaseComp("Curtis' favor", thought)==0) {
      player.Walk(122, 280, eBlock);
      player.FaceLocation(122, 100, eBlock);
      Wait(20);
      player.Walk(171, 280, eBlock);
      player.FaceLocation(171, 100, eBlock);
      Wait(20);
      player.Walk(255, 280, eBlock);
      player.FaceLocation(255, 100, eBlock);
      Wait(20);
      Display("You spot a book titled 'Chants of Experience', by William Flake. That looks like something Curtis would enjoy.");
      Display("You take it with you.");
      AddInventory(17);
      GiveScore(6);
      Thou10.Visible=false;
      }
   else Display("That provides no further insight.");
   }
else if (player.activeinv==16) Display("That's not the way to return a book.");
else Display("You can't use that here."); 


Using inventory item 1 on that hotspot returns the following message, regardless of whether the StrCaseComp is 0 or 1:
Quote
An internal error has occured. Please note down the following information.
If the problem persists, contact Chris Jones.
(ACI version 2.71.894NMP)

Error: run_text_script1: error -6 running function 'hotspot2_c':
Error: Null string referenced
in Room 66 script (line 50)

Line 50 of the room script is the following:

Code: ags
if (StrCaseComp("Curtis' favor", thought)==0) {


I don't understand why it doesn't work, as the exact same line works perfectly in other places.

Any help will be appreciated.

SupSuper

That means that the thought string has no value (null) when the comparison is done. Either make sure you assign it a value at start - "StrCopy(thought,"");" - or change the comparison to this - "if ((thought != null) && (StrCaseComp("Curtis' favor", thought)==0))".
Programmer looking for work

alkis21

String thought is declared in the Script Header, and I've used it in various parts of the game with no problems.

In the Script Header:
Code: ags
String thought;


In the Global Script:
Code: ags
function game_start() {
  // called when the game starts, before the first room is loaded
thought="yourself";


Come to think of it, I've never used it in a room script before. Does this mean I can't? Is there no way around this?

Ashen

You're having the same problem as police brutality had with ints in this thread. Because you've declared the String in the Script Header, each script has it's own instance (i.e. the thought that you set in the Global Script isn't the same one you're checking in the room script). To make the String properly global (so you can use it in room scripts), do as KhrisMUC said in the other thread (and as is in the BFAQ).

Also, please post error messages as text, not images. Thank you.
I know what you're thinking ... Don't think that.

alkis21

So according to KhrisMUC in the thread you mention, I am supposed to put:

Code: ags
String thought;
export thought;


at the top of the global script

and then:

Code: ags
import String thought;


into the script header.

However, the above returns the following error message:

Local variable cannot have the same name as an import

Khris

Did you remove the string declaration from the script header?

alkis21

I'm not sure what you mean, this is what my script header looks like now:

Code: ags
// Main header script - this will be included into every script in
// the game (local and global). Do not place functions here; rather,
// place import definitions and #define names here to be used by all
// scripts.
import String thought;
import function GetScale();

Khris

Then you are probably declaring the String in the room script.
As it says in the error message, you're declaring a local variable (=inside the room script) using a name already taken.

Look for a line containing "string thought;" in the room script and remove it.

alkis21

#8
I wish it were that simple... I'm positive that I haven't declared 'thought' anywhere else. I've been using it for ages in GUIs and character interactions, today was the first time I tried it in a room script. I checked the room script and of course it's not there, just the code I wrote in my first post.

I'm wondering if Game.GlobalStrings[] might help... but that would mean changing a large part of my script, so I'd really appreciate an alternative.

EDIT:

OK guys I think I got it to work! In a very weird way....
I switched to my original code, and I put:

In the Script Header:
Code: ags
String thought;


In the Global Script:
Code: ags
function game_start() {
  // called when the game starts, before the first room is loaded
thought="yourself";


Then in function repeatedly_execute_always(), I put:

Code: ags
Game.GlobalStrings[1]=thought;


And finally, in the room script I put:

Code: ags
if (StrCaseComp("Curtis' favor", Game.GlobalStrings[1])==0) {


And it worked! Everything seems to be in order, but I have to test it some more to be sure. Can anyone think of a reason why this weird solution might fail somewhere?

SMF spam blocked by CleanTalk