Lists in C++

Miscellaneous Forums/General Discussion/Lists in C++

Im trying to convert the LIST functions from this to C++:

Global tweaponlist:TList=New TList


Type tweapon

   Field originX#, originY#, power, h:Iscenenode
   Field dir:Byte,style:Byte,friendly:Byte

   Method updatebullets()
      dg_Movenode h,1,0,0
      If DG_NodeX(h)>680 drop();Return
    
   End Method

   Method drop()
      h.remove()
      tweaponlist.remove(Self)
   End Method

End Type

Function UpdateAllWeapons()
   bulletcount=0
   For Local w:tweapon=EachIn tweaponlist
      bulletcount :+ 1
      Select w.style
         Case WEP_BULLET
            w.updatebullets()
      End Select
   Next
End Function


Anyone know?

Basic crap with std::list
class CWeapon
{
  public:

  void update();  // you'd just implement these elsewhere
  void drop();
};

std::list<CWeapon*> m_weaponList;  // put this somewhere, maybe a static list in the CWeapon class, I dunno

...

// update weapons

int bulletCount = 1;
std::list<CWeapon*>::iterator weaponIter = m_weaponList.begin();

while ( weaponIter != m_weaponList.end() )
{
  bulletCount += 1;

  switch ( whatever )
  {
    ..  figure it out
  }

  ++weaponIter;  // could also use n++, but ++n is logically nicer
}


That's the gist of it without explaining it properly. Since you don't already seem to have an understanding of C++ or even basic knowledge of the classes provided by the STL, I would recommend first learning those. Just my two cents.

C++ comes with a standard library including a variety of list classes, each with their own pros and cons. The equivalent to BlitzMax's TList would probably be std::list:

#include <list>
using namespace std;

list<tweapon*> tweaponlist;


class tweapon
{
public:
   float originX, originY;
   int power;
   Iscenenode h;
   char dir, style, friendly;

   void updatebullets() {
      dg_Movenode(h, 1, 0, 0);
      if (DG_NodeX(h) > 680) {
         drop();
         return;
      }
   }

   void drop() {
      h.remove();
      tweaponlist.remove(this)
   }
};

void UpdateAllWeapons()
{
   int bulletcount = 0;

   list<tweapon>::iterator weaponIterator i;
   for (i = tweaponlist.begin(); i != tweaponlist.end(); ++i) {
      tweapon *w = (*i);
      ++bulletcount;
      switch (w->style) {
         Case WEP_BULLET:
            w->updatebullets();
            break;
      }
   }
}


Probably the biggest difficulty in learning STL lists like this are the use of iterators, but they're really not that complex, once you learn how they work.

BTW, you probably already know this, but in C++ you can define WEP_BULLET and all your other WEP_ stuff in a "enum":

enum WeaponType {
  WEP_BULLET,
  WEP_[...]
};


And numbers will automatically be assigned to them in order (or you can assign them manually if you want). The main advantage to this is tweapon.style can be defined as a WeaponType rather than a char, and therefore you get less potential bugs because C++ can tell do additional error checking, and if your IDE has code completion, it can give you a list of WEP_ items automatically when you need it.

Fantastic.


Thanks guys :)

Since you don't already seem to have an understanding of C++ or even basic knowledge of the classes provided by the STL, I would recommend first learning those. Just my two cents
I am alright with the classes, at least to the extent of my equivalent use in blitzmax. (with types/methods)
The class framework for my game is there i just had no decent dynamic storage as I was getting confused by all the different examples using different list types.
(I have temporarily been using fixed arrays to store the pointers until i could figure the lists out :)

Weird... I just noticed Noel's post. Somehow I didn't notice it yesterday when I posted :). Do you have some trick for posting backwards in time Noel?

Ever seen Ghost in the Shell? I hacked your brain.