• Popular Now

• 13
• 18
• 19
• 27
• 10

This topic is 3942 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, I have a class that holds all the objects of a class, called Npc. Then I have a 2d array of Npc pointers, which represent where the Npc's are at. So when I spawn an Npc, I do this little tidbit:
npcs.push_back(n);
manager->world->x[abs(n.x)].z[n.z].npc = &npcs[npcs.size() - 1];

Basically npcs is a vector of Npc objects, and then I try setting the grid's address at nx/nz to the address of the lastly added object of Npcs. I am pretty sure somethings going wrong here. and as a side note the abs(n.x) is mostly due to personal preference of the grid in my imagination and working with negative numbers as it goes on. Dont ask me why =/

Share on other sites
I all have to say wow you have a tough one. Maybe its because the pointer is some how pointing to a refernce. I don't know am just guessing.

Share on other sites
Well, I can't tell you what's going wrong without some description of what's going wrong or some code to see exactly how your manager object is structured. However, you could replace
&npcs[npcs.size() - 1]
with
&npcs.back()
which would make your code cleaner, more intuitive, and more managable should you decide to change the type of container represented by npcs.

Share on other sites
Well, depends on what you mean "wrong". First of all, for instance, this line:

manager->world->x[abs(n.x)].z[n.z].npc

makes me want to take a bath. It compiles and runs but it's just...bad. Better have a World::SetGrid(x,y,npc) function or something like that.

Other than that, you have to realize that std::vector is free to move elements into memory as you add or remove items. So what is now a valid address may not be later. Storing the address of an std::vector item for later use is wrong, unless the vector never resizes, which is not your case. A correct solution would be to make npcs a vector of pointers to NPC.

Share on other sites
Quote:
 Original post by mikemanWell, depends on what you mean "wrong". First of all, for instance, this line:manager->world->x[abs(n.x)].z[n.z].npcmakes me want to take a bath. It compiles and runs but it's just...bad. Better have a World::SetGrid(x,y,npc) function or something like that.Other than that, you have to realize that std::vector is free to move elements into memory as you add or remove items. So what is now a valid address may not be later. Storing the address of an std::vector item for later use is wrong, unless the vector never resizes, which is not your case. A correct solution would be to make npcs was a vector of pointers to NPC.

Thats the issue!

Sometimes it works and sometimes it doesnt, so it baffled me completely. I am terribly sorry for making you guys guess but my code is getting pretty beastly and its hard to tell what I need to include.

What do you reccommend I use? Would std::list be a better option? or should I use a slightly abstracted array?

Thanks so much!