Results 1 to 5 of 5

Thread: Cannot load ini-files in the game

  1. #1

    Cannot load ini-files in the game

    Hi all!

    I'm making a space shooter game. In the game I want to have a simple form of dialog. This is simply a text that shows after x frames have passed and it shows for y frames.

    [pascal]
    type
    TDialog = record
    Name: String; // identifier used by the editor
    StartFrame, NumOfFrames: Integer;
    Text1, Text2, Text3: String; // text to display
    end;
    TDialogs = array of TDialog;
    [/pascal]

    To store this information I've decided to use ini files (I may use a TStream later, but during development ini files are more flexible). The save and load procedures look like this:

    [pascal]
    procedure SaveDialogToIni(var Dialog: TDialogs; const FileName: String);
    var
    Ini: TIniFile;
    i: Integer;
    begin
    Ini := TIniFile.Create(FileName);
    try
    Ini.WriteInteger('General', 'NumOfDlg', Length(Dialog));
    for i := 0 to High(Dialog) do
    begin
    Ini.WriteString ('Dialog'+IntToStr(i+1), 'Name' , Dialog[i].Name );
    Ini.WriteInteger('Dialog'+IntToStr(i+1), 'StartFrame' , Dialog[i].StartFrame );
    Ini.WriteInteger('Dialog'+IntToStr(i+1), 'NumOfFrames', Dialog[i].NumOfFrames);
    Ini.WriteString ('Dialog'+IntToStr(i+1), 'Text1' , Dialog[i].Text1 );
    Ini.WriteString ('Dialog'+IntToStr(i+1), 'Text2' , Dialog[i].Text2 );
    Ini.WriteString ('Dialog'+IntToStr(i+1), 'Text3' , Dialog[i].Text3 );
    end;
    finally
    Ini.Free;
    end;
    end;

    procedure LoadDialogFromIni(var Dialog: TDialogs; const FileName: String);
    var
    Ini: TIniFile;
    i, len: Integer;
    begin
    Dialog := nil;

    Ini := TIniFile.Create(FileName);
    try
    len := Ini.ReadInteger('General', 'NumOfDlg', 0);
    SetLength(Dialog, len);
    for i := 0 to len - 1 do
    begin
    Dialog[i].Name := Ini.ReadString ('Dialog'+IntToStr(i+1), 'Name' , '');
    Dialog[i].StartFrame := Ini.ReadInteger('Dialog'+IntToStr(i+1), 'StartFrame' , 0 );
    Dialog[i].NumOfFrames := Ini.ReadInteger('Dialog'+IntToStr(i+1), 'NumOfFrames', 0 );
    Dialog[i].Text1 := Ini.ReadString ('Dialog'+IntToStr(i+1), 'Text1' , '');
    Dialog[i].Text2 := Ini.ReadString ('Dialog'+IntToStr(i+1), 'Text2' , '');
    Dialog[i].Text3 := Ini.ReadString ('Dialog'+IntToStr(i+1), 'Text3' , '');
    end;
    finally
    Ini.Free;
    end;
    end;
    [/pascal]

    I use the same dialog-unit in two programs (the game and the dialog editor). In the editor everything the following loads the info and everything is fine:
    [pascal]
    if OpenDialog1.Execute then
    begin
    ListBox1.Clear;

    LoadDialogFromIni(Dialog, OpenDialog1.FileName);

    for i := 0 to High(Dialog) do
    ListBox1.Items.Add(Dialog[i].Name);
    end;
    [/pascal]

    However in the game I use:
    [pascal]
    LoadDialogFromIni(Dialog, AName + '.dlg');
    [/pascal]
    and it doesn't load.

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

  2. #2

    Cannot load ini-files in the game

    I can't see anything major that is wrong. What exactly happens when you call the load function in the game?

    At the begining of the load function you have Dialog := nil; which I don't think you need.
    [size=10px][ Join us in #pgd on irc.freenode.net ] [ Sign the Petition for a Software Patent Free Europe ][/size]

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

    Cannot load ini-files in the game

    Setting Dialog to Nil is actually really bad

    In a var section if you define a variable to be a record it allocates the memory for the record equal tot he size of the record, (unlike objects in which only space for a pointer is created which is then allocated when you create the object - the pointer then points to the object somewhere).

    By setting the record to Nil you are leaving all the memory currently used by the record still in memory with nothing indicating to the program where it is.

    Then the record doesn't have any memory and you are trying to put something into it - think of it like trying to pour water into a non exstant glass - the water is going to go somewhere you dont expect - so the values that you are allocating to Dialog ar not being put anywhere (except maybe over the OS memory segment) and you are probably getting access violations (though delphi isn't too good at picking up non pointer based access violations).

    Hope that all makes sense
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  4. #4

    Cannot load ini-files in the game

    Nothing happens when I call the function in the game. High(Dialog) return -1 so it's like the file didn't exist (or had no info in it). Anyway, FileExists returns true so it should be there.

    @cairnswm
    Yup, that makes sense.
    Signature:
    <br />This is a block of text that can be added to posts you make. There is a 255 character limit

  5. #5
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Cannot load ini-files in the game

    Just so you know...

    When you do this
    [pascal]Dialog := nil;[/pascal]
    ...this is how you would deallocate dynamically assigned memory blocks and objects aswell.

    ie. if you were to declare Dialog and use it like this
    [pascal]TDialog = class(TObject)
    a: Integer;
    b: String[12];
    end;

    ...

    var Dialog: TDialog;

    ...

    Dialog := TDialog.Create;

    ...[/pascal]

    You could then destroy the object by assigning 'Nil' to it.
    Jason McMillen
    Pascal Game Development
    Co-Founder





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
  •