Results 1 to 2 of 2

Thread: Some help with structuring my code - please

  1. #1
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Some help with structuring my code - please

    Hi All

    I have a 'style' problem, I'm hoping someone here can sort of help me out.

    In my games I typically have a Game Object (Sort of manages screens, states etc). (No probs here)

    Then I have a Map class. The map class controls the actual happenings in the game itself. It controls things like drawing, movement, and most importantly collisions and path finding.

    I also have a Item class. All objects in the game inherit from this base item class. The Item class knows about the Map (so all descendants can use the pathfinding etc).

    Heres my problem. I find my units get rediculously large. For Run-A-War I ended up with something like 15 classes in one unit.

    [pascal]
    Type
    TMap = Class;
    TItem = Class
    Map : TMap;
    End;
    TMap = Class
    .. Items : TList; ...
    Procedure AddItem(Item : TItem);
    End;

    Procedure TMap.AddItem(Item : TItem);
    Begin
    Items.Add(Item);
    DrawItems.Add(Item);
    Item.Map := Map;
    End;
    [/pascal]

    Now the reason for the resulting big units is trying to do a little information hiding by using protected and private methods. If I start having other units I find all my Map methods and properties becoming public which I'm trying to avoid.

    Any suggestions.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  2. #2

    Some help with structuring my code - please

    Classes declared in the same unit are implicit "friends" (to use C++ terminology), so they have access to the private and protected members of other classes declared in that unit.

    One of the shortcomings of Object Pascal is shown by your example. Type1 has a member of Type2, and Type2 has a member of Type1. For this to work in Object Pascal, you have to provide the full declaration and implementation of both Type1 and Type2 in the same unit. In C++, this is easily accomplished because you can forward declare a type in one header, but provide the actual declaration in another header. Thus you would have something like this:

    Item.h
    Code:
    class TMap;
    
    class TItem
    {
    private:
      TMap *Map;
    };
    Map.h
    Code:
    class TItem;
    
    class TMap
    {
    private:
      TList *Items;
    protected:
      void AddItem(TItem *Item);
    };
    What you could do is to separate the classes into .inc files. With this, you would have something like the following:

    GameObjects.pas
    Code:
    unit GameObjects;
    
    interface
    
    uses
      Classes;
    
    type
      // Forward declarations
      TMap = class;
    
    {$I ItemInterface.inc}
    {$I MapInterface.inc}
    
    implementation
    
    {$I MapImplementation.inc}
    
    end.
    ItemInterface.inc
    Code:
      TItem = class
      protected
        Map: TMap;
      end;
    MapInterface.inc
    Code:
      TMap = class
      private
        Items: TList;
      protected
        procedure AddItem(Item: TItem);
      end;
    MapImplementation.inc
    Code:
    procedure TMap.AddItem(Item: TItem);
    begin
      Items.Add(Item);
      DrawItems.Add(Item);
      Item.Map := Self;
    end;
    Advantages:
    :arrow: Files are a lot smaller.

    Disadvantages:
    :arrow: Interface is in a separate file to the implementation (but then, that is what C++ is like anyway)
    :arrow: No code completion or class completion in the Delphi IDE.
    :arrow: A lot more files to manage.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •