SRL Archive - OSR Help

Creating and using ini files


Bad Boy JH

2011-03-06 00:27:37

I want to use ini files to save player data, however I can't find a decent tutorial on the subject.

if I wanted to save multiple characters with 4 data types (Name, Pass, Nick, Pin), how would I do it…

Sex

2011-03-06 00:59:18


[B]WriteINI[/B]

procedure WriteINI(const Section, KeyName, NewString, FileName: string);



[B]ReadINI[/B]

function ReadINI(const Section, KeyName, FileName: string): string;



[B]DeleteINI[/B]

procedure DeleteINI(const Section, KeyName, FileName: string);
Here is an example ini:
From: filename.ini

[section]
keyname=kyle

To read the key you would do ReadKey := ReadINI('section', 'keyname', AppPath + Filename);
That would return 'kyle'.
Write and read should be self explanatory I hope.

Bonfield

2011-03-06 00:59:31

here is how i increased my stats
  
Inc(Script_Total[TOTAL_HIGH_ALCHES]);
WriteINI('Script_Total', '1', IntToStr(Script_Total[TOTAL_HIGH_ALCHES]), ScriptPath + 'ScriptStats.INI');
IncEx(Script_Total[TOTAL_XP_GAINED], 65);
WriteINI('Script_Total', '3', IntToStr(Script_Total[TOTAL_XP_GAINED]), ScriptPath + 'ScriptStats.INI');

i read them like this


procedure Script_Totals;
var
i : Integer;
begin
for i := 0 to 4 do
Script_Total[i] := StrToIntDef(ReadINI('Script_Total', IntToStr(i), ScriptPath + 'ScriptStats.INI'), 0);

Writeln(' Total Time Ran : ' + MsToTime(Script_Total[TOTAL_TIME_RAN], Time_Formal));
Writeln(' Total High Alches : ' + IntToStr(Script_Total[TOTAL_HIGH_ALCHES]));
Writeln(' Total Curses : ' + IntToStr(Script_Total[TOTAL_CURSES]));
Writeln(' Total XP Gained : ' + IntToStr(Script_Total[TOTAL_XP_GAINED]));
Writeln(' Script Runs : ' + IntToStr(Script_Total[TOTAL_STARTS]));
end;

for each player just do a for to do loop and write each players name, pin, etc

i didnt really explain it to well but i hope you get the idea

Bad Boy JH

2011-03-06 01:24:46

OK thanks guys, just a quick question on forms, can I open a second form (ie like load players form) and if you can, how, because CreateForm doesn't seem to be working…

Sex

2011-03-06 01:25:10

You can but iirc it can't be modal?

Edit: Idk what I'm talking about ? I done it before lon ago, search the forum :p.

Bad Boy JH

2011-03-06 01:41:50

I forgot to call ShowModal, but that freezed everything
so I just called Show, but that stuffed it up, because the function that loaded it kept working. Which means I can't load the player, any ideas?

In the mean time i may look into some other scripts with a form and load player.

Sex

2011-03-06 01:47:30

Show me what you put I'll do it.

Bad Boy JH

2011-03-06 03:43:50

Got a method that makes it work, but when i free the sub-form it causes the error message "access violation"

Flight

2011-03-06 03:47:06

It's like your own personal SRL stats. :p How original and nice idea!

Bonfield

2011-03-06 04:52:17

I think he just wants to save/load player details to an ini I'm guessing so a form will load all the info, or load players that way save re-writing declareplayers each time

The example i posted kept track of players stats in a script I made

Sex

2011-03-06 15:13:58

Program FormTest;

var
frmDesign : TForm;
frmDesign2 : TForm;
OpenNewForm : TButton;
Label1 : TLabel;

Procedure ButtonClick(Sender: TObject); Forward;

Procedure InitForm;
Begin
frmDesign := TForm.Create(nil);
frmDesign.Left := 250;
frmDesign.Top := 114;
frmDesign.Width := 258;
frmDesign.Height := 118;
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
OpenNewForm := TButton.Create(frmDesign);
OpenNewForm.Parent := frmDesign;
OpenNewForm.Left := 88;
OpenNewForm.Top := 32;
OpenNewForm.Width := 89;
OpenNewForm.Height := 25;
OpenNewForm.Caption := 'Open New Form';
OpenNewForm.TabOrder := 8;
OpenNewForm.OnClick := @ButtonClick;
End;

procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('InitForm', v);
end;

procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('ShowFormModal', v);
end;

Procedure InitForm2;
Begin
frmDesign2 := TForm.Create(nil);
frmDesign2.Left := 288;
frmDesign2.Top := 151;
frmDesign2.Width := 172;
frmDesign2.Height := 61;
frmDesign2.Caption := 'frmDesign';
frmDesign2.Color := clBtnFace;
frmDesign2.Font.Color := clWindowText;
frmDesign2.Font.Height := -11;
frmDesign2.Font.Name := 'MS Sans Serif';
frmDesign2.Font.Style := [];
frmDesign2.Visible := False;
frmDesign2.PixelsPerInch := 96;
Label1 := TLabel.Create(frmDesign);
Label1.Parent := frmDesign2;
Label1.Left := 48;
Label1.Top := 8;
Label1.Width := 72;
Label1.Height := 13;
Label1.Caption := 'Yay New Form!';
End;

procedure SafeInitForm2;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('InitForm2', v);
end;

procedure ShowFormModal2;
begin
frmDesign2.ShowModal;
end;

procedure SafeShowFormModal2;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('ShowFormModal2', v);
end;

Procedure ButtonClick(Sender: TObject);
Begin
Case Sender Of
OpenNewForm :
Begin
SafeInitForm2;
SafeShowFormModal2;
End;
End;
End;

Begin
SafeInitForm;
SafeShowFormModal;
End.
I found that here.