Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
2 registered members (TipmyPip, 1 invisible), 18,789 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Reading from file, #202894
04/17/08 22:14
04/17/08 22:14
Joined: Mar 2008
Posts: 67
crumply Offline OP
Junior Member
crumply  Offline 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:
 Code:
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
 Code:
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
 Code:
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 Offline
Expert
Uhrwerk  Offline
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.
 Code:
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: Uhrwerk] #202915
04/18/08 00:03
04/18/08 00:03
Joined: Mar 2008
Posts: 67
crumply Offline OP
Junior Member
crumply  Offline OP
Junior Member

Joined: Mar 2008
Posts: 67
No but it makes better sense than what I was doing.

Still get error in function loadtexts.

I don't know what to do. I could use an separate array for each but that seems crude.

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 Offline
Expert
Uhrwerk  Offline
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 Offline OP
Junior Member
crumply  Offline 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 Offline
Expert
Uhrwerk  Offline
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 Offline OP
Junior Member
crumply  Offline OP
Junior Member

Joined: Mar 2008
Posts: 67
The Struct:
 Code:
typedef struct { 
  int* opcode; 
  int* actor; 
  char* textstr[128];
} DIALOGUE;  // defines a struct type named "SPOT"


Using the Struct:
 Code:
var filehandle;

DIALOGUE* speechbank[10000];


The function:
 Code:
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:
 Code:
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 Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
What about this version:
 Code:
#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...

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1