1 registered members (TipmyPip),
18,449
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Reading from file,
#202894
04/17/08 22:14
04/17/08 22:14
|
Joined: Mar 2008
Posts: 67
crumply
OP
Junior Member
|
OP
Junior Member
Joined: Mar 2008
Posts: 67
|
I am making a dialogue system for my game and I am trying to load my text from a file. The file contains the number of rows opcode(number) actor(number) text(characters) So I made a struct: typedef struct {
int opcode;
int actor;
char textstr[100];
} DIALOGUE; // defines a struct type named "SPOT"
and then in my script, a bunch of them DIALOGUE* speechbank[10000]; and then wrote a function to populate them from the file. Except I get an invalid pointer error! Here's my function function loadtexts()
{
var i = 0; //Temp loop var
var maxentries = 0; //This is the number of dialogue entries to be read.
filehandle = file_open_read("%APPDIR%//dialogue.txt"); // opens the file dialogue to read
statustext = "File opened";
wait (100);
maxentries = file_var_read(filehandle); //how many entries are there in this file?
//Time for a loop
while (i < maxentries)
{
speechbank[i].opcode = file_var_read(filehandle);
speechbank[i].actor = file_var_read(filehandle);
file_str_read(filehandle,speechbank[i].textstr);
i++;
}
file_close(filehandle); // closes the file
} Any help demisifying this one would be great.
|
|
|
Re: Reading from file,
[Re: crumply]
#202913
04/17/08 23:51
04/17/08 23:51
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
I guess you have not allocated the memory for the structs you want to store in the array. while (i < maxentries)
{
// First allocate the memory needed.
speechbank[i] = (DIALOGUE*)malloc(sizeof(DIALOGUE);
speechbank[i].textstr = str_create("#128");
// Then you can read the data and store it in the array.
speechbank[i].opcode = file_var_read(filehandle);
speechbank[i].actor = file_var_read(filehandle);
file_str_read(filehandle,speechbank[i].textstr);
i++;
} Does that solve your problem?
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Reading from file,
[Re: crumply]
#202916
04/18/08 00:06
04/18/08 00:06
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
So what does the error message exactly say? Can you pin down the error message to a line, that causes the crash using printf?
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Reading from file,
[Re: Uhrwerk]
#202917
04/18/08 00:13
04/18/08 00:13
|
Joined: Mar 2008
Posts: 67
crumply
OP
Junior Member
|
OP
Junior Member
Joined: Mar 2008
Posts: 67
|
it's when it reaches file_str_read(filehandle,speechbank[i].textstr); but ONLY the 2nd time around the loop :S
EDIT: I changed while (i < maxentries) to while (i < maxentries)-1 when that happened. When I changed it back it did it the first time around the loop!
Last edited by crumply; 04/18/08 00:16.
|
|
|
Re: Reading from file,
[Re: crumply]
#202918
04/18/08 00:19
04/18/08 00:19
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
Now I am confused. Can you please post the code you have in loadtexts again?
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Reading from file,
[Re: Uhrwerk]
#202920
04/18/08 00:23
04/18/08 00:23
|
Joined: Mar 2008
Posts: 67
crumply
OP
Junior Member
|
OP
Junior Member
Joined: Mar 2008
Posts: 67
|
The Struct: typedef struct {
int* opcode;
int* actor;
char* textstr[128];
} DIALOGUE; // defines a struct type named "SPOT"
Using the Struct: var filehandle;
DIALOGUE* speechbank[10000]; The function: function loadtexts()
{
var i = 0; //Temp loop var
var maxentries = 0; //This is the number of dialogue entries to be read.
filehandle = file_open_read("dialogue.txt"); // opens the file dialogue to read
wait (100);
maxentries = file_var_read(filehandle); //how many entries are there in this file?
// //Time for a loop
while (i < maxentries)
{
// First allocate the memory needed.
speechbank[i] = (DIALOGUE*)malloc(sizeof(DIALOGUE));
printf("Ok so far");
speechbank[i].textstr = str_create("#128");
// Then you can read the data and store it in the array.
printf("Ok so far");
speechbank[i].opcode = file_var_read(filehandle);
printf("Ok so far");
speechbank[i].actor = file_var_read(filehandle);
printf("Ok so far");
file_str_read(filehandle,speechbank[i].textstr);
printf("Ok so far kk");
i++;
}
file_close(filehandle); // closes the file
return(1);
} The data: 3
1 0 This is a test
1 1 This is the second text
0 1 This is the third with close opcode Also, if you have IRC or anything similar I would like to speak to you 
Last edited by crumply; 04/18/08 00:24.
|
|
|
Re: Reading from file,
[Re: crumply]
#202924
04/18/08 00:55
04/18/08 00:55
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
What about this version: #include <acknex.h>
typedef struct {
int opcode;
int actor;
STRING* textstr;
} DIALOGUE; // defines a struct type named "SPOT"
var filehandle;
DIALOGUE* speechbank[10000];
function loadtexts()
{
var i = 0; //Temp loop var
var maxentries = 0; //This is the number of dialogue entries to be read.
filehandle = file_open_game("dialogue.txt"); // opens the file dialogue to read
wait (100);
maxentries = file_var_read(filehandle); //how many entries are there in this file?
// //Time for a loop
while (i < maxentries)
{
// First allocate the memory needed.
speechbank[i] = (DIALOGUE*)malloc(sizeof(DIALOGUE));
speechbank[i].textstr = str_create("#128");
// Then you can read the data and store it in the array.
speechbank[i].opcode = file_var_read(filehandle);
speechbank[i].actor = file_var_read(filehandle);
file_str_read(filehandle,speechbank[i].textstr);
i++;
}
file_close(filehandle); // closes the file
return(1);
}
void main()
{
loadtexts();
}
Always learn from history, to be sure you make the same mistakes again...
|
|
|
|