This time I'm trying to make my game save high scores to disk.
I have tried a few combinations without result. Currently, my code does this :
OnLoaded
- FileAction Read "SaveFile"
File - "SaveFile"
- OnRead
- FileMoveData HighScore.Value
- OnWrite
- FileMoveData HighScore.Value
AppState GameOver
- FileAction Write "SaveFile"
SaveFile is encoded to char and saves to SaveFile.dat. Nothing else is set.
I tried looking at the ReadFile-example as well as some old examples I found on the forum, but all concerned reading a already filled data file.
I tried adding an array to SaveFile and a ZExpression reading values from it, such as :
SaveArray[0] = HighScore.Value; but that didn't help either. SaveFile.dat is 10 bytes big though, so something must have happened.
As always, help is appreciated!
Writing and Reading
Moderator: Moderators
Re: Writing and Reading
Hi Imerion,
*Ville, can you either add support for integers .. or at least change the error message from "Must be float property" into "Must be float or byte property" or something.
So, attached is a simple example that uses the TargetArray property ( instead of FileMoveData ). To properly test it, build a standalone executable, run it, press spacebar a couple of times, close it and start it again.
K
Not exactly sure what you're doing wrong, but i can imagine that it is confusing as the File component is basically two components in one ( the old File component + the newer TargetArray functionality ).Imerion wrote:This time I'm trying to make my game save high scores to disk. I have tried a few combinations without result.
Keep in mind that a char variable is only 8-bit, so you can only use it for values from 0 to 255. Also, the FileMoveData component only supports bytes and floats, while you probably want to use a integer ( highest value is 2147483647 )* Fortunately the TargetArray functionality does support integers.Imerion wrote:SaveFile is encoded to char and saves to SaveFile.dat.
*Ville, can you either add support for integers .. or at least change the error message from "Must be float property" into "Must be float or byte property" or something.
So, attached is a simple example that uses the TargetArray property ( instead of FileMoveData ). To properly test it, build a standalone executable, run it, press spacebar a couple of times, close it and start it again.
K
- Attachments
-
- Score.zgeproj
- (4.27 KiB) Downloaded 670 times
Re: Writing and Reading
Hi gents,
Sorry to revive a closed topic. How exactly does the File Read/Write function work?
I am trying a very simple exercise of placing a numerical figure in a txt file, say 10.
Then I just need to get this file read and the number be displayed on screen.
And then, on a second iteration, to increase 10 to say, 15. and then save this in the text file, over the initial figure.
What would the process or steps be like?
Thanks!
Zakk
Sorry to revive a closed topic. How exactly does the File Read/Write function work?
I am trying a very simple exercise of placing a numerical figure in a txt file, say 10.
Then I just need to get this file read and the number be displayed on screen.
And then, on a second iteration, to increase 10 to say, 15. and then save this in the text file, over the initial figure.
What would the process or steps be like?
Thanks!
Zakk
Re: Writing and Reading
Hi Zakk,
K
Since there's no way to write a string to a file i'd recommend using a byte array instead ( and do the string conversion yourself ).
Below is a simple example. I'm not sure what you want to happen when the ( number.txt ) file isn't found, so right now it displays the text "ERROR?" and keeps the internal number value as-is ( 0 in a standalone ), so in that case it will create number.txt containing "5" once you close the application.
Code: Select all
<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
<OnLoaded>
<FileAction Comment="Read number.txt" File="NumberFile"/>
<ZExpression>
<Expression>
<![CDATA[// If file contains data, construct string from data & convert to integer
if(NumberFile.Size)
{
string s = "";
for(int i=0; i<NumberBuffer.SizeDim1; i++)
{
s += chr(NumberBuffer[i]);
}
Number = strToInt(s);
NumberText.Text = s;
}
else // Not sure what you want to do when there's no file
{
NumberText.Text = "ERROR?";
}]]>
</Expression>
</ZExpression>
</OnLoaded>
<OnRender>
<RenderText Name="NumberText"/>
</OnRender>
<OnClose>
<ZExpression>
<Expression>
<![CDATA[// Add 5 to number before saving
Number += 5;
// Convert integer to string and fill buffer
string s = intToStr(Number);
int l = length(s);
NumberBuffer.SizeDim1 = l;
for(int i=0; i<l; i++)
{
NumberBuffer[i] = ord(subStr(s, i, 1));
}]]>
</Expression>
</ZExpression>
<FileAction Comment="Write number.txt" File="NumberFile" Action="1"/>
</OnClose>
<Content>
<Variable Name="Number" Type="1"/>
<Array Name="NumberBuffer" Type="4" SizeDim1="2"/>
<File Name="NumberFile" FileName="number.txt" TargetArray="NumberBuffer"/>
</Content>
</ZApplication>