Sign in to follow this  
Aglet

A matrix of numbers

Recommended Posts

Aglet    122
Hi fellas. I'm trying to build me one of those matrices with integers in it, and then do some simple collision testing. I've had the program up and running but then I decided that I could do with some more cases than just one. Thing is, if I do a switch-case with the tilesystem (as seen below) it doesn't work. It ignores my new settings (i.e. keeping all the numbers at 0 when they really should be a mixture of ones and zeros). Would appreciate any help I can get (as usual). Thanks!
levelCollissionX = levelCollissionY = 0;
        short int tilesystem[15][20] = {
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        };
    switch(levelNumber)
    {
        case 0:
         levelCollissionX = levelCollissionY = 0;
        short int tilesystem = {
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
        {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},
        {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},
        {1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        };
        
        break;
    } 

Share this post


Link to post
Share on other sites
DivineGod    164
You are redeclaring tilesystem in your switch scope, therefore it will only exist with a mix of 0's and 1's within the switch.

should be like this:


short int tilesystem = ...;
switch (...)
{
case 0: ...
tilesystem = ....;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
check out the scope, you are basically doing a scoped redeclaration.

Share this post


Link to post
Share on other sites
Aglet    122
Nope doesn't seem to work. I've tried declaring it in a header file as well. But it doesn't seem to like being declared somewhere else. I get an error saying I miss a '{' before the matrix or something...
Any ideas?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Aglet
Nope doesn't seem to work. I've tried declaring it in a header file as well. But it doesn't seem to like being declared somewhere else. I get an error saying I miss a '{' before the matrix or something...
Any ideas?


provide the full source code, showing the problem you are experiencing, preferably downstripped to a short file that will compile as a single unit.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Aglet's co-worker here. Well actually, posting the whole sourcecode downstripped would be .. a lot of megs. This is the only thing that is bugging us, as we can't seem to declare the 2D Matris and then initialising it to something.
This code down here gives an error. I just wonder how it should be done (lets just forget about the switch-case for now)

short int tilesystem[15][20];

tilesystem = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};

ERROR

short int tilesystem[15][20];

tilesystem[15][20] = {

doesn't work

short int tilesystem[][];

tilesystem = {

doesn't work

Share this post


Link to post
Share on other sites
moeron    326
You can't separate the declaration and initialization like that. If you want to initialize it with all those values you have to do it right as you declare it.

For example

// this works because I declare and initialize at the same time
short correct_tile[2][2] ={
{0,1},
{0,5},
};

//But trying something like this is not correct
short invalid_tile[2][2];
invalid_tile={
{0,1},
{0,5},
};

Share this post


Link to post
Share on other sites
Enigma    1410
Easiest solution:
levelCollissionX = levelCollissionY = 0;
short int tilesystem[15][20] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
switch(levelNumber)
{
case 0:
{
levelCollissionX = levelCollissionY = 0;
short int newtilesystem[15][20] = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},
{1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},
{1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
// or memcpy if using C not C++
std::copy(newtilesystem, newtilesytem + (sizeof(newtilesystem) / sizeof(newtilesystem[0])), tilesystem);

break;
}
}


I.e. just declare a new array using initialization syntax and copy it over.

Σnigma

Share this post


Link to post
Share on other sites
Zahlman    1682
As indicated, the original problem is one of scope. The given syntax is only valid for *initialization*, not *assignment*; these are different things in C++. The switch statement introduces a new scope, so if you say "short int tilesystem" etc. in there, that declares a different variable from the outside one, which is then initialized to the other values, and then it falls out of scope at the end of the switch statement. (Note that individual *cases* do *not* introduce new scopes - they are case *labels* after all, not blocks.) And if you try to assign with the same syntax to the existing variable, it doesn't compile, because assignment is not initialization.

Enigma's solution is probably the simplest way of just making it work. You could also just assign 1's to individual array slots in the usual way: "tilesystem[x][y] = 1;" a whole bunch of times. As you can imagine, that's ridiculously tedious and error-prone. There is also the option of using Boost::Assign to make it a little easier and friendlier to write this kind of array re-assignment.

However, you *should not do things this way*. An experienced developer would never do this except as a quick hack for testing. What you want to do is put the data into a file, and read an appropriate file according to the levelNumber setting. This has numerous benefits:

- You don't need to recompile when you change something.
- You can add a tiny bit more information to the file, plus a little extra support in the code, and suddenly not be limited to a particular level "size" (each level could be different, even).
- You don't need to add the extra braces and so on to your data file. (The simplest solution, in C++, would probably use spaces instead of commas.)

If you don't know how to do this, feel free to post again in For Beginners; I'll be much nicer then ;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this