• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
LAURENT*

An array with multiple data types

17 posts in this topic

My program was running smoothly until I needed my array to store into data types. After I ran the code the screen went blink and I could run my program again until I remove the into data type. Can sia force my array to store different data types? I really really need it to do this for my level renderer to be perfect.

Edited by LAURENT*
-2

Share this post


Link to post
Share on other sites

Okay, you might actually need to tell us which language you are using here wink.png (and what is an "into data type"? typo?)

0

Share this post


Link to post
Share on other sites

You can do different data type arrays in Python, Ruby, PHP and JavaScript

 

 Are you looking for something like a HashMap or Dictionary ?

Edited by Shippou
2

Share this post


Link to post
Share on other sites

If it's for something low level like a renderer, you might want to rethink some things for performance sake, Having multiple datatypes in a single array will require a lot of checking which is probably something you do not want.

 

We're missing a lot of information on what you actually want to achieve, but considering you are talking about a renderer, I assume you want to render different level objects? Perhaps consider having a renderable (or anything) base class that has a render function, but more info would be nice! :)

 

Bacterius already mentioned that knowing what the language you're working with is something nice to know, also what kind of data types you are referring to and what they should be doing to have your level renderer work.

0

Share this post


Link to post
Share on other sites

I code in c++ and I really meant int data type but I got auto corrected.

 You can cheat and use an array of pointers - however this can get very tricky to maintain and not recommended .

Edited by Shippou
0

Share this post


Link to post
Share on other sites

If it's for something low level like a renderer, you might want to rethink some things for performance sake, Having multiple datatypes in a single array will require a lot of checking which is probably something you do not want.

 

We're missing a lot of information on what you actually want to achieve, but considering you are talking about a renderer, I assume you want to render different level objects? Perhaps consider having a renderable (or anything) base class that has a render function, but more info would be nice! smile.png

 

Bacterius already mentioned that knowing what the language you're working with is something nice to know, also what kind of data types you are referring to and what they should be doing to have your level renderer work.

 

I code in c++ programming language. I need my array to hold 2 data types. int data type and a special one I don't feel like explaining. I need the int data type to be stored so my renderer can place my object correctly base on reading from numbers from a special file. Overall I just really need to store different data types in a array. This will be the only array to do this in my program.

-2

Share this post


Link to post
Share on other sites

If you mean each array element can either be an int or your other type, you can check out unions.

Basically youd have something like:

struct ArrayElement {

bool isThisAnObject; //use this to tell your code whether the type is MyObject or int so it can read/modify the correct one (assuming you cant deduce this some other way)

union { //object and integer overlap in memory/share some of the bytes, so you an only use one at a time or else face corruption of data

MyObject object;

int integer;

} data;

}

as the data type of the array. Note that all the elements of the array will be of size max(sizeof(MyObject),sizeof(int)) so you wont be saving any RAM by having some of the elements be int, AND you probably need to pay the extra byte to tell your code the actual type of each array element.

 

But it would be better if you give more information about the actual problem to see if theres some other more clean solution.

0

Share this post


Link to post
Share on other sites

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.
2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites

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;
}
Edited by LAURENT*
0

Share this post


Link to post
Share on other sites
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.
2

Share this post


Link to post
Share on other sites

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;
}

Edited by LAURENT*
0

Share this post


Link to post
Share on other sites

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.

Edited by superman3275
0

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  
Followers 0