An array with multiple data types

Started by
16 comments, last by LAURENT* 9 years, 9 months ago

I don't feel like explaining.


If you don't feel like expalining your problem, why should we feel like explaining a solution? tongue.png

More seriously, it sounds like you're falling into the AB problem. You have some specific solution in mind for an unspecified problem and you're asking how to create that solution when there's likely a better, different solution to the actual problem. Tell us what you're trying to actually do.

Sean Middleditch – Game Systems Engineer – Join my team!

Advertisement

Is the array storing an int and another piece of data, or is it storing an int or another piece of data?

For the former, just use a struct:


struct dataElement {

    int intValue;
    MagicData magicDataValue;

};

If it's the latter, then as Waterlimon has said, unions are probably the better method.

But as SeanMiddleditch has said, if you don't explain carefully what you're trying to do, our advice may not be the best.

If it's the latter, then as Waterlimon has said, unions are probably the better method.


I would rather follow SiCrane's advise regarding boost::variant in this case. Depending on what kind of extra type needs to be stored a union might not even be possible (although C++11 makes unions less restrictive) but I would prefer the additional inbuilt type safety in the variant.

Here is a bit more detail on what I'm trying to do. I would post all of my code but that could be problematic. Do you see where I declared my variable at the top of this function? The variables are of int type. My main problem is trying to alter the value of the variables but the only variables that are being altered are int x & y. These variables are being used to control where I place my tiles and their respected collision detected. There are 2 for loops. 1 for the floor and the other for the wall/ elevated platforms. In the for loop for elevated platform my conditional statement do not change the values of my data types other wise my tile x & y would not be place at the coordinate 0,0.

SDL_Rect has 4 values. x, y, h, w. It would be declared like this

SDL_Rect Wall;

Wall.x = ?;

Wall.y = ?;

Wall.h = ?;

Wall.w = ?;

I need to learn why the variables are not being edited. If I can figure this out I will gain full control over where I place my tiles.


bool set_tiles( Tile *tiles[], Tile *walls[] )
{
    //The tile offsets
    int x = 0, y = 0;
	int a = 0, b = 0;
	int p = 0, q = 0;
	

    //Open the map
    std::ifstream map( "lazy.map" );
	std::ifstream map2( "Zrender.map" );
	std::ifstream coords ("Coords.map");
    //If the map couldn't be loaded
    if( map == NULL )
    {
        return false;
    }
	if( map2 == NULL )
    {
        return false;
    }
    //Initialize the tiles
    for( int t = 0; t < TOTAL_TILES; t++ )
    {
		//Determines what kind of tile will be made
        int tileType = -1;

        //Read tile from map file
        map >> tileType;

        //If the was a problem in reading the map
        if( map.fail() == true )
        {
            //Stop loading map
            map.close();
            return false;
        }

			//If the number is a valid tile number
			if( ( tileType >= 0 ) && ( tileType <  TILE_SPRITES ) )
			{
				tiles[ t ] = new Tile(NULL, NULL, NULL , NULL, x, y, tileType );
				//Move to next tile spot
				x += TILE_WIDTH;

			}
        
			//If we don't recognize the tile type
			else
			{
				//Stop loading map
				map.close();
				return false;
			}

			

			//If we've gone too far
			if( x >= LEVEL_WIDTH )
			{
				//Move back
				x = 0;

				//Move to the next row
				y += TILE_HEIGHT;
			}
		}
//Ending of for loop.

	 for( int tz = 0; tz < TOTAL_TILES_WALL; tz++ )
	 {
		
		//Determines what kind of tile will be made
        int tileTypeWall = -1;
		int Position = -1;

        //Read tile from map file
        map2 >> tileTypeWall;
		coords >> Position;
        //If the was a problem in reading the map
        if( map2.fail() == true )
        {
            //Stop loading map
            map2.close();
            return false;
        }

			if (Position == 0) 
			{
				p += TILE_WIDTH;
			}
			//If the number is a valid tile number
			if ( tileTypeWall == TILE_RED_WALLf  )
			{
				walls[ tz ] = new Tile( NULL, NULL, p , q, NULL, NULL, tileTypeWall );
			}
        
		

		
		}

    //Close the file
    map.close();

    //If the map was loaded fine
    return true;
}

bool touches_wall( SDL_Rect box, Tile *tiles[] )
{
    //Go through the tiles
    for( int t = 0; t < TOTAL_TILES; t++ )
    {
        //If the tile is a wall type tile
        if( ( tiles[ t ]->get_type() >= TILE_CENTER ) && ( tiles[ t ]->get_type() <= TILE_TOPLEFT ) )
        {
            //If the collision box touches the wall tile
            if( check_collision( box, tiles[ t ]->get_box() ) == true )
            {
                return true;
            }
        }
    }

    //If no wall tiles were touched
    return false;
}
I'm not following the problem. We don't need all your code, just the small bit of it with the issue, and a description of what you're trying to do and what isn't working. By second for-loop do you mean this one?

	 for( int tz = 0; tz < TOTAL_TILES_WALL; tz++ )
	 {
		
		//Determines what kind of tile will be made
        int tileTypeWall = -1;
		int Position = -1;
I don't see where arrays come into this; your only arrays are all storing Tile*. The integer variables you mention have nothing to do with arrays, and I don't see why multiple types come into this.

This is a problem for a debugger. Step through the code and see what's going wrap. Your map parsing/loading code probably just has a simple bug hiding in it somewhere. Your use of the Position variable is very confusing; I don't know what it's supposed to represent or what data it's supposed to have but it feels wrong. You only increment p if Position is zero (is that correct?) and you never increment q anywhere that I can see, so of course it's always going to be zero.

Sean Middleditch – Game Systems Engineer – Join my team!

Tile is the constructor of the Tile class and inside it's parameter it holds int values. Check this out for a more clear view how I'm using this. I tried a new method today of reading from a map file and altering my value base on what values int position reads. It did not work unfortunately.


class Tile
{
    private:
    //The attributes of the tile
    SDL_Rect floorpad;
	SDL_Rect wallside;
	SDL_Rect wallfront;

    //The tile type
    int type;

    public:
    //Initializes the variables
    Tile(int a, int b, int p, int q, int x, int y, int tileType );

    //Shows the tile
    void show();
	

    //Get the tile type
    int get_type();

    //Get the collision box
    SDL_Rect get_box();
};

Tile::Tile( int a, int b, int p, int q, int x, int y, int tileType )
{
    //Get the offsets
    floorpad.x = x;
    floorpad.y = y;

    //Set the collision box
    floorpad.w = TILE_WIDTH;
    floorpad.h = TILE_HEIGHT;
	
	wallside.x = a;
	wallside.y = b;
	wallside.w = TILE_WALLs_WIDTH;
	wallside.h = TILE_WALLs_HEIGHT;
	
  

	wallfront.x = p;
	wallfront.y = q;
	wallfront.w = TILE_WALLf_WIDTH;
	wallfront.h = TILE_WALLf_HEIGHT;


    //Get the tile type
    type = tileType;
}

Polymorphism is probably what you want. For example, with Polymorphism you can have a base class Tile and then have a bunch of child classes which inherit from the Tile class. You could then have an array of pointers to instances of the Tile class. In these pointers you can store instances of subclasses of the Tile class.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

I'll try my best to implement a polymorphism test tomorrow. Hopefully this solves the problem.

This topic is closed to new replies.

Advertisement