Page 1 of 1

How to check if a tile already exists in that spot?

PostPosted: Tue Dec 25, 2012 4:55 am
by Hblade
I'm making a tile editor, so far so good but I wanted to make a drawing function and I want to be able to check and see if a tile already exists or not in a certain location. How would I be able to do this?

I get a READ error?
Code: Select all
 if(tX[x]==0) {
        CreateActor("clone", "TILES", "(none)", "(none)", x, y, true);
        tX[x]=1;
                 }

Re: How to check if a tile already exists in that spot?

PostPosted: Tue Dec 25, 2012 6:14 am
by skydereign
You are using a one dimensional array? It seems you are using some form of method to record which tiles are where, and in that case you are in the right direction. But if you are getting a read error, that means [x] is out of bounds of your tX array. You should however probably be using a two dimensional array for this.

Re: How to check if a tile already exists in that spot?

PostPosted: Tue Dec 25, 2012 7:09 am
by Hblade
How would I save and load a 2 dimensional array? Currently I'm using the save variables method to save the map information. So far I can save and load created maps, and it's not required that I have a draw method but it would be really nice.

Re: How to check if a tile already exists in that spot?

PostPosted: Tue Dec 25, 2012 9:29 am
by skydereign
The simplest way is to use FILE* and fprintf. There are many examples of this on the forums, but here is a simple map save function.
Code: Select all
int map[10][10];

void
map_save (char* filename)
{
    FILE* file = fopen(filename, "w");
    int i, j;

    for(i=0;i<10;i++)
    {
        for(j=0;j<10;j++)
        {
            fprintf(file, "%d, ", map[j][i]);
        }
        fprintf(file, "\n");
    }
    fclose(file);
}

The read is the same, but using the reverse operations (fscanf instead of fprintf).

Re: How to check if a tile already exists in that spot?

PostPosted: Tue Dec 25, 2012 11:50 am
by Hblade
Thanks sky I'll read this tomorrow. I really appreciate it, this will help with the tile editor a ton.

Re: How to check if a tile already exists in that spot?

PostPosted: Tue Jan 01, 2013 1:23 am
by Hblade
Alright so I get how to write some text, but how would I actually use this load into a variable? Like, how would the tile know where to go?
Code: Select all

void
save_txt (char* filename)
{
    FILE* file = fopen(filename, "w");
    int i, j;

    for(i=0;i<10;i++)
    {
        for(j=0;j<10;j++)
        {
            fprintf(file, "%d\n", j);
        }
        fprintf(file, "\n");
    }
    fclose(file);
}


This was used to debug, the text file output:
Code: Select all
0
1
2
3
4
5
6
7
8
9

0
1
2
3
4
5
6
7
8
9

0
1
2
3
4
5
6
7
8
9

0
1
2
3
4
5
6
7
8
9

0
1
2
3
4
5
6
7
8
9

0
1
2
3
4
5
6
7
8
9

0
1
2
3
4
5
6
7
8
9

0
1
2
3
4
5
6
7
8
9

0
1
2
3
4
5
6
7
8
9

0
1
2
3
4
5
6
7
8
9



Now how would this code ever get used? Like how would this text file be used to change variables?

Re: How to check if a tile already exists in that spot?

PostPosted: Tue Jan 01, 2013 1:49 am
by skydereign
As I mentioned in our pms, you just need to use the opposite operations. Of course, normally when you save information, the information is important (and stored in some variable). That way you can load the information back into the same variable. In your example, you are just printing out j...
Code: Select all
void
load_txt (char* filename)
{
    FILE* file = fopen(filename, "r"); // notice the format is switched to "r"
    int i, j;

    for(i=0;i<10;i++)
    {
        for(j=0;j<10;j++)
        {
            int temp = 0;
            fscanff(file, "%d\n", &temp); // temp now holds the value that should be read in
            // so you would want to do something with it... but didn't actually save anything important
        }
        fscanf(file, "\n");
    }
    fclose(file);
}

So, when you save, you save variables. That way you can load the values back into the variable.

Re: How to check if a tile already exists in that spot?

PostPosted: Tue Jan 01, 2013 3:50 am
by Hblade
skydereign wrote:As I mentioned in our pms, you just need to use the opposite operations. Of course, normally when you save information, the information is important (and stored in some variable). That way you can load the information back into the same variable. In your example, you are just printing out j...
Code: Select all
void
load_txt (char* filename)
{
    FILE* file = fopen(filename, "r"); // notice the format is switched to "r"
    int i, j;

    for(i=0;i<10;i++)
    {
        for(j=0;j<10;j++)
        {
            int temp = 0;
            fscanff(file, "%d\n", &temp); // temp now holds the value that should be read in
            // so you would want to do something with it... but didn't actually save anything important
        }
        fscanf(file, "\n");
    }
    fclose(file);
}

So, when you save, you save variables. That way you can load the values back into the variable.


Alright Thanks. Its still confusing to me for some reason. I've loaded the text file, but have no idea how to output it to a text actor.

It's obvious that I'm missing something insanely simple, something thats just not going through my head for some reason

EDIT:
I feel like i'm right on top of the answer and it's just about to click but o3o

Re: How to check if a tile already exists in that spot?

PostPosted: Tue Jan 01, 2013 4:56 am
by skydereign
As I mentioned, you need to store it in a variable. Your code doesn't use a variable, and therefore makes it harder to display. For a tile editor, you usually have a 2d array, which you load back into the map array. From there you display it as you would.

Re: How to check if a tile already exists in that spot?

PostPosted: Tue Jan 01, 2013 6:07 am
by Hblade
Could you make an example gef of saving and loading this way?

Oh and happy new years :D!

Re: How to check if a tile already exists in that spot?

PostPosted: Tue Jan 01, 2013 6:26 am
by skydereign
You should be able to do this yourself. Here are the save/load functions, I'll leave the 2d array editing to you.
Code: Select all
#define WIDTH 5
#define HEIGHT 5

int map[HEIGHT][WIDTH] = {{1, 1, 1, 1, 1},
                          {0, 0, 0, 0, 1}};

void
map_save (char* filename)
{
    FILE* file = fopen(filename, "w");
 
    if(file != NULL)
    {
        int i, j;
 
        for(i=0;i<HEIGHT;i++)
        {
            for(j=0;j<WIDTH;j++)
            {
                fprintf(file, "%d, ", map[i][j]);
            }
            fprintf(file, "\n");
        }
        fclose(file);
    }
}

void
map_load (char* filename)
{
    FILE* file = fopen(filename, "r");
 
    if(file != NULL)
    {
        int i, j;
 
        for(i=0;i<HEIGHT;i++)
        {
            for(j=0;j<WIDTH;j++)
            {
                fscanf(file, "%d, ", &(map[i][j]));
            }
            fscanf(file, "\n");
        }
        fclose(file);
    }
}

Re: How to check if a tile already exists in that spot?

PostPosted: Tue Jan 01, 2013 6:51 am
by Hblade
Thanks sky, lol. The part that was confusing me was, I never seen a "variable=this" so like I was confused as to how the 2D array gets actually loaded xD

Now it all makes sense :P