[Solved] Dynamic arrays - Null pointer referenced

Started by Glenjamin, Tue 18/12/2018 20:11:17

Previous topic - Next topic

Glenjamin

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

//define health
int NPHealth[];
 

then in game_start, I include the following:
Code: ags
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
 cCharacter.Say("health is %d",NPHealth[5]);


Code: ags


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.




Crimson Wizard

#1
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.

morganw

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.

Khris

Script header:
Code: ags
import int NPHealth[];


Script body:
Code: ags
int NPHealth[];
export NPHealth;

void game_start() {
  NPHealth = new int[Game.CharacterCount];
}

Glenjamin

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
 cCharacter.Say("health is %d",NPHealth[5]);


Code: ags
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!

Khris

#5
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.

Crimson Wizard

#6
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.

Khris

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.

Glenjamin

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!

SMF spam blocked by CleanTalk