Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Glenjamin on Tue 18/12/2018 20:11:17

Title: [Solved] Dynamic arrays - Null pointer referenced
Post by: Glenjamin on Tue 18/12/2018 20:11:17
Hi all,

Im working on a game with several characters. Each character has a set of variables that impact their behaviors.

To tackle this, I figured I'd use "Dynamic arrays" straight from the dynamic help section of the engine.

In a script, I define the variable array. In this case it's health.

Code (ags) Select

//define health
int NPHealth[];
 

then in game_start, I include the following:
Code (ags) Select
NPHealth = new int[Game.CharacterCount];


Then to test if it works I set up the following: One action which tells me what a NPHealth variable is, and one which tells me what the same NPHealth variable is after subtracting 20.

Code (ags) Select
cCharacter.Say("health is %d",NPHealth[5]);


Code (ags) Select


NPHealth[5] -= 20;
cCharacter.Say("health is %d",NPHealth[5]);


I keep getting "Null pointer referenced" errors. I tried moving the location of where the array is defined but nothing has worked so far.

Thanks in advance.



Title: Re: Dynamic arrays - Null pointer referenced
Post by: Crimson Wizard on Tue 18/12/2018 20:47:39
Where do you define the array and in which scripts you use it? Which lines you are getting "Null pointer referenced" at?
There is not enough information yet, but it sounds like maybe incorrect variable import/export. For example, if you put just "int NPHealth[];" in the script header, then this variable will be duplicated in every room etc scripts instead of referencing same variable.
Title: Re: Dynamic arrays - Null pointer referenced
Post by: morganw on Tue 18/12/2018 20:48:54
I've tried it and it seemed to work okay, but the value will initialise to 0 and you didn't show how you are assigning any other value.
I think the problem is probably where you are assigning the value, rather than in the bits you have shown.
Title: Re: Dynamic arrays - Null pointer referenced
Post by: Khris on Tue 18/12/2018 21:17:47
Script header:
Code (ags) Select
import int NPHealth[];

Script body:
Code (ags) Select
int NPHealth[];
export NPHealth;

void game_start() {
  NPHealth = new int[Game.CharacterCount];
}
Title: Re: Dynamic arrays - Null pointer referenced
Post by: Glenjamin on Tue 18/12/2018 21:29:49
Sorry about the missing info.

I intend to define the arrays in their own script called "NPCvariables" to keep things neat. NPHealth is currently defined in the header of that script.

I'm getting the error for the testing lines:

Code (ags) Select
cCharacter.Say("health is %d",NPHealth[5]);


Code (ags) Select
NPHealth[5] -= 20;
cCharacter.Say("health is %d",NPHealth[5]);


Both lines are in a room script, in hotspot anyclick functions just for testing purposes.

Going to give Khris' method a shot and get back to you. Thanks so far!
Title: Re: Dynamic arrays - Null pointer referenced
Post by: Khris on Tue 18/12/2018 23:03:31
It sounds like cCharacter is null. Is it a pointer? Is it set to something?
Just use Display() instead.

Also, like CW said, declaring a variable in the header will create a separate one for each room. My code will fix that, but not the NPE.
Title: Re: Dynamic arrays - Null pointer referenced
Post by: Crimson Wizard on Tue 18/12/2018 23:14:46
Quote from: Khris on Tue 18/12/2018 23:03:31
Also, like CW said, declaring a variable in the header will create a separate one for each room. My code will fix that, but not the NPE.

Thing is, the dynamic arrays are pointers, and if they got duplicated then only one of them is actually assigned an object (probably in NPCvariables script module or global script?), and other remain NULL. This is why I suggested that null-pointer error may be related to incorrect declaration of NPHealth:

Quote from: Glenjamin on Tue 18/12/2018 21:29:49
Both lines are in a room script, in hotspot anyclick functions just for testing purposes.
Title: Re: Dynamic arrays - Null pointer referenced
Post by: Khris on Tue 18/12/2018 23:57:25
Right, I suspected as much but wasn't sure and too lazy to give it a test run.
Of course it would have helped to know at which of the two lines in the second snippet the error occurs...  :P

Edit: actually no. Oh well. Just don't declare variables in headers, is the takeaway here.
Title: Re: Dynamic arrays - Null pointer referenced
Post by: Glenjamin on Wed 19/12/2018 02:05:24
I used Khris' method and everything seems to be working according to plan.

I'll be sure to keep an eye out as to not have my variables in the header.

Thanks everyone!