Jump to content
  • Advertisement

BurdenJohn

Member
  • Content Count

    26
  • Joined

  • Last visited

Community Reputation

132 Neutral

About BurdenJohn

  • Rank
    Member
  1. I wanted to know when it comes to using UDP in a game for real-time movement, if I should build an ack layer on top of it.   My game uses an 8-directional movement system. And right now I'm sending keypresses/keyreleases. It works completely fine with UDP, but I'm running it locally.   Eventually when players try sending data to my server via my external IP, there are most likely going to be datagrams dropped. So I was thinking of building an ack layer over top of it, which works like this:   My player would send a movement datagram, saying that he moved to the left. The server may/may not receive this. If it does receive this, the server will then send back an acknowledgement. If the client does not receive this after a certain amount of time (I set an alarm which is equal to double the ping), then we assume the datagram was dropped and we send the exact same data (move left) to the server and the process repeats until we have an acknowledgement.   The same system is implemented on the server in which the server would then send the move packet back to the client to allow it to move, and wait for an acknowledgement. If it doesn't get one, it re-transmits.   So now it comes to my two questions:   1) Is this a fairly efficient system for an acknowledgement system over-top of UDP messages that are required to be sent/received? When it comes to things like movement and actions in real-time in which dropped messages can have an effect on the game/   2) Is this system really necessary? In other words, do I really need to worry about datagrams being dropped?   I've heard that in reality, it really doesn't happen that often, but when you've got a ton of real-time movement data being transmitted back and forth, one would think it would happen.   The thing is, the way my movement system works is, the server is in control of the movement. The client simply sends a keypress/keyrelease and doesn't move the player until the server sends back a movement datagram. I mention this because, if a datagram gets dropped and I don't have any sort of re-transmit system in place, there would be times when the player would press the movement keys and not even move anywhere because a datagram got dropped.   This can be very annoying, and I know games such as Halo use this system (waiting for the server to tell us to move rather than the clients moving on their own). And games like that, you hardly ever have times where you would press the movement keys and nothing would happen. Most likely because they have some sort of UDP-ack-reliable system implemented overtop of UDP.   So is it worth it to implement an ack system for things like UDP movement? Or should I not even worry about it? I'm not exactly sure on average, how often a datagram is dropped, and if it would heavily interfere with gameplay?
  2. BurdenJohn

    Topdown wall collision, not working

    Got it working! //Simple rectangle intersection function bool intersect(RECTANGLE a, RECTANGLE b) { if (a.left < b.right && b.left < a.right && a.top < b.bottom) return b.top < a.bottom; else return false; } //Loops through all solids to see if two rectangles (given offset) //are intersecting. bool Player::place_meeting(int offsetx, int offsety, RECTANGLE prect){ RECTANGLE pNew; pNew = prect; pNew.left+=offsetx; pNew.right+=offsetx; pNew.top+=offsety; pNew.bottom+=offsety; bool inter = false; for(std::vector<Block>::iterator block = blocks->begin(); block != blocks->end(); ++block) { RECTANGLE brect; brect.left = block->x; brect.top = block->y; brect.right = block->x + block->size; brect.bottom = block->y + block->size; inter = intersect(pNew, brect); if(inter) break; } return inter; } //Player update (60fps) //left, right, up, and down are determined by keypress/keyrelease events void Player::update(){ int spd = 2; RECTANGLE prect; prect.left = this->x; prect.top = this->y; prect.right = this->x + this->size; prect.bottom = this->y + this->size; //Detect if we haven't hit anything (or in this case, are going to //because speed is a factor. //If we aren't going to hit anything, let the player keep moving //in that direction if (left) {if (!(place_meeting(-spd,0, prect))) {x -= spd;}} if (right) {if (!(place_meeting(spd,0,prect))) {x += spd;}} if (up) {if (!(place_meeting(0,0-spd,prect))) {y -= spd;}} if (down) {if (!(place_meeting(0,0+spd,prect))) {y += spd;}} } I detect for the collision using my version of a Game Maker function called: place_meeting(). It simply detects (with an offset), if my players rectangle is going to intersect with a blocks rectangle, given my speed as an offset. And if it is, I stop letting my player move in that direction.   Works like a charm.
  3. BurdenJohn

    Topdown wall collision, not working

      Well now I'm trying it your way as I'm more comfortable with this method, can't be looking through tons of code right now to solve this simple task.   Unfortunately, I'm still getting stuck along the edges, even when I check against what directions I'm going.   Here's a picture showing what's happening now for the following cases I'm testing (I only test two cases for now since there's no point in going forward if my player is all ready getting stuck):   Currently right now I have the following cases: - Player is going up but NOT left or right (only going up) - Player is going up AND left, but not right (up and left)   Even with just two cases so far, the player is all ready getting stuck..   If my player is going up and left, and he's colliding against the right side of the block, its smooth! Perfect, right?   If my player is going up, I'm only checking collision against the bottom of the block, it works! Perfect, right? Well if my player is going up and left and he's colliding against the bottom of the blocks, as he glides to the left across the bottom of the blocks, he gets stuck on the edges between the two blocks. (x = brect.right + 1)   This is bad, and it's the exact same thing that was happening before.   Here are my collision functions: //player right collides with box left bool SFMLPlayer::check_pright_bleft(RECTANGLE* _prect, RECTANGLE* _brect, int move_speed){ RECTANGLE prect = *_prect; RECTANGLE brect = *_brect; int offset = move_speed; if(prect.right > brect.left && prect.right <= brect.left + offset){ if ( (prect.top >= brect.top && prect.top < brect.bottom) ){ return true; } else if ( (prect.bottom >= brect.top && prect.bottom < brect.bottom) ){ return true; } } return false; } //player left collides with box right bool SFMLPlayer::check_pleft_bright(RECTANGLE* _prect, RECTANGLE* _brect, int move_speed){ RECTANGLE prect = *_prect; RECTANGLE brect = *_brect; int offset = move_speed; if(prect.left < brect.right + offset && prect.left > brect.right - offset){ if ( (prect.top >= brect.top && prect.top < brect.bottom) ){ return true; } else if ( (prect.bottom >= brect.top && prect.bottom < brect.bottom) ){ return true; } } return false; } //player bottom collides with box top bool SFMLPlayer::check_pbottom_btop(RECTANGLE* _prect, RECTANGLE* _brect, int move_speed){ RECTANGLE prect = *_prect; RECTANGLE brect = *_brect; int offset = move_speed; if(prect.bottom > brect.top && prect.bottom <= brect.top + offset){ if( (prect.right >= brect.left && prect.right < brect.right) ){ return true; } else if( (prect.left >= brect.left && prect.left < brect.right) ){ return true; } } return false; } //player top collides with box bottom bool SFMLPlayer::check_ptop_bbottom(RECTANGLE* _prect, RECTANGLE* _brect, int move_speed){ RECTANGLE prect = *_prect; RECTANGLE brect = *_brect; int offset = move_speed; if(prect.top < brect.bottom && prect.top >= brect.bottom - offset){ if( (prect.right >= brect.left && prect.right < brect.right) ){ return true; } else if( (prect.left >= brect.left && prect.left < brect.right) ){ return true; } } return false; } And here is where I'm using the test cases, inside the block iteration (looping through all blocks in the vector):   for(std::vector<Block>::iterator block = blocks->begin(); block != blocks->end(); ++block) { RECTANGLE prect; prect.left = this->x; prect.top = this->y; prect.right = this->x + this->size; prect.bottom = this->y + this->size; RECTANGLE brect; brect.left = block->x; brect.top = block->y; brect.right = block->x + block->size; brect.bottom = block->y + block->size; //AABB collision approach int offset = move_speed; //We are ONLY going up, check the top of player against bottom of block if(up && !right && !left){ if( check_ptop_bbottom(&prect, &brect, move_speed) ){ y = brect.bottom+1; } } //We are going UP AND LEFT, check the left side of player against right side of block, //AND also check the top of player against bottom of block (we could be on the right side or bottom side of the block) else if(up && (left && !right)){ if( check_pleft_bright(&prect, &brect, move_speed) ){ x = brect.right + 1; } else if( check_ptop_bbottom(&prect, &brect, move_speed) && prect.top > brect.top ){ y = brect.bottom; } } } Does anyone have any insight as to why my player gets stuck while gliding to the left along the bottom of the blocks, yet he does not get stuck while gliding up along the right side of the blocks?   Where in my collision code, or even my test case code, is it allowing the player to stop moving to the left when he's on the bottom of the blocks and pushing up? I've spent way too long on this and its unfortunately blocking me from doing any other work.   I've tried everything from changing an "<=" to an "<", placing offsets in different places, I just can't figure out how to make it so I can smoothly glide across all sides. There's something wrong in my collision code and its incredibly hard to detect.
  4. BurdenJohn

    Topdown wall collision, not working

    I'm sorry I just couldn't get your rectangle_collision_x/y() functions working. I'm extremely close now though, but still stuck.   Here's my current collision code: for(std::vector<Block>::iterator block = blocks->begin(); block != blocks->end(); ++block) { RECTANGLE prect; prect.left = this->x; prect.top = this->y; prect.right = this->x + this->size; prect.bottom = this->y + this->size; RECTANGLE brect; brect.left = block->x; brect.top = block->y; brect.right = block->x + block->size; brect.bottom = block->y + block->size; //AABB collision approach int deca = 3; //PLAYER RIGHT TOUCHES BOX LEFT //Check if player right side touches box left side if( (prect.right + move_speed >= brect.left) && (prect.right + move_speed < brect.left + move_speed*deca) ){ //Check if we are within the vertical value of the left side if ( (prect.top >= brect.top && prect.top <= brect.bottom) || (prect.bottom >= brect.top && prect.bottom <= brect.bottom) ){ x = brect.left-size-move_speed/(deca-1); //continue; } } //PLAYER LEFT TOUCHES BOX RIGHT //Check if player left side touches box right side else if( (prect.left - move_speed <= brect.right) && (prect.left - move_speed > brect.right - move_speed*deca) ){ //Check if we are within the vertical value of the right side if ( (prect.top >= brect.top && prect.top <= brect.bottom) || (prect.bottom >= brect.top && prect.bottom <= brect.bottom) ){ x = brect.right+move_speed/(deca-1); //continue; } } //PLAYER BOTTOM TOUCHES BOX TOP //Check if player bottom side touches box top side else if( (prect.bottom + move_speed >= brect.top) && (prect.bottom + move_speed < brect.top + move_speed*deca) ){ //Check if we are within the horizontal value of the bottom side if( (prect.left >= brect.left && prect.left <= brect.right) || (prect.right >= brect.left && prect.right <= brect.right) ){ y = brect.top-size-move_speed/(deca-1); //continue; } } //PLAYER TOP TOUCHES BOX BOTTOM //Check if player top side touches box bottom side else if( (prect.top - move_speed <= brect.bottom) && (prect.top - move_speed > brect.bottom - move_speed*deca) ){ //Check if we are within the horizontal value of the bottom side if( (prect.left >= brect.left && prect.left <= brect.right) || (prect.right >= brect.left && prect.right <= brect.right) ){ y = brect.bottom+move_speed/(deca-1); //continue; } } } So what's happening now is, the sides are perfect! I can glide across the sides very easily!   Now let's say the top of my player (prect.top) is touching the bottom of the wall of blocks. I can glide to the right, though when it hits edges, it jitters a bit, but it still glides, but the jitterness still needs to be fixed. However if I glide to the left and I hit edges, the player stops.   The same as when the bottom of my player (prect.bottom) is touching the top of the wall of blocks (brect.top), gliding to the right works, but jittery (not smooth) when it hits edges, gliding to the left, when you hit an edge, player stops.   My top and bottom block collision checks are under the following comments: //PLAYER BOTTOM TOUCHES BOX TOP   and   //PLAYER TOP TOUCHES BOX BOTTOM   EDIT: I just noticed that since the player is stopping in the x-coordinate, the two statements that are causing it are the ones under these comments: //PLAYER RIGHT TOUCHES BOX LEFT //PLAYER LEFT TOUCHES BOX RIGHT   I'm extremely close to finally getting it perfect, just need to fix: - Jitterness when gliding to the right against bottom/top of blocks, only when the player hits the edges of a block - The player stopping when gliding to the left against bottom/top of blocks, only when the player hits the edges of a block   Does anyone know, possibly through the two specified if statements under those two comments I mentioned, what could be causing it and how to fix it?   I've tried a large combination of things. One thing I did to fix my problem from before was, I change all of the if's to else-ifs.
  5. BurdenJohn

    Topdown wall collision, not working

    Hmm it didn't seem to work. I'm now trying the AABB approach:   for(std::vector<Block>::iterator block = blocks->begin(); block != blocks->end(); ++block) { RECTANGLE prect; prect.left = this->x; prect.top = this->y; prect.right = this->x + this->size; prect.bottom = this->y + this->size; RECTANGLE brect; brect.left = block->x; brect.top = block->y; brect.right = block->x + block->size; brect.bottom = block->y + block->size; int deca = 3; //PLAYER RIGHT TOUCHES BOX LEFT //Check if player right side touches box left side if( (prect.right + move_speed >= brect.left) && (prect.right + move_speed < brect.left + move_speed*deca) ){ //Check if we are within the vertical value of the left side if ( (prect.top >= brect.top && prect.top <= brect.bottom) || (prect.bottom >= brect.top && prect.bottom <= brect.bottom) ){ x = brect.left-size-move_speed/(deca-1); //continue; } } //PLAYER LEFT TOUCHES BOX RIGHT //Check if player left side touches box right side if( (prect.left - move_speed <= brect.right) && (prect.left - move_speed > brect.right - move_speed*deca) ){ //Check if we are within the vertical value of the right side if ( (prect.top >= brect.top && prect.top <= brect.bottom) || (prect.bottom >= brect.top && prect.bottom <= brect.bottom) ){ x = brect.right+move_speed/(deca-1); //continue; } } //PLAYER BOTTOM TOUCHES BOX TOP //Check if player bottom side touches box top side if( (prect.bottom + move_speed >= brect.top) && (prect.bottom + move_speed < brect.top + move_speed*deca) ){ //Check if we are within the horizontal value of the bottom side if( (prect.left >= brect.left && prect.left <= brect.right) || (prect.right >= brect.left && prect.right <= brect.right) ){ y = brect.top-size-move_speed/(deca-1); //continue; } } //PLAYER TOP TOUCHES BOX BOTTOM //Check if player top side touches box bottom side if( (prect.top - move_speed <= brect.bottom) && (prect.top - move_speed > brect.bottom - move_speed*deca) ){ //Check if we are within the horizontal value of the bottom side if( (prect.left >= brect.left && prect.left <= brect.right) || (prect.right >= brect.left && prect.right <= brect.right) ){ y = brect.bottom+move_speed/(deca-1); //continue; } } } Unfortunately, this seems to work only for blocks that are alone and not touching another.   If I place a line of blocks, and my player starts gliding along it (say, the top of my player glides along the bottom of the line of blocks), once my player hits the edge of a block, he stops.   This is taking me way longer than it should, I should all ready be coding other parts of the game, but this is completely blocking me.
  6. BurdenJohn

    Topdown wall collision, not working

    Hmm changing my IntersectRect() code to that seems to make my player go through all walls now:   bool SFMLPlayer::IntersectRect(RECTANGLE* r1, RECTANGLE* r2) { return ! ( r2->left > r1->right || r2->right < r1->left || r2->top < r1->bottom || r2->bottom > r1->top ); } EDIT: All right well I have a better intersection code now, my player finally detects collisions against the walls and doesn't go through them, however I can't glide across them, my player just gets stuck when I push against the normal of the side of the wall.   Here's my new rectangle intersection code: bool rectangle_collision(float x_1, float y_1, float width_1, float height_1, float x_2, float y_2, float width_2, float height_2) { return !(x_1 > x_2+width_2 || x_1+width_1 < x_2 || y_1 > y_2+height_2 || y_1+height_1 < y_2); } And my for loop using it (with the old intersection function call commented out): for(std::vector<Block>::iterator block = blocks->begin(); block != blocks->end(); ++block) { RECTANGLE prect; prect.left = this->x + hmove; prect.top = this->y + vmove; prect.right = prect.left + this->size; prect.bottom = prect.bottom + this->size; RECTANGLE brect; brect.left = block->x; brect.top = block->y; brect.right = block->x + block->size; brect.bottom = block->y + block->size; /* if(IntersectRect(&prect, &brect)){ place_meeting = true; break; } */ if(rectangle_collision(prect.left, prect.top, 32, 32, brect.left, brect.top, 32, 32)){ place_meeting = true; break; } } Now by stuck, I mean when I move up to a side of one of the blocks, I can't move anywhere else except the normal (opposite direction to which I'm facing). Once I move that way, I'm free to move again until I meet a side of the block again.   For example, if my player is moving to the bottom side of a block, and it hits that side, I can't move left or right. The only direction I can move is down. Once I move down, I'm free to move anywhere I want until I hit another side again.   If I hit the left side of a block (the right side of my player hitting the left side of a block), I can't move up or down, I can only move left to free myself.   And so on.   Not sure why this is happening, but I'm close. Just need to be able to glide across the sides.
  7. THIS CODE ON THIS POST IS NOW OBSOLETE. Please refer to this post (current method for movement collisions in a topdown environment): http://www.gamedev.net/topic/657046-topdown-wall-collision-not-working/?view=findpost&p=5156335   Still having trouble, player gets stuck along the bottom of the blocks while moving to the left and pushing up (player stops moving to the left for some reason).   Using SFML simply for drawing, gameplay is implemented using basic logic   I'm currently trying to do smooth top-down collision in which I use intersecting rectangles to detect if my player is going to be colliding with a rectangle given his velocity in x- and y-directions.   Here's my rectangle struct:   arectangle.h struct RECTANGLE{ int left, top, right, bottom; }; My block class: #include <SFML/Graphics.hpp> #pragma once class Block{ sf::RenderWindow* window; public: Block(sf::RenderWindow* _wind, int _x, int _y, int _size); sf::RectangleShape mask; void draw(); int x, y; int size; }; The main importance in the block class is x, y, and size (32 in my test case). Here's my block instances, as well as my player which gets a reference to the blocks vector: (main.cpp) vector<Block> blocks; Block b1(&window, 96, 32, 32); Block b2(&window, 32, 160, 32); Block b3(&window, 0, 0, 32); blocks.push_back(b1); blocks.push_back(b2); blocks.push_back(b3); SFMLPlayer player(&window, &blocks); The player is situated at position 32, 32 (with size rectangle 32).   And now here's the collision code, in SFMLPlayer.cpp: bool SFMLPlayer::IntersectRect(RECTANGLE* r1, RECTANGLE* r2) { return ! ( r2->left > r1->right || r2->right < r1->left || r2->top > r1->bottom || r2->bottom < r1->top ); } //Gets run at 60fps void SFMLPlayer::update(){ int room_width = 200; //temp int room_height = 200; //temp int hmove, vmove; hmove = (right - left); vmove = (down - up); int move_speed = 2; bool place_meeting = false; for(std::vector<Block>::iterator block = blocks->begin(); block != blocks->end(); ++block) { RECTANGLE prect; prect.left = this->x + hmove; prect.top = this->y + vmove; prect.right = prect.left + this->size; prect.bottom = prect.bottom + this->size; RECTANGLE brect; brect.left = block->x; brect.top = block->y; brect.right = block->x + block->size; brect.bottom = block->y + block->size; if(IntersectRect(&prect, &brect)){ place_meeting = true; break; } } x += ( hmove * move_speed ) * !place_meeting; y += ( vmove * move_speed ) * !place_meeting; //room boundaries check if(x + size + hmove > room_width+1){ x = room_width - size + 2; } if(x - hmove < 0-1){ x = -2; } if(y + size + vmove > room_height+1){ y = room_height - size + 2; } if(y - vmove < 0-1){ y = -2; } mask.setPosition(x, y); } I'm using a boolean: place_meeting() to detect if my (player + velocity) rectangle is going to hit any of the blocks using my InterSectRect(RECT*, RECT*) function.   And if it is, I set place_meeting to true and break out of the loop.   There are a few problems which I will display in an image: I can't even get to the left side of the room, I'm blocked out by that block in the bottom left. Its like its entire left side is expanded to the other parts of the room (as shown in the green marker line in the second picture).   After some more testing I found out that I can't get past the top part of any blocks. I can get past the bottom parts though.   Here's three more pictures:
  8. Thanks for the information everyone.
  9. Is it at all possible to just write out the terms of the contract, as I've done on this topic, and just put a place for two signatures between the two participants? Or would that not be a legal contract?
  10. Thank you all for the suggestions. I'm going to go with Giallanon's suggestion, was very simple and it works!   Thanks guys.
  11. Honestly this was the simplest solution! Just a quick question though, here's my removePlayer function:   int PlayerList::removePlayer(unsigned int id){ //std::cout << "===REMOVING PLAYER===" << std::endl; if(players[id] != NULL){ delete players[id]; players[id] = NULL; } return 0; } It works when I remove the line "delete players[id];" For some reason when I leave that in, I get an exception when I try to execute removePlayer(2) for example. It says the heap gets corrupted.   Why is that? And also, why do I need to delete that piece of memory, because when I set it to NULL, the reference is gone anyways? Or would the player instance still be in memory? If that's the case, how do I get around this issue where it corrupts the heap?
  12. Mmm I could only find one that was close to what I wanted: https://www.docracy.com/5843/commissioned-illustration-offer-short-easy   However it doesn't cover the fact that: - The price of the agreement is to be paid in full after the job is done, this states before - It doesn't talk about profit-share   As for the list of game attorneys, are there any free ones out there?
  13. The thing is the error comes from File: "xmemory0" at line 617: #define _ALLOC_MEMBER_CONSTRUCT( \ TEMPLATE_LIST, PADDING_LIST, LIST, COMMA, CALL_OPT, X2, X3, X4) \ template<class _Objty COMMA LIST(_CLASS_TYPE)> \ void construct(_Objty *_Ptr COMMA LIST(_TYPE_REFREF_ARG)) \ { /* construct _Objty(_Types...) at _Ptr */ \ ::new ((void *)_Ptr) _Objty(LIST(_FORWARD_ARG)); \ } _VARIADIC_EXPAND_0X(_ALLOC_MEMBER_CONSTRUCT, , , , ) #undef _ALLOC_MEMBER_CONSTRUCT Also, commenting out push_back() seemed to get the error away, but of course I still need that in (caused an exception). So I tried your emplace_back() suggestion, and I get the same error as before (cannot access private member...) line 617 of file xmemory0 as posted above. Here's my playerList.cpp code as it stands: http://pastie.org/private/0lhdxjoczzjfdg2acyzq   Does it seem like my list would work though using these methods?
  14. Hmm not sure what's going on here, but now I'm trying to use std::unique_ptr but I'm getting a private member access error? #include "playerList.h" void PlayerList::printList(){ std::cout << "PRINTING PLAYER LIST" << std::endl; for(std::vector<std::unique_ptr<Player>>::iterator it = playList.begin(); it != playList.end(); ++it) { Player* p = it->get(); std::cout << '[' << p->id << ']' << p->name << std::endl; } } void PlayerList::addPlayer(Player* p){ std::cout << "ATTEMPTING TO ADD PLAYER: " << p->name << std::endl; std::unique_ptr<Player> uptr(p); unsigned int count = 0; unsigned int nullCount = 0; //Check for a null player, if we find one, replace it with the new one for(std::vector<std::unique_ptr<Player>>::iterator it = playList.begin(); it != playList.end(); ++it) { count++; Player* ps = it->get(); if(it->get() == nullptr){ nullCount++; unsigned int pos = it - playList.begin(); it->swap(uptr); it->get()->id = pos; return; } } //If we didn't find a null player unsigned int pos = playList.size(); uptr->id = pos; playList.push_back(uptr); } void PlayerList::removePlayer(unsigned int id){ for(std::vector<std::unique_ptr<Player>>::iterator it = playList.begin(); it != playList.end(); ++it) { Player* p = it->get(); if(p->id == id){ std::cout << "REMOVING PLAYER: " << p->name << std::endl; it->reset(); } } }   The idea now is that when I add a player, I check for any null players that are in the list.   If I find one, I make swap the null player with the new player I'm adding, and the new player gets the id that's equal to the current position of the iteration ( it - playList.begin() )     A null player is now a player that, when it gets removed, its unique_ptr is reset().   Not sure if this works though, can't figure out why I'm getting that error. I've read that it has to do with making a copy? I don't recall ever making a copy of the unique_ptr?
  15. Well I'm off to an okay start. Made a custom list class that uses a vector.   So far when I add players, their ids are fine. When I remove a player, the players still retain their ids, which is fine, however after removing a player and then adding a new player, the player does not fill the position of the one that was previously removed.   Example:   push_back josh, tod, bob, tim, steph, giorgio.   [0] Josh [1] Tod [2] Bob [3] Tim [4] Steph [5] Giorgio   remove bob List now prints:   [0] Josh [1] Tod [3] Tim [4] Steph [5] Giorgio   push_back Greg   List now prints: [0] Josh [1] Tod [3] Tim [4] Steph [5] Giorgio [5] Greg   The reason why 5 is repeated is because the id that is assigned to the player is based on the size of the list. However I need it so the ID is based on the position that it fills, but currently the vector isn't filling the list, its just pushing it to the back. Not sure how to fill empty slots (in this case, position 2 was supposed to be assigned to greg).   playerList.h #include <vector> #include "player.h" #include <iostream> #pragma once class PlayerList{ std::vector<Player> playList; //the list public: void addPlayer(Player* p); //adds Player p void removePlayer(unsigned int id); //removes player given id Player getPlayer(unsigned int id); //gets player given id void printList(); }; playerList.cpp #include "playerList.h" void PlayerList::printList(){ for(std::vector<Player>::iterator it = playList.begin(); it != playList.end(); ++it) { std::cout << "[" << it->id << "] " << it->name << std::endl; } } void PlayerList::addPlayer(Player* p){ //std::cout << "ADDING PLAYER, LIST SIZE: " << playList.size() << std::endl; unsigned int theId = playList.size(); p->id = theId; playList.push_back(*p); std::cout << "ADDING PLAYER, theId: " << theId << std::endl; } void PlayerList::removePlayer(unsigned int id){ for(std::vector<Player>::iterator it = playList.begin(); it != playList.end(); ++it) { if(it->id == id){ playList.erase(it); } } } One thing I could do, is when adding a player, check the list for any blank spots that aren't filled, but how do I do this? When I remove a player, how would I mark its position in the list as "blank" rather than just erase it completely?
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!