I've defined a dynamic array in my global header, expecting it to be available everywhere. Instead, I seem to be having issues.
When I go to access the array from a global function (GameStart) it runs perfectly... my pointer works just as it should be. When I access the array from a room function, (a hotspot action) it tells me I'm accessing a null pointer. Could this be because the array is resetting itself each time the global header is run (and thus every time a new room is loaded)? If so, how could I go about declaring and initializing the dynamic array without resetting it each time I load my header?
I would include code, but it's scattered about 4-5 files... let me know if you need it.
You put it in the global script, not the header, then put an export myvarname; in the script after its declaration. Then you put an import TYPE VARNAME; in the header... pretty much the same as using global functions in rooms,a s described here (http://www.adventuregamestudio.co.uk/manual/Calling%20global%20functions%20from%20local%20scripts.htm)
Thanks for the quick response, SSH. Unfortunately, I did what you told me and I'm still getting the same error. Which means I probably described something incorrectly. Here's the resulting code as it stands thus far:
EmotionDialogEngine.ash
Dialog* dialogArray[20];
struct EmotionDialog
{
int dialogNumber;
int EmotionTopics[31];
import function init(int, Dialog*);
import function SetTopicEmotion(int, Emotion);
import Emotion GetTopicEmotion(int);
import function ToggleTopics();
import bool CheckTopicForCurrentEmotion(int);
import function StartDialog();
};
EmotionDialogEngine.asc
function EmotionDialog::init(int dialogID, Dialog* dlog) {
this.dialogNumber = dialogID;
dialogArray[dialogID] = dlog;
int i = 0;
while (i < 30)
{
this.EmotionTopics[i] = 0;
i++;
}
}
function EmotionDialog::SetTopicEmotion(int topic, Emotion emote) { this.EmotionTopics[topic] = emote; }
Emotion EmotionDialog::GetTopicEmotion(int topic) { return this.EmotionTopics[topic]; }
bool EmotionDialog::CheckTopicForCurrentEmotion(int topic)
{
Emotion topicEmotion = this.EmotionTopics[topic];
if(topicEmotion > 1)
if(CurrentEmotion == topicEmotion)
return true;
else
return false;
else
return false;
}
function EmotionDialog::ToggleTopics()
{
int i = 0;
while(i < 30){
if(this.EmotionTopics[i] > 0) {
Emotion topicEmotion = this.EmotionTopics[i];
if(this.CheckTopicForCurrentEmotion(i))
dialogArray[this.dialogNumber].SetOptionState(i, eOptionOn);
}
i++;
}
}
function EmotionDialog::StartDialog()
{
dialogArray[this.dialogNumber].Start();
}
Global Script in game_start()
exDiag.init(0, ExampleDialog);
exDiag.SetTopicEmotion(1, eNONE); // eNONE is an enumeration in another module
exDiag.SetTopicEmotion(2, eRAGE);
exDiag.SetTopicEmotion(3, eSAD);
exDiag.SetTopicEmotion(4, eHAPPY);
Room Script function calls
function hHotspot1_Talk()
{
exDiag.ToggleTopics();
dialogArray[exDiag.dialogNumber].Start(); //the offending line
}
That's all the code I've written. The offending line is that dialogArray[exDiag.dialogNumber].Start();. I'm getting a null pointer referenced (error -6).
As you can see, I've eschewed the dynamic array entirely. Before, I was able to access the array from within game_start, but for some reason, when I go to use it in a room, it crashes and burns.
Your comments are appreciated.
Quote from: Recluse on Wed 19/03/2008 19:50:03
EmotionDialogEngine.ash
Dialog* dialogArray[20];
That won't work there. Move it to the script and do an export/import.
A dynamic array of a supported type can be made global as so (using int as an example):
// script header
import int MyIntArray[];
import int MyIntArraySize;
// script body
int MyIntArray[];
int MyIntArraySize = 0; // store the size of our array since AGS doesn't yet support getting this
// initializing the array, probably in game_start
MyIntArraySize = 20; // save the new size
MyIntArray = new int[MyIntArraySize];
// accessing the array, global or room script (or any scripts after *this* script)
MyIntArray[10] = MyIntArray[5]; // get and set as normal
EDIT: D'oh! Nevermind what I wrote at first. Dialog is a built-in type and your other type is enumerated. :-[
Thanks SSH! I've gotten past that nasty bug and now I can start fixing the functionality... which apparently broke while I was working on the other part.