Results 1 to 3 of 3

Thread: Trubble with arrays + classes

  1. #1

    Trubble with arrays + classes

    Im creating a tile based game, and have created a map class.
    The map class contains an array which holds the tile information and some other stuff.

    Map class:

    [pascal]
    type
    TDPMap = class (TObject)
    StartX: integer;
    StartY: integer;
    Map: array[0..MapHeight-1,0..MapWidth-1] of integer;
    MapCollision: array[0..MapHeight-1,0..MapWidth-1] of integer;
    procedure LoadMap(Map: string; MapClass: array of TDPMap);
    procedure SaveMap(MapName: string; MapClass: array of TDPMap);
    end;
    [/pascal]

    When executing the below code, I get an access violation
    (Temp is a local integer)

    [pascal]
    Temp:=Map.map[Y,X];
    [/pascal]

    So my question is: whats wrong? can't you access arrays within a class like you would with a "ordinary" array?
    BK - we.create (tm)

  2. #2

    Trubble with arrays + classes

    First of, just a general tip: Even though you can omit the "private, public, published" you shouldn't for greater readabiltiy and to make sure that things don't get mixed up.

    Secondly, if you get an Access Violation that most likely means that you haven't created your TDPMap object. Even though you don't need to have your own constructor, you'll have to call the inherited one.

    So do this
    Map := TDPMap.Create;
    and to free it call
    Map.Free;


    Although this has nothing to do with your problem, I would recommend that you turn all variables in your class into properties. To create an array property do this

    private
    FMyArray: array[1..MapWidth - 1, 1..MapHeight - 1] of Integer;
    public
    property MyArray[X, Y: Integer]: Integer read GetValue write SetValue;

    in GetValue do

    Result := FMyArray[X, Y]

    and in SetValue do

    FMyArray[X, Y] := Value;

    You don't have to do this, but it's always good to follow the standards.
    Ask me about the xcess game development kit

  3. #3

    Trubble with arrays + classes

    Thanks for the quick reply!

    It works just perfect now, I have also "converted" the variables to properties like you suggested me to.

    Thanks again!
    BK - we.create (tm)

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
  •