• entries
    7
  • comments
    52
  • views
    22120

"I should finish this by the end of the year"

Sign in to follow this  

1117 views

LOL

I must admit that I haven't worked on this that much, only a few hours per week at most. I definitely missed the deadline, and I'm not finishing this anytime soon. That's fine, though.

I fixed the dialogue bug mentioned on last entry. As I said, some "blocks" of the dialogue incorrectly change position when you shoot, even though your character and the dialogue are not related at all.

sFUUWmG.png

I'm using C++, so the dialogue bubble is a std::vector that stores instances of my Sprite class, with each Sprite being a "block" of the bubble. The thing is, my Sprite class has its own std::vector of stuff, and also has a pointer member that points to a specific element inside that vector. The point of that pointer (heh) is to avoid iterating the vector each time to search for that specific element inside it. Bad practice? Probably.

So, back to the dialogue bubble. The code that put the "blocks" together was something like this.
.std::vector bubble;for (int i = 0; i < amountBlocks; i++) { Sprite sprite(bla, bla, bla); bubble.push_back(sprite);}
.
The way std::vector works is that it automatically allocates memory as you push new things in it, but when that happens it doesn't allocate space just for the new element but rather for the whole thing, and then copies all the elements to the new memory location while deleting the original elements. In other words, it's like building a whole new hotel and moving all the people to it instead of just building a new room for that new person that wanted to spend the night.


This is all fine and dandy, except for that damn pointer in the Sprite class. The following expertly drawed image tries to explain what happens when a new Sprite is pushed to the vector:

fhzuGxw.png

(1) The vector with a single sprite. The pointer inside the Sprite, which points to an element of an internal std::vector, is all good, and I'm enjoying life.
(2) A new Sprite is pushed to the vector. A new memory space is allocated, so the original Sprite is copied to the new location and also a new Sprite is added.
(3) The pointer of the first Sprite still points to the original location, oops!

Solution? Many:

- Reserve the memory of the vector beforehand using std::vector::reserve().
- Call a "updatePointers()" method for each element in the vector everytime something is pushed.
- Use std::list instead of std::vector, which only allocates the memory for the new element. It's more expensive to access the elements, though.
- Don't use pointers that point to something inside a vector at all.
- Delete the code, quit programming and enjoy life.

In my case I used the first, and just like that the problem was solved, yay!

Probably a bad practice on my part, as I learned C++ on my own so I rarely know what I'm doing. Personal note added: "be careful with pointers, and avoid them if you can".

Anyway, regarding the progress of the game itself, well... I'm still stuck making the first level. Yeah, I've been stuck with it since September or so. Not only I haven't been working much, but when I do, I work sloooooooow, I really take my time just putting things on the map, playing the level dozens of times and so on.

Here's a screenie of a little city from the first level. Making it was fun, it really takes me back to the days were I drew paper cities to make races using my toy cars. Good lord, I was poor.

sf18RlX.png

Thanks for reading!

Sign in to follow this  


2 Comments


Recommended Comments

Easy bug to fall foul of.

Can't you instead just store the index of the element, then look it up in the vector whenever you need it? The index won't change when the vector reallocates.

Share this comment


Link to comment

You know, that also would have worked. Now I feel stupid.
 
I guess I'll go refactor some code now, don't mind me...

Share this comment


Link to comment

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