PDA

View Full Version : A Map Editor Question



mongoixp
05-02-2011, 01:01 PM
Hello PGD users.

When I Has making my Map Editor i encoutred a really strange error on my MapEd the map can't be opened
It appears a message showing : '' is not a valid integer value can anyone help me with this?


Here is the code for opening and Saving and The program


Opening:

procedure TForm1.Button5Click(Sender: TObject);
var
Arquivo : TextFile;
X, Y : Integer;
Temp: String;
begin
if OpenDialog1.Execute then
begin
AssignFile(Arquivo,OpenDialog1.FileName);
Reset(Arquivo);
for X:= 0 to 9 do
begin
ReadLn(Arquivo, Temp);
for Y := 0 to 9 do
Area[Y, X] := StrToInt(Copy(Temp, Y + 1, 1))
end;
CloseFile(Arquivo);
DrawGrid1.Refresh;
end;
end;For Saving

procedure TForm1.Button4Click(Sender: TObject);
var
Arquivo : TextFile;
X,Y : Integer;
S : string;
begin
if SaveDialog1.Execute then
begin
AssignFile(Arquivo,SaveDialog1.FileName);
Rewrite(Arquivo);
for X:= 0 to 9 do
begin
for Y:= 0 to 9 do
S:= S + IntToStr(Area[Y,X]);
WriteLn(Arquivo,S);
S:='';
end;
CloseFile(Arquivo);
end;
end;Can anyone help me with this ?
THX

mongoixp

AthenaOfDelphi
05-02-2011, 10:46 PM
Hi mongoixp,

The problem is, I believe, the fact that you aren't marking the end of one integer in your string, so StrToInt is interpreting the whole string as one big integer which will result in an error.

What you could do is this:-



var
f : file of integer;
x,y : integer;
begin
assignFile(f,saveDialog1.filename);
rewrite(f);
for x:=0 to 9 do
begin
for y:=0 to 9 do
begin
write(f,area[x,y]);
end;
end;
closefile(f);
end;


Of course this does mean the file can only store integers... may not be what you want. So, you could look at introducing some formatting to your string based file...

If the values in the area array are only going to be 16 bit values, you could do this:-



For writing:-

for x:=0 to 9 do
begin
s:='';
for y:=0 to 9 do
begin
s:=s+intToHex(area[x,y],4);
end;
writeln(arquivo,s);
end;

And then for reading:-

for x:=0 to 9 do
begin
readln(arquivo,s);
for y:=0 to 9 do
begin
area[x,y]:=intToStr('$'+copy(s,y*4,4));
end;
end;


And if you want to store a variety of information, you could use streams... a good starting point would be my article on versioned data storage as it presents streams and the concepts of versioning your data store, plus it has a fairly good example of using it as a map store (it's how I store my maps). The article can be found here (http://www.pascalgamedevelopment.com/content.php?117-Tripping-The-Class-Fantastic-Versioned-Data-Storage).

Hope this helps.

AthenaOfDelphi
05-02-2011, 10:53 PM
Ignore my comment about what I think the problem is... I just re-read your code and now, I'm not sure :)

Actually, scratch that too... just noticed something...



for X:= 0 to 9 do
begin
for Y:= 0 to 9 do
S:= S + IntToStr(Area[Y,X]);
WriteLn(Arquivo,S);
S:='';
end;


The line S:=''; should really be before the 'y' for loop. As it is, S could contain anything. Now I'm not sure how this would cause your problem unless you were unlucky and say got an end of file marker or something like that in there.

The other thing I mentioned in my original reply was about the lengths of the strings being written out. What happens if one the values you're writing is bigger than 9? At the moment, it appears that this code won't work properly.

Anyhow, if I can be of any further confusion, please don't hesitate to ask :D

mongoixp
12-02-2011, 09:19 PM
HI, Now i know why pop up the error message " '' is not a valid integer value" in my original source code i put a wrong nunber in the lines of

for x:=0 to 29
and
y:=0 to 29My array is 0 to 9

Anyway thanks AthenaofDelphi for aswering my post.