Using list to store structs

Ask questions about cheating in any games you would like. Does not need to pertain to MicroMacro.
Post Reply
Message
Author
Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

Using list to store structs

#1 Post by Exempt » Mon Oct 31, 2011 1:03 pm

I'm completely new to lists with c++ and I'm having some issues
The struct and th list is declared like this.

Code: Select all

struct unitTarget
{
	uint16 unitid;
	uint16 positionX;
	uint16 positionY;
	uint16 model;
	uint8 animation;
};
list<unitTarget> targets;
I'm trying to store the data into the list with no luck... There is alot of stuff that will have to change to make it work right. x,y model and animation can all be updated at different times in the packet but I need a way to.. Put only the data that changed into the list according to the unitid.

Code: Select all

unitTarget myTarget;
			myTarget.unitid = unitid;
			if(positionx != NULL) myTarget.positionX = positionx;
			if(positiony != NULL) myTarget.positionY = positiony;
			if(model != NULL) myTarget.model = model;
			if(animation != NULL) myTarget.animation = NULL;

			targets.push_back(myTarget);
			
			list<unitTarget>::iterator it;
			for(it=targets.begin(); it != targets.end(); it++)
			{
				bool isDead = false;
				if((*it).animation == 45 || (*it).animation == 21) isDead = true;

				printf("ID: %i, X: %i, Y: %i, Mod: %i, Anim: %i\r", (*it).unitid, (*it).positionX, (*it).positionY, (*it).model, (*it).animation);
				//targets.remove_if(isDead);
			}

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Using list to store structs

#2 Post by Administrator » Mon Oct 31, 2011 1:42 pm

I'm not sure what you're trying to do there. If you are only trying to update your list of objects, why do you always insert a new object into the list?

You should pretty much just do something like this:

Code: Select all

bool found = false;
for(it=targets.begin(); it != targets.end(); it++)
{
  if (it->unitid ==unitid)
  {
    it->positionX = positionx;
    // other changes here
    found = true;
    break;
  }
}
if( !found )
{
  // insert a new object; this one isn't found
}
If this list is going to be larger than, say, 100, you would probably want to use a map instead of iterating the vector every time.

Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

Re: Using list to store structs

#3 Post by Exempt » Mon Oct 31, 2011 2:26 pm

I don't think it'll get to 100 but it is possible in some cases but for my uses it likely will be 0-30. That is similar to my current code but I wasn't using -> that may be my current issue. Ty.
Edit: I forgot to ask about destoying one of the elements. Say one of the units is dead animation will == 21 or 45 I need to destroy this from the list.

I set all the positionx, positiony... varibles to = NULL just before I update them with information from the packet, so it the packet doesn't have that information with it. It will just use what is already stored inside the list for the unitid.
I'm having issues tho because even when the positionx is not NULL it is not getting updated in the list for some reason.

Code: Select all

unitTarget myTarget;
			myTarget.unitid = unitid;
			if(positionx != NULL) myTarget.positionX = positionx;
			if(positiony != NULL) myTarget.positionY = positiony;
			if(model != NULL) myTarget.model = model;
			if(animation != NULL) myTarget.animation = animation;

			bool found = false;
			for(it=targets.begin(); it != targets.end(); it++)
			{
				if(it->unitid == unitid)
				{
					if(it->animation == 21 || it->animation == 45)
					{
						printf("Target ID %i is dead.\n\n", it->unitid);
						targets.erase(it);
					}
					printf("Found ID %i match adjusting info.\n", it->unitid);
					if(positionx == NULL) myTarget.positionX = (*it).positionX;
					if(positiony == NULL) myTarget.positionY = (*it).positionY;
					if(model == NULL) myTarget.model = (*it).model;
					if(animation == NULL) myTarget.animation = (*it).animation;
					printf("ID: %i, X: %i, Y: %i, Mod: %i, Anim: %i\n\n", it->unitid, it->positionX, it->positionY, it->model, it->animation);
					found = true;
					break;
				}
			}
			if(!found)
			{
				printf("ID was not found adding new Element\n\n");
				targets.push_back(myTarget);
			}

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Using list to store structs

#4 Post by Administrator » Mon Oct 31, 2011 5:43 pm

Code: Select all

               if(positionx == NULL) myTarget.positionX = (*it).positionX;
Isn't this backwards? Shouldn't you be checking if it isn't null, and setting the iterators positionX, not checking if it is null and getting the position from it's positionX?

Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

Re: Using list to store structs

#5 Post by Exempt » Mon Oct 31, 2011 8:29 pm

Lol, yes I should. I meant to post that update but I forgot. I decided to go ahead and make it a map after reading about them. It's so much easier to sort one for this purpose.

Thanks a bunch. :)

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests