Pulling strings from a .dat file.

Started by KodiakBehr, Mon 17/11/2014 01:30:54

Previous topic - Next topic

KodiakBehr

This should be pretty straightforward, but I'm at a loss for what I'm doing wrong here.

I've got a large database of names, about 600+, that needs to be read from a dat file into an array.

Just to test the waters, I tried to fill the first array slot with the first name...

Code: ags

function ReadCabinetNames(){
  if (!File.Exists("CabinetNames.dat")) return;
  File *i = File.Open("CabinetNames.dat",eFileRead);
  PotentialCabinet[0].firstname = i.ReadStringBack();
  i.Close();
}


It tells me that PotentialCabinet[0] is a null pointer.  What am I doing incorrectly?

Sidenote, are there special formatting instructions for the text in the .dat that I must observe?

Thanks, in advance,

KB

Gilbert

What's the format of .dat files? Is it a well-known interchange format used by many software packages, or is it a file created by AGS?

From the manual entry of File.ReadStringBack():
QuoteYou should only use this with files which you previously wrote out with File.WriteString. Do NOT use this function with any other files, even text files.

So, if the .dat file was not written with File.WriteString() in AGS you should not use this function. Try using File.ReadRawLineBack() instead if the file assumes a plain text format.

KodiakBehr

It was a text file I converted to a .dat.  I tried ReadRawLineBack(), but got the same result.  I've also experimented with drawing from a text file, to no avail.

Let me frame this question another way...

How would you go about bulk-importing 300 strings into 300 array slots?

Gilbert

Quote from: KodiakBehr on Mon 17/11/2014 04:27:28
It was a text file I converted to a .dat.
By 'converted' did you mean really converting to other format, or just renaming the file?
If it's the latter then the .dat file is still a text file then the aforementioned function should work, but if it's the former then the resultant file is probably not a text file anymore, which as specified clearly in the manual, not handled by the function:
QuoteNOTE: this command can only read back plain text lines from text files. If you attempt to use it with binary files or files written with commands like WriteString, WriteInt, etc then the results are unpredictable.

For custom files you need to write your own file handling functions using File.ReadRawInt(), etc., unless someone already wrote a module or a plug-in to read that specific format.

If the file was already a text file before converting to .dat, why did you convert it? Just try to use the original text file instead.

monkey0506

Quote from: KodiakBehr on Mon 17/11/2014 01:30:54It tells me that PotentialCabinet[0] is a null pointer.  What am I doing incorrectly?

If it's telling you that, then it means your array entries haven't been initialized. What version of AGS are you using, and how is PotentialCabinet defined?

KodiakBehr

By converted I mean I renamed a text file, but you're right, there's no real reason for it, so I should be drawing from a text file instead.

My array entries haven't been initialized, but on game_start I'm running that function above, and it's still producing a null entry when I ask the game to display the contents of PotentialCabinet[0].  The array is correctly defined, and I've never had this trouble with importing integers from a text or dat file.

Gilbert

Can you post the part initialising those variables?

monkey0506

Quote from: KodiakBehr on Mon 17/11/2014 13:10:20My array entries haven't been initialized, but on game_start I'm running that function above, and it's still producing a null entry when I ask the game to display the contents of PotentialCabinet[0].  The array is correctly defined, and I've never had this trouble with importing integers from a text or dat file.

Perhaps I wasn't clear. What version of AGS are you using, and how is the array defined? This is important to answering your question. The way in which your array is "correctly defined" is entirely dependent on which version of AGS you are using. If you want help with this question, we need to know this information.

Monsieur OUXX

You need to answer monkey's questions, but just a hint: could it be that PotentialCabinet is defined in the header of a script, and function ReadCabinetNames is in another script? If yes, then that's a common beginners mistake:
- You must put this in the script header:
Code: ags
import CustomType PotentialCabinet; //Replace 'CustomType' with the real name 

- In the script body, you must put :
Code: ags

CustomType PotentialCabinet[10]; //Replace 10 with your own value
export PotentialCabinet;

...Otherwise the reason why the value is "null" is because you actually have two different PotentialCabinet variables when you believe you have only one.

   
 

KodiakBehr

#9
Quote from: monkey_05_06 on Mon 17/11/2014 18:37:32
Perhaps I wasn't clear. What version of AGS are you using, and how is the array defined? This is important to answering your question. The way in which your array is "correctly defined" is entirely dependent on which version of AGS you are using. If you want help with this question, we need to know this information.

Sorry, I am using AGS 3.3.0.1155 (BETA).

The array is a part of a struct titled "CabinetElect".  In a script called Variables.ash, the struct is defined as:

Code: ags

struct CabinetElect{
  String firstname;
  String lastname;
  String sex;
  String faction;  
  int loyalty;
  int picture;  
  };


The array is imported in that script with:

Code: ags

import CabinetElect PotentialCabinet[300];


The array is defined in a script called Variables.asc as:

Code: ags

CabinetElect PotentialCabinet[300];


And is later exported from the same script with:

Code: ags

export PotentialCabinet;

Crimson Wizard

#10
Quote from: KodiakBehr on Mon 17/11/2014 22:42:51
Sorry, I am using AGS 3.3.0.1155 (BETA).

I think this might be experimental version with custom resolutions, or very early 3.3.1 development version, because we used even numbers (1154, 1156) for 3.3.0 betas.
(1154 was 3.3.0 RC, so might be based on that)

KodiakBehr

Should we be migrating away from this version, then?

I note that even if I run this code:

Code: ags
  PotentialCabinet[0].firstname="x";
  ReadCabinetNames();
  Display("%s", PotentialCabinet[0].firstname);


It will spit out x as a display, which has me assuming that the problem is with this function. 

Code: ags
function ReadCabinetNames(){//Read Achievement data from file
 if (!File.Exists("CabinetNames.txt")) return;
 File *i = File.Open("CabinetNames.txt",eFileRead);
PotentialCabinet[0].firstname = i.ReadRawLineBack();
i.Close();
}


Or maybe the text file?

Snarky

In that case, are you sure File.Exists() returns true? That would be a straightforward reason why it does nothing.

Try replacing the body of the function with a simple dummy assignment to rule out the problem Monsieur OUXX suggests. If that works, you'll know for sure it's going wrong in reading from the file.

KodiakBehr

Now that's really interesting.

Code: ags

function ReadCabinetNames(){//Read Achievement data from file
 if (!File.Exists("CabinetNames.txt")) return;
 Display("Nope, the file exists.");
 //File *i = File.Open("CabinetNames.txt",eFileRead);
 //PotentialCabinet[0].firstname = i.ReadRawLineBack();
 //i.Close();
}


Blocking out the rest, it still didn't display that the file exists.  This is interesting, because a text file called CabinetNames.txt is located in the same folder as the rest of the data.  Any ideas why this could be happening?

Snarky

I don't think I've ever tried reading from an external file, so I have no idea. Could be a "basepath" issue (which folder AGS assumes as the root/default), or some problem with access permissions. I assume you're running the game through the editor to test this? Might be worth trying it standalone as well, I think the folders work a little bit differently when you do. I seem to remember running into a problem like that with plugins.

Cassiebsg

Are you sure you have your txt file on the compiled folder?
You probably do, but just making sure that the obvious reason is not the problem. ;)
There are those who believe that life here began out there...

Monsieur OUXX

The file to read is supposed to be placed in the Compiled folder.

If the file is there, then it's definitely a permissions issue. Make sure the game is in "My Documents" or the Windows 7/8 equivalent to it, not on the Desktop, not in some edgy location like C:\AGS.
 

KodiakBehr

Thank you all for your help.  The file was in the Compiled folder.  The issue doesn't appear to be with permissions.

I have no idea what is happening, or why this is proving so difficult.  I'll drop the issue and instead find a way to avoid having to address this problem.

Crimson Wizard

Sorry for dumb question, but are you sure the function is ever called? What if you put Display before checking for the file?

KodiakBehr

Yup, I proved the function was being called.  No idea what was/is happening, but I went through everything proposed above to no avail.

In the end, I decided that the dataset being imported was a little frivolous, and not worth losing any more days over when a smaller, just as viable, dataset could be hard-coded instead.

SMF spam blocked by CleanTalk