If you do fix your own problem, explain it. That way others can understand their problems if they search for it, or learn new things.
For the above reason, here I go:
The pointers should remain valid as long as the user is in the arena, assuming the bot core does not do some funky things randomly. Once he leaves the arena, the pointer itself (if you made a local copy of it) will still point to something, just there won't be any valid data there, thus causing errors. So what you'll need to do is setup a function to clear, or NULL, your local pointer copies whenever you get the player-left calls.
Cyan~Fire - Wed Nov 12, 2003 6:05 pm
Post subject:
Yep, sorry.
BTW In case anyone wants to do something using my method:
What I did was create an array of player pointers in the class botInfo ("Player * caps[MAX_TEAMS];") and just store pointers in there from events ("caps[team] = p;"). It's a great way if you need to make a list of players. Just make sure you make a function like this one:
bool botInfo::check_leave(Player *p, int team) //This doesn't have to be a separate function if you don't need it outside of EVENT_PlayerLeave
{
if (team > setts.teams) return false;
if (caps[team] == p)
{
caps[team] = NULL;
//Do stuff
return true;
}
return false;
} |
Or, if you don't want to specify a team, then just do a for loop.
ExplodyThingy - Wed Nov 12, 2003 10:10 pm
Post subject:
Or just use catid's linked list. Its much less likely to cause an error. For example, say you setup someone in caps[0] through [5], and player [3] leaves. When you iterate through the list, youll succeed on accessing info in caps[0] through 2, but in 3 therell be a null pointer. however, 4 and 5 will still exist. Cats list allows for a Delete method which goes ahead and takes the next item's previous and points it to the one previous, and the previsous's next item and points it to the next.
50% Packetloss - Wed Nov 12, 2003 10:27 pm
Post subject:
yah, everything in datatypes.h is very useful.
Dustpuppy - Thu Nov 13, 2003 10:54 am
Post subject:
A linked list isn't necessary in this case, an array would do the job better.
Cyan~Fire - Thu Nov 13, 2003 6:24 pm
Post subject:
Yeah, linked list would mean making copies of every single player (lots of memory) and making sure you delete the entries, generally more complicated. I could see using linked list if I needed the players after they leave, but I don't.
Dr Brain - Thu Nov 13, 2003 9:53 pm
Post subject:
Uh, ever hear of pointers? And an array would have the same problem.
Choosing which data structure to use depends highly on the application. If you are going to have relitivly few inserts and deletes (random access), then an array/vector is the way to go, but if instead lookups are less common and you need to do a lot of changing, linked lists are the structure of choice.
Anonymous - Fri Nov 14, 2003 8:09 am
Post subject:
Theyre all pointers to the same block of memory. It wont make new copies of a player in the memory with either case, itll simply make a new refenerence to the same section. If you try to access the player from either of the list forms if the player has left, the core has already deleted the player's info in memory, and youll end up with pointers pointing to no where, and kablow.
The while loop in cats ll checks to make sure the parse pointer exists, which is a node of the list. You should add a "if(parse->item)" in this case, the item being the template class pointing to the Player. If the player doesnt exist, it will resolve false, save you from accessing nonexistant data, and allows you delete the node of the linkedlist.
Cyan~Fire - Fri Nov 14, 2003 4:03 pm
Post subject:
I just don't see why I should use something more complicated when it doesn't need it.
And about the invalid pointers, with that function I have, a player struct shouldn't be removed without that thing activating. It's worked so far.