Thread: Another question :O need ideas and confirmation

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    Another question :O need ideas and confirmation

    I know I'm asking loads of questions, but when I get frusterated I turn to you peeps

    Anyways, I'm trying to make a tile based game, and now I have this struct

    Code:
    struct tileSlot{
    	object obj;
    	float x, y;
    
    };
    And an array
    Code:
    tileSlot slots[31][23];
    And then I set a fixed position to all the 'slots', using this code

    Code:
    void tileSlotPos(){
    	float width = 0;
    	//width loop
    	for(int w = 0; w >= 31; ++w){
    		float height = 0;
    		//height loop
    		for(int h = 0; h >= 23; ++h){
    			slots[w][h].x = width;
    			slots[w][h].y = height;
    
    			height += 32;
    		}
    		width += 32;
    	}
    
    }
    and then I create the platforms using this code.

    Code:
    void createPlat(int slotW, int slotH){
    	platform plat;
    	slots[slotW][slotH].obj = plat;
    	plat.x = slots[slotW][slotH].x;
    	plat.y = slots[slotW][slotH].y;
    
    	plats.push_back(plat);
    
    }
    Then later when I call, for testing.

    Code:
    tileSlotPos();
    
    createPlat(20, 20);
    the platform ends up in the top left corner, meaning x= 0 and y = 0, or slot[0][0]
    Do you peeps have any idea why it does this? And do you have any idea for a different approach to doing this?
    Thanks in advance!
    Currently research OpenGL

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, it looks like your for loop will not run at all, since w >= 31 is false as the first iteration begins.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Your tile contains an object instance. You create a platform instance on the stack, assign it to that object instance, and then modify the platform instance. You expect the object instance to have the values you gave the platform instance. This is wrong for four reasons:

    1) The assignment copies the values. There remains no connection between the object instance in the tile instance and the platform instance on the stack after the copying is done.
    2) The assignment slices. Basically, only the object subpart of the platform instance is copied, everything else is ignored. This is not good at all.
    3) If the assignment actually copied everything, you'd be overwriting memory, because a platform instance is larger than an object instance. And you still wouldn't retain the values that you assigned to the platform instance after you copied from it.
    4) If the assignment somehow made the object instance reference the platform instance (like this code would do in Java), you'd have a dangling reference, because the platform instance is destroyed when createPlat returns.

    The correct solution depends on the circumstances, but probably consists of making the object instance in tile a smart pointer to object, and allocating a platform instance on the heap in createPlat (with new), making the smart pointer take control of the instance.

    On a side note, is it really so much work to write createPlatform instead of createPlat?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    well, platform is a class inherited from object
    Code:
    class platform : public object
    Is it still wrong?
    And, laserlight, thanks ^^ I hate such errors xP
    EDIT: it works now
    Currently research OpenGL

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I expected it to be inheriting (not inherited, that means something else), but it's still wrong. You say it works, I say it contains a bug that will bite you very seriously indeed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    why o.o?

    And I already ended with a bug, the bug is when I set the height of the slot array, to 23, like slots[0][23], the platform jumps up to, slots[1][0], now why does it do that? Is it that bug you talked about, or something completely different?
    Currently research OpenGL

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Look up the term "slicing" as it pertains to C++ programming.

    As for the other, there is no slots[0][23]. The highest index of the second dimension is 22. 23 is an out of bounds access, leading to undefined behavior. (The memory layout of arrays dictates that, in effect, accessing slots[0][23] is equivalent to accessing slots[1][0], but that's just happenstance.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    ah dammit, slicing sounds bad >.<
    Why can't it just be simple!? D: well, I'm too lazy to fix it right now xP
    Currently research OpenGL

Popular pages Recent additions subscribe to a feed