Writing and Reading

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
Imerion
Posts: 197
Joined: Sun Feb 09, 2014 4:42 pm

Writing and Reading

Post by Imerion » Sun Apr 06, 2014 3:36 pm

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! :)

User avatar
Kjell
Posts: 1669
Joined: Sat Feb 23, 2008 11:15 pm

Re: Writing and Reading

Post by Kjell » Sun Apr 06, 2014 6:10 pm

Hi Imerion,
Imerion wrote:This time I'm trying to make my game save high scores to disk. I have tried a few combinations without result.
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:SaveFile is encoded to char and saves to SaveFile.dat.
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.

*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 133 times

Imerion
Posts: 197
Joined: Sun Feb 09, 2014 4:42 pm

Post by Imerion » Sun Apr 06, 2014 8:20 pm

Now I got it to work! Great example, thanks again! :D

Post Reply