Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Help!? Classes, Sprites and files

  1. #1

    Help!? Classes, Sprites and files

    Hello,
    i am not sure that my problem is for here but as they say you'll never know inless you ask so here i go:

    i am creating a game turn based strategy to be exact and i am almost done the one last thing that i need to do is create some different map than a random generated squares so i started creating a map editor.
    In the map editor i have a

    [pascal]
    TTile = class(TImageSprite)
    private
    public
    TileSize: integer;
    TilePicture: TBitmap;
    TileType:integer;
    TilePos : record
    X : integer;
    Y : integer;
    end;
    TileExist:Boolean;[/pascal]
    type and a


    [pascal] Tile: array [1..5000] of TTile;[/pascal]

    var so i create the map and then save it using the procedure

    [pascal]procedure SaveTheTiles;
    var f:file of TTile;i:integer;
    begin
    assignfile(f,'tmp.map');
    rewrite(f);

    for i:=1 to 5000 do
    if (Tile[i] <> Nil) then
    write(f,Tile[i]);

    CloseFile(f);

    end;[/pascal]

    i know its not the best way to do it but lets leave that behind for now.
    So anyway i save the map and then try to load it in the game

    [pascal]procedure TMap.Load;
    var f:file of TTile;
    begin
    assignfile(f,'tmp.map');
    reset(f);

    while not eof(f) do begin
    numberunits.tiles :=numberunits.tiles+1; // this is defined as global var and also the starting value is 0
    Tile[numberunits.tiles]:=TTile.Create(form1.dxspriteengine1.Engine);
    read(f,Tile[numberunits.tiles]);
    end;
    closefile(f);
    end;[/pascal]

    so far so good but when i try to access some value in the array of Tiles
    example:
    showmessage(inttostr(tile[1].tilesize));
    i get an error :

    project1.exe raised an exception class bla bla access violation at address 004667A0 bla bla and so on....

    i dont get it why do i get an error ??!?!?!?!?!?!!??
    if someone can help PLEASEEEEEEEE help me
    thanks[/pascal]

  2. #2

    Help!? Classes, Sprites and files

    That won't work because when you do
    [pascal]write(f,Tile[i]);[/pascal]
    it is just writing a pointer to the class into the file.
    So, when you load it you are loading pointers which (probably) don't point to anything anymore, hence the access violation.

    You will need to create a method to save the values in the tile class to a file. Unfortunately, this will not be easy since TImageSprite doesn't have any method for saving either, so you would have to save that data also. When you want to load the map you will then have to create new tiles and read in the saved values to them.
    [size=10px][ Join us in #pgd on irc.freenode.net ] [ Sign the Petition for a Software Patent Free Europe ][/size]

  3. #3

    Help!? Classes, Sprites and files

    Hey, I have a map editor that I made in delphiX a while ago if you would like to take a look at the source. If you do, let me know and I will email it to you.

    Thanks

    Bobby
    Bobby Baker
    <br />Delphi Sanctuary
    <br />www.delphisanctuary.com

  4. #4

    Help!? Classes, Sprites and files

    could you e-mail me this editor, becouse i'm creating game too and have some problems with it.
    http://www.programuotojas.com

  5. #5

    Help!? Classes, Sprites and files

    Useless Hacker is correct, you'll need to save and load the properties of the sprite and not the sprite itself. Write(f, Tile[i].Whatever); Write(f, Tile[i].Whatever2); etc.

    You'll also want to Free the tiles in your loading code before you use TTile.Create() on them. Otherwise the tiles that you had before will still exist in memory but with nothing to point at them.
    Signature:
    <br />This is a block of text that can be added to posts you make. There is a 255 character limit

  6. #6

    Help!? Classes, Sprites and files

    this is damn weird
    cous when i've generated the map inside the main program it have no problem but if i use a program then save it, and then load the map in the main program it crashes, but if i recall it was no problem using the write(f,record_type) atleast in pascal worked ?!??!?!?!

  7. #7

    Help!? Classes, Sprites and files

    at Ultra:
    i cant use Write(f, Tile[i].Whatever2);
    cous the file is of type TTile i.e. Tile[x] so i cant save them this way so i guess i will have to save them in a text file or if its supported via a stream but then Useless Hacker told that this method is not implented, and the text file method is quite slow and hard to create so if anyone have any idea how can i make it easy or atleast faster i will be very happy to be told

  8. #8

    Help!? Classes, Sprites and files

    Also you're gonna have problems with the tile picture part of your class. By giving each individual tile it's own TBitmap you are gonna use a heck of a lot of memory for bitmaps for each indivudual tile. 5000 bitmaps takes up a of a lot of memory. Let's say each of these tiles is 32*32 with a depth of 16. 32*32*16*5000 is a whole lot of memory. It would be much better to have make the TilePicture reference another array of images that is used to store ther images for tiles. Then you would only have one bitmap for each different image as opposed to one bitmap for each individual tile.

    TilePicture : ^TBitmap;

    Tiles[X].TilePicture:=@TileImages[Y];
    Isometric game development blog http://isoenginedev.blogspot.com/

  9. #9

    Help!? Classes, Sprites and files

    Quote Originally Posted by hammer
    ... if i recall it was no problem using the write(f,record_type) atleast in pascal worked ?!??!?!?!
    Yes, you can write record types like that (unless the record contains pointers,) but in Delphi a class is in fact a pointer to the actual class. When you write Thing := TThing.Create; Delphi allocates memory for the new TThing object and stores a pointer to it in Thing.

    The TBitmap member isn't necessary, because TImageSprite already stores a TPictureCollectionItem reference.
    [size=10px][ Join us in #pgd on irc.freenode.net ] [ Sign the Petition for a Software Patent Free Europe ][/size]

  10. #10

    Help!? Classes, Sprites and files

    As so many have said before classes are pointers so if your file is of type TTile it's the same as saying f: File of Pointer; and it just won't work. Instead you can just use f: File;

    A small (untested!) way of saving a tile map would be:

    [pascal]

    type
    TTile = class
    public
    CanWalkOn: Boolean;
    Image: Integer;
    procedure SaveToFile(var f: File);
    procedure LoadFromFile(var f: File);
    end;

    TTileMap = class
    public
    Tiles: array[0..9, 0..9] of TTile;
    procedure SaveToFile(const FileName: String);
    procedure LoadFromFile(const FileName: String);
    procedure Clear;
    end;

    {...}

    procedure TTile.SaveToFile(var f: File);
    begin
    try
    BlockWrite(f, CanWalkOn, SizeOf(CanWalkOn));
    BlockWrite(f, Image, SizeOf(Image));
    except
    // something went wrong
    end;
    end;

    procedure TTile.LoadFromFile(var f: File);
    begin
    try
    BlockRead(f, CanWalkOn, SizeOf(CanWalkOn));
    BlockRead(f, Image, SizeOf(Image));
    except
    // something went wrong
    end;
    end;

    procedure TTileMap.SaveToFile(const FileName: String);
    var
    f: File;
    x,y: Integer;
    begin
    AssignFile(f, FileName);
    try
    Rewrite(f);
    for y := 0 to 9 do
    for x := 0 to 9 do
    begin
    Tiles[x,y].SaveToFile(f);
    end;
    finally
    CloseFile(f);
    end;
    end;

    procedure TTileMap.Clear;
    var
    x,y: Integer;
    begin
    for x := 0 to 9 do
    for y := 0 to 9 do
    begin
    if Assigned(Tiles[x,y]) then
    begin
    Tiles[x,y].Free;
    Tiles[x,y] := nil;
    end;
    end;
    end;

    procedure TTileMap.LoadFromFile(const FileName: String);
    var
    f: File;
    x,y: Integer;
    begin
    Clear;
    AssignFile(f, FileName);
    try
    Reset(f);
    for y := 0 to 9 do
    for x := 0 to 9 do
    begin
    Tiles[x,y] := TTile.Create;
    Tiles[x,y].LoadFromFile(f);
    end;
    finally
    CloseFile(f);
    end;
    end;

    [/pascal]
    Signature:
    <br />This is a block of text that can be added to posts you make. There is a 255 character limit

Page 1 of 2 12 LastLast

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
  •