Have a look here for how to write and read variable-length strings to and from streams.
Have a look here for how to write and read variable-length strings to and from streams.
[size=10px][ Join us in #pgd on irc.freenode.net ] [ Sign the Petition for a Software Patent Free Europe ][/size]
The other alternative is to use the XML Document and add each item with its property into the XML document.
This is quite a lot of work but makes it easy to edit by hand if you want to change something on the fly.
William Cairns
My Games: http://www.cairnsgames.co.za (Currently very inactive)
MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)
If it helps i have a class i use when writting my formats
[pascal]
uses
Classes;
type
TknStream = Class(TFileStream)
public
procedure WriteChar(const Value: Char);
procedure ReadChar(var Value: Char);
procedure WriteString(const Value: String);
procedure ReadString(var Value: String);
....
end;
....
procedure TknStream.WriteChar(const Value: Char);
begin
Write(Value, SizeOf(Char));
end;
procedure TknStream.ReadChar(var Value: Char);
begin
Read(Value, SizeOf(Char));
end;
procedure TknStream.WriteString(const Value: String);
var
i: Integer;
begin
For i := 1 To Length(Value) Do WriteChar(Value[i]);
WriteChar(#0);
end;
procedure TknStream.ReadString(var Value: String);
var
c: Char;
begin
Value := '';
Repeat
ReadChar(c);
If c <> #0 THen Value := Value+c;
Until c = #0;
end;
[/pascal]
More value types are easy to put in, just copy what is in the read/write char procedures, but instead of SizeOf(Char) change it to SizeOf(YourType).
Hope this helps.
M109uk
<br />--------------------------------------------------------
<br />www.pulse-soft.oneuk.com
Good point! In general I'd say binary format files are better suited for game data, but I think XML is a cool format.Originally Posted by cairnswm
If you want to save your data in XML format, you might want to check out my XML parser component (see "My Projects")
Ask me about the xcess game development kit
Wow guys, I'm kinda overwhelmed. I've coded a whole lot of my game based off my current structure. . I don't wanna go to XML because the point of doing it binary is to keep people from "hacking" the game. That class above, will work for my problem?
I have a 2005 CRF 250 so <^>(><<^>
<br />http://www.gtrpg.com/
The class M109uk posted is a nice little helper but it won't solve your problem directly. You still need to work around the variable-length record problem as explained in my posts...
Ask me about the xcess game development kit
I don't quiet understand that memorystream thing. And before I lose all hope, what way do you suggest I restructure the type so it'll save cleanly? And do Strings save? Because those can be variable length right? Do i have to change all : Strings into : Array[0..255] of Char ?
I have a 2005 CRF 250 so <^>(><<^>
<br />http://www.gtrpg.com/
Because I'm such a nice guy :roll: , I wrote a little example program that saves 101 string lists to a file and loads them again. Create a new app, add a button to your form and paste this code into your code window:
[pascal]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyFileHeader = record
Title: array[0..255] of Char;
SomeInteger: Integer;
NumElements: Integer;
end;
TMyElementInfo = record
Desc: array[0..255] of Char;
AnotherInteger: Integer;
Size: Int64;
end;
TMyElement = record
StringList: TStringList;
Blah: Integer;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Header: TMyFileHeader;
ElementInfo: array of TMyElementInfo;
Elements: array of TMyElement;
I, J: Integer;
Temp: TMemoryStream;
FileStream: TFileStream;
begin
// Fill the header
Header.Title := 'This is a test';
Header.SomeInteger := 42;
Header.NumElements := 101;
SetLength(Elements, 101);
SetLength(ElementInfo, 101);
// Fill the elements first
for I := 0 to 100 do
begin
Elements[I].StringList := TStringList.Create;
for J := 0 to 9 do
Elements[I].StringList.Add('BlahBlahBlah');
Elements[I].Blah := 12;
end;
// Fill the element info array
for I := 0 to 100 do
begin
ElementInfo[I].Desc := 'Element';
ElementInfo[I].AnotherInteger := 14;
Temp := TMemoryStream.Create;
Temp.Clear;
Temp.Seek(0, soFromBeginning);
Elements[I].StringList.SaveToStream(Temp);
ElementInfo[I].Size := Temp.Size;
Temp.Free;
end;
// Save
FileStream := TFileStream.Create('C:\TestTest.dat', fmCreate);
FileStream.Write(Header, SizeOf(Header));
for I := 0 to 100 do
FileStream.Write(ElementInfo[I], SizeOf(ElementInfo[I]));
for I := 0 to 100 do
begin
Elements[I].StringList.SaveToStream(FileStream);
FileStream.Write(Elements[I].Blah, SizeOf(Integer));
end;
FileStream.Free;
// Load
FileStream := TFileStream.Create('C:\TestTest.dat', fmOpenRead);
FileStream.Read(Header, SizeOf(Header));
SetLength(Elements, Header.NumElements);
SetLength(ElementInfo, Header.NumElements);
for I := 0 to Header.NumElements - 1 do
FileStream.Read(ElementInfo[I], SizeOf(TMyElementInfo));
for I := 0 to Header.NumElements - 1 do
begin
Temp := TMemoryStream.Create;
Temp.Clear;
Temp.Seek(0, soFromBeginning);
Temp.CopyFrom(FileStream, ElementInfo[I].Size);
Elements[I].StringList := TStringList.Create;
Elements[I].StringList.LoadFromStream(Temp);
Temp.Free;
FileStream.Read(Elements[I].Blah, SizeOf(Integer));
end;
FileStream.Free;
end;
end.
[/pascal]
The basic concept of this is to separate fixed-length and variable-length data carefully and store information about the size of the variable-length data either in the header or in a separate record.
Ask me about the xcess game development kit
Wow, your awsome. Right now im leaving for a bit but im gonna get home tonight and try and sort everything out in my game. I'll let you know how it works out. Thanks again! (btw. What projects are you actively working on? You're obviously a great coder and i'd like to see more)
I have a 2005 CRF 250 so <^>(><<^>
<br />http://www.gtrpg.com/
Ok i've had a chance to mess around with all that coding and concepts... One major problem however. File size. This way, the files are coming out to around 75KB... Thats horrible, concidering this is a MMORPG. The server has to send the map files to all the users every time they move out of the map to a new map. It looks like I may have to stick to my sloppy way of doing it
I have a 2005 CRF 250 so <^>(><<^>
<br />http://www.gtrpg.com/
Bookmarks