Results 1 to 5 of 5

Thread: Reference between different units

  1. #1

    Reference between different units

    Hello,
    I am developping a strategy game using Lazarus. In a first unit called Map, I defined the classes TProvince et TSquare (a square belongs to a province). In the Economy unit, I defined the following classes for the economic objects in a province or in a square : TProvinceEconomy, TSquareEconomy, TActivitySector (an industry in a square), Tware (a ware available in a province).

    Classes which represent industries (e.g. Farming) inherit from TActivitySector. Classes which represent wares (e.g. wheat), inherit from Tware. Each industry produce uses several wares to produce a ware, and therefore should be associated with the TWare object. An industry belongs to a square economy. A ware belongs to a province economy.

    I also defined a few abstract classes and interfaces in another unit to help with circular references. The Map unit has a reference on the Economy unit in its implementation part.

    I hope my explaination is clear.

    Map unit :
    Code:
    Unit Map
    TProvince = class
    FEconomy :  TAbstractProvinceEconomy; // A province economy
    FSquare : array of TSquare; // The squares which belong to the province
    constructor Create;
    end;
    
    TSquare = class
    FProvince : TProvince; // The province the square belongs to.
    FEconomy :  TAbstractSquareEconomy; // A square economy
    constructor Create(AProvince : TProvince );
    end;
    
    constructor TProvince.Create;
    begin
    self.FEconomy := TProvinceEconomy.Create;
    SetLength(self.FSquares, 2);
    self.FSquares[0] := TSquare.Create(self);
    end;
    When creating a TSquare object, I give the province as a parameter, and I create a TSquareEconomy.

    Economy unit :
    Code:
    Unit Economy
    TWare = class(TAbstractWare)
    FName : string;
    end;
    
    TProvinceEconomy = class(TAbstractProvinceEconomy)
    FWares : array of TAbstractWare; // Liste of available wares
    constructor Create;
    end;
    
    TSquareEconomy = class(TAbstractSquareEconomy)
    FSectors : array of TActivitySector; // List of industries
    end;
    
    // Secteur d'activit?©
    TActivitySector = class
    FName : string;
    // The ware produced by the industry
    FWare : TWare;
    constructor Create;
    end;
    
    // Secteur Agriculture (inherits from TActivitySector)
    TFarming = class(TActivitySector)
    constructor create;
    end;
    
    TWheat = class(TWare)
    end;
    
    constructor TProvinceEconomy.Create;
    begin
    SetLength(self.FWares, 2);
    // The ware "wheat" is available.
    self.FWares[0] := TWheat .Create;
    end;
    
    constructor TFarming.Create;
    begin
    // The ware we want to associate with the industry.
    self.FWare := self.FEconomy.FSquare.FProvince.FEconomy.FWares[0] as TWheat;
    end;
    But it doesn't work. FWares[0] seems to be empty. So I don't know if I am using the right way. Could you help me ?
    he software said "Requires Windows98, Win2000, or better." So I installed Linux.

  2. #2

    Reference between different units

    Uh... you forgot many important things like: end., implementation, interface, type.

    Do you know how to write a unit? I'm asking because if what you gave is a snippet, you could at least mark where you snip so we know it's a real unit.

    Firstoff, this isn't a managed language, so you do have to reference other units. Like so:
    Code:
    unit unit1;
    
    interface
    
    uses unit2;
    
    implementation
    
    end.
    Code:
    unit unit2;
    
    interface
    
    uses unit1;
    
    implementation
    
    end.
    Otherwise this'd be a problem for you.
    Code:
    // Secteur d'activit?©
    TActivitySector = class
    FName : string;
    // The ware produced by the industry
    TWare &#58; TWare; // <-- You assign to FWare, not TWare. ;&#41;
    constructor Create;
    end;

  3. #3

    Reference between different units

    Quote Originally Posted by Robert Kosek
    Firstoff, this isn't a managed language
    I don't entirely see what that has to do with this issue?!?

  4. #4

    Reference between different units

    Quote Originally Posted by Robert Kosek
    Firstoff, this isn't a managed language, so you do have to reference other units.
    Code:
    unit unit1;
    
    interface
    
    uses unit2; // <------------------------------------------------
    
    implementation
    
    end.
    Code:
    unit unit2;
    
    interface
    
    uses unit1; // <------------------------------------------------
    
    implementation
    
    end.
    In a managed language anything in the same assembly is referenced automatically, thus no need for external references or inclusions because it's internal. You don't have this. You must manually reference EVERY UNIT you change, reference, or need FOR EVERY UNIT that you write.

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

    Reference between different units

    If it compiled the references are there. Its just been trimmed for the sake of being brief.

    However too much has been trimmed to see where the problem could be. In the classes listed I cannot see where TFarming inherits a TEconomy from.

    I would suggest making a zip file available somewhere so that people could download it and try and trace the problem.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

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
  •