Jump to content
  • Advertisement
Sign in to follow this  
Temrek

Array loses all data?

This topic is 4616 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I am writing a function to read a specific file structure but for some reason an array I want to save the data in always returns a 0 no matter what it is set to do. And yes I know that some things I use for this is ancient, thats becouse a friend of mine is very good at the older libraries. It is a function to read a map file for a rather simple game, all data I save is saved properly EXCEPT the data I save in the LowLayerTiles 2 dimensional integer array.
#include <stdio.h>
#include <string.h>
#include "ReadFiles.h"


//Public functions
ReadFiles::ReadFiles()
{

}

ReadFiles::~ReadFiles()
{
  

}
void ReadFiles::LoadMap(char* filename)
{
FILE* mapfile;
char line[255];
mapfile = fopen(filename, "rt");
ReadLine(mapfile, line);
sscanf(line, "Area Music: %i\n", &AreaMusic);
ReadLine(mapfile, line);
sscanf(line, "Battle Music: %i\n", &BattleMusic);
ReadLine(mapfile, line);
sscanf(line, "Entry Script: %i\n", &ScriptOnEntry);
ReadLine(mapfile, line);
sscanf(line, "Width: %i\n", &AreaWidth);
ReadLine(mapfile, line);
sscanf(line, "Height: %i\n", &AreaHeight);
ReadLine(mapfile, line);
sscanf(line, "Mana Capacity: %i\n", &ManaCapacity);
ReadLine(mapfile, line);
sscanf(line, "Mana Recovery: %i\n", &ManaRecovery);
//Define arrays for map reading
LowLayerTiles = new int [AreaHeight,AreaWidth];
MidLayerTiles = new int [AreaHeight,AreaWidth];
HighLayerTiles = new int [AreaHeight,AreaWidth];
Interactibles = new int [AreaWidth, AreaHeight];
NPCs = new int [AreaWidth, AreaHeight];
Triggers = new int [AreaWidth, AreaHeight];
//Done with the arrays
char CrntPlace[2048];
for (int i = 0; i<AreaHeight; i++)
    {
    int Coordnate;
    ReadLine(mapfile, line);
    char * tileid;
    sscanf(line, "%s\n", CrntPlace);
    //printf ("%s\n",CrntPlace);
    tileid = strtok(line," ");
           for (int j = 0; j<AreaWidth; j++)
           {

           Coordnate = int(tileid[0]) - 48;
           Coordnate = Coordnate * 10;
           Coordnate = Coordnate + int(tileid[1]) - 48;
     
           
           tileid = strtok (NULL, " ,.");

      LowLayerTiles[i,j] = Coordnate;
          printf ("%i\n",LowLayerTiles[i,j]);
           }
    }
fclose(mapfile);
}
void ReadFiles::ReadLine(FILE *f,char *string)
{
	do
	{
		fgets(string, 255, f);
	} while ((string[0] == '/') || (string[0] == '\n'));
}

Here is the header file.
class ReadFiles
{
  public:
   //int temp;
     
    //Map Variables
    //Music variables
    int AreaMusic;
    int BattleMusic;
    //Scripts to run
    int ScriptOnEntry;
    //Area Size
    int AreaWidth;
    int AreaHeight;
    //Mana
    int ManaCapacity;
    int ManaRecovery; //Mana recovered per second
    //LayerVariables
    int *LowLayerTiles;
    int *MidLayerTiles;
    int *HighLayerTiles;
    int *Interactibles;
    int *NPCs;
    int *Triggers;
    
    
    //Public functions
    ReadFiles(); //Constructor
    ~ReadFiles();  //Destructor
    
    void LoadMap(char* filename);
    
  private:
    void ReadLine(FILE* f,char* string);    
};


Share this post


Link to post
Share on other sites
Advertisement
Edit nr 3:
Ok, seems I didn't post nonsense afterall. I was taken by surprise that new int[i,j] actually compiles. Each dimension of an array in C/C++ (I'm assuming that you're not using C#, which I know nothing about but does seem to accept [,]) is denoted by a separate pair of: []. So accessing element (i,j) of a two dimensional array is as follows:

element = LowLayerTiles[j];

Unfortunately, initializing a 2-dimensional array with two non-constant variables such as your AreaHeight and AreaWidth is not possible with new. Therefore creating a two dimensional array requires either writing your own array class that maps a 2D array to a 1D array, or using existing solutions such as a std::vector of std::vectors. Solutions are described in this thread or by MaulingMonkey in this thread.

Tom

[Edited by - dimebolt on October 5, 2005 8:58:46 AM]

Share this post


Link to post
Share on other sites
arrayname[i, j] is equivalent to: "evaluate i, throw away the results (there may have been side effects that will persist though), then tell me what arrayname[j] is".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!