Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


An array with multiple data types


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
17 replies to this topic

#1 LAURENT*   Members   -  Reputation: 281

Like
-2Likes
Like

Posted 27 July 2014 - 12:55 PM

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*, 27 July 2014 - 12:57 PM.


Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 10220

Like
0Likes
Like

Posted 27 July 2014 - 01:42 PM

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


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 Code Fox   Members   -  Reputation: 2158

Like
2Likes
Like

Posted 27 July 2014 - 01:53 PM

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, 27 July 2014 - 01:57 PM.


The Internet is so big, so powerful and pointless

that for some people it is a complete substitute for life .

~ Andrew Brown


#4 LAURENT*   Members   -  Reputation: 281

Like
0Likes
Like

Posted 27 July 2014 - 01:54 PM

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



#5 Rld_   Members   -  Reputation: 1607

Like
0Likes
Like

Posted 27 July 2014 - 01:55 PM

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.



#6 Code Fox   Members   -  Reputation: 2158

Like
0Likes
Like

Posted 27 July 2014 - 02:04 PM

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, 27 July 2014 - 03:12 PM.


The Internet is so big, so powerful and pointless

that for some people it is a complete substitute for life .

~ Andrew Brown


#7 LAURENT*   Members   -  Reputation: 281

Like
-2Likes
Like

Posted 27 July 2014 - 02:40 PM

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.



#8 cube2222   Members   -  Reputation: 203

Like
0Likes
Like

Posted 27 July 2014 - 03:22 PM

I think you could use an array containing two arrays each holding oen type.



#9 Waterlimon   Crossbones+   -  Reputation: 2925

Like
0Likes
Like

Posted 27 July 2014 - 03:53 PM

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.


o3o


#10 SiCrane   Moderators   -  Reputation: 9963

Like
3Likes
Like

Posted 27 July 2014 - 04:05 PM

You might want to try boost::variant.



#11 SeanMiddleditch   Crossbones+   -  Reputation: 9850

Like
2Likes
Like

Posted 28 July 2014 - 12:13 AM

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.

#12 LennyLen   GDNet+   -  Reputation: 4378

Like
0Likes
Like

Posted 28 July 2014 - 12:24 AM

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.



#13 BitMaster   Crossbones+   -  Reputation: 5075

Like
0Likes
Like

Posted 28 July 2014 - 12:34 AM

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.

#14 LAURENT*   Members   -  Reputation: 281

Like
0Likes
Like

Posted 29 July 2014 - 11:32 AM

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*, 29 July 2014 - 11:37 AM.


#15 SeanMiddleditch   Crossbones+   -  Reputation: 9850

Like
2Likes
Like

Posted 29 July 2014 - 12:11 PM

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.

#16 LAURENT*   Members   -  Reputation: 281

Like
0Likes
Like

Posted 29 July 2014 - 12:33 PM

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*, 29 July 2014 - 12:36 PM.


#17 superman3275   Crossbones+   -  Reputation: 2061

Like
0Likes
Like

Posted 29 July 2014 - 01:21 PM

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, 29 July 2014 - 01:24 PM.

I'm a game programmer and computer science ninja ph34r.png!

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 smile.png!


#18 LAURENT*   Members   -  Reputation: 281

Like
0Likes
Like

Posted 29 July 2014 - 08:57 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS