Thread: Generalising methods to handle multiple weapon types

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195

    Generalising methods to handle multiple weapon types

    When my game is done, I'm going to have 6/7 different weapon types (although I may want to add new weapons in later, so I want to adopt a general technique for handling weapons in the game). At the moment, I have only lasers, and already have 8 methods in a class for that, although 6 of those 8 could be used for other weapon types aswell. So rather than having 6/7 versions of same 6 methods, to handle all the weapons, the plan was to write the code so that 6-10 methods could handle all different weapon types.

    Anyway, at present, I use linked lists for the weapons, with different linked lists for the weapon type. That way, every time a laser etc goes off the screen, its just a case of getting rid of that link in the list. But I'm having some problems generalising the code.

    These are some of the weapon types:
    Code:
    /* Laser Structure */
    struct LaserStruct
    {
           unsigned int weapon_position[2];
           unsigned int weapon_speed;
           LaserStruct *next_weapon;
    };
    
    /* Missile Structure */
    struct MissileStruct
    {
           unsigned int weapon_position[2];
           MissileStruct *next_weapon;
    };
    And this is some of the code:
    Code:
    unsigned int CGameWeapons::GetWeaponPosition(unsigned int array_idx1, void *current_node)
    {
             if( (array_idx1 < 2)&&(current_node != NULL) )
             {
                 return (current_node->weapon_position[array_idx1]);
             }
             else
             {
                 return (RETURN_ERROR);
             }
    }
    
    void CGameWeapons::SetWeaponPosition(unsigned int array_idx1, unsigned int position, void struct *current_node)
    {
         if( (current_node != NULL)&&(array_idx1 < 2) )
         {
                         current_node->weapon_position[array_idx1] = position;
         }
         
         return;
    }
    Although in order to generalise it, I had to declare the argument to the methods as void (rather than type LaserStruct or MissileStruct - which depends on the type of weapon), and because of that, when I try and compile, it says " `void*' is not a pointer-to-object type ". I understand the problem, but I'm not sure how to solve it.

    In the method declaration etc, I did try changing the pointer from type void to type void struct, although that didn't work, lol. Any ideas?
    Last edited by Swarvy; 05-21-2009 at 01:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accepting Multiple Types of Input
    By Junior89 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2006, 11:25 PM
  2. multiple types in one decleration
    By hebele in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2004, 07:23 AM
  3. multiple inputs of multiple types
    By frontz in forum C Programming
    Replies: 8
    Last Post: 01-19-2004, 02:57 PM
  4. Returning multiple types w/o a struct
    By Trauts in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2003, 11:04 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM