[SOLVED] String not working

Started by Gepard, Fri 04/11/2016 14:56:55

Previous topic - Next topic

Gepard

I have a string defined in global script header called "Interacting With". However, when I try to run the IndexOf function, it says null pointer referenced. It is just one line of code, but I have no idea, what is wrong.

Code: ags
  if (InteractingWith.IndexOf("panel_r1")>=0) {} 
Drink up me 'arties! Yo ho!

Crimson Wizard

#1
This means that you did not set any actual string to InteractingWith yet. "String" is actually a pointer, which means that if no value was assigned, it is like space, nothingness.

If according to game logic the string can be "null", then check it before using
Code: ags

if (!String.IsNullOrEmpty(InteractingWith))
{
     if (InteractingWith.IndexOf("panel_r1")>=0) {} {
     ..............
}


Alternatively you could assign empty string value in game_start:
Code: ags

InteractingWith = "";


Gepard

#2
The InteractingWith string is set when a player clicks on an object in in a room:
Code: ags
function oPanel_AnyClick()
{
 InteractingWith = "panel_r1";
 gInventory.Visible = true;
 btnInteracting.NormalGraphic = 53;
}


Than other string F1 is set when a player clicks on an inventory item in inventory window. Than, when player click on a button, this code should start:

Code: ags
    Display ("%s", InteractingWith);
    Display ("%s", F1);
  if (InteractingWith.IndexOf("panel_r1")>=0) {
    if (F1 == "open" && F2 == "door") player.ChangeRoom (2, 63, 168);
    }


The Display lines are there for control only. The weird thing is that the InteractingWith string wont even be displayed. Only the F1 will and than nothing happens. However, when I tried to put the same display line for InteractingWith right after it is set, it will display no problem.

Sometimes I get this error, but not always:

Error: Error running function 'Button1_OnClick':
Error: ScriptSprintf: argument 0 is expected to be a string, but it is null pointer
Drink up me 'arties! Yo ho!

Crimson Wizard

#3
I cannot tell straight away, maybe your functions get called in unexpected order?

Quote from: Gepard on Fri 04/11/2016 18:31:00
Sometimes I get this error, but not always:

Error: Error running function 'Button1_OnClick':
Error: ScriptSprintf: argument 0 is expected to be a string, but it is null pointer

Once again, when you create a String variable, but before assigning any value, it is "null pointer" and cannot be used for anything. So you need to either assign any value (even if empty string: "") at the start of the game, or make checks with String.IsNullOrEmpty where you are using it to make sure it has anything assigned. Otherwise you will be getting these errors all the time.

Gepard

Code: ags
    Display ("%s", InteractingWith);
    Display ("%s", F1);
  if (!String.IsNullOrEmpty(InteractingWith)) Display ("Yes it is.");
  if (InteractingWith.IndexOf("panel_r1")>=0) {
    if (F1 == "open" && F2 == "door") player.ChangeRoom (2, 63, 168);
    }


I did as you said. Defined the InteractingWith at the game start, but than again when the string is set by player clicking on an object, it does display correctly. The above code only displays F1 string. Im hopeless :confused:
Drink up me 'arties! Yo ho!

Crimson Wizard

Quote from: Gepard on Fri 04/11/2016 19:03:22
I did as you said. Defined the InteractingWith at the game start, but than again when the string is set by player clicking on an object, it does display correctly. The above code only displays F1 string. Im hopeless :confused:
Where is this code located? Do you reset InteractingWith anywhere?

Gepard

#6
That's the thing. The only time I set InteractingWith is on game start and when player clicks on the object. This code is when player clicks on a button located on inventory GUI. I think the problem is that the String is set on object click. Because when I set the String "panel_r1" on game start, the code works no problem.
Drink up me 'arties! Yo ho!

Crimson Wizard

Quote from: Gepard on Fri 04/11/2016 19:29:54
That's the thing. The only time I set InteractingWith is on game start and when player clicks on the object. This code is when player clicks on a button located on inventory GUI. I think the problem is that the String is set on object click. Because when I set the String "panel_r1" on game start, the code works no problem.
Hmm, how this variable is declared and imported/exported? Maybe there are two variables with the same name in two scripts (oPanel_AnyClick is in the room script, right?).

Gepard

Well, in global script header I have:
String InteractingWith;
and in global script I have: InteractingWith = "";

And yes, oPanel_AnyClick is room script.
Drink up me 'arties! Yo ho!

Crimson Wizard

#9
Quote from: Gepard on Fri 04/11/2016 20:02:50
Well, in global script header I have:
String InteractingWith;
So here is the problem. When you declare variables in global header like that, it works as separate variable declaration for every script that sees global header (all the room scripts). In other words you are using different variable in every room script.
To make room scripts use only one variable, you declare it inside global script, and then do import/export:

Code: ags

// In GlobalScript.ash:
import String InteractingWith;

// In GlobalScript.asc:
String InteractingWith;
export InteractingWith;



Alternatively, you may open Global Variables pane from the project tree, and create global variables there.

Gepard

I feel so stupid. Thank you again! It's been a long time since I've used AGS. Thank you!
Drink up me 'arties! Yo ho!

SMF spam blocked by CleanTalk