Please help with Write/Read String error

BlitzMax Forums/BlitzMax Programming/Please help with Write/Read String error

Cruis.In(Posted 2013) [#1]
Hi everyone, I've run into an error late down in my reading/writing strings to a file.

First I Lset it:
ship.ShipClass = LSet(Ship.ShipClass, 20)

Then Write it:
WriteString save2, Ship.ShipClass 'ship class

'restore it
PlayerObject.ShipClass = Trim(save2.ReadString(20))

Was going fine now I've come into this issue where although its saved/restored fine and even reading back checking it using print or a drawtext, it isn't really reading correct

I know it is case sensitive.

I know how I wrote it in and what it is saving. It is saving "Heavy Cruiser"
Because shipclass is equal to heavy cruiser.

I print shipclass and it says "Heavy Cruiser"

Yet if I try an if statement after restoring it doesn't read. If shipclass = "Heavy Cruiser" then xxx.

What is happening is that when I save, I know the shipclass is gone bonkers and it isn't being read as it displays because other ship functions associated with being a heavy cruiser stopped working. When I load, it's working again. So it saved fine to the file, it's just that when I save it real time, the player's object typically is the one that is being updated and his/her current variables, in this case his shipclass.

Any ideas what could be going wrong? I narrowed it down after many hours to shipclass somehow doing something wonky when I save, but it continues on and when I load that save, its fine.

Any help would be sincerely appreciated,

Justin


Cruis.In(Posted 2013) [#2]
hi guys, I browsed the forum a bit, and found some people suggesting to use writeline...

I used that and it fixed it immediately, no more issue with restoring the ship class.

But I am not happy with this, as I use writestring throughout, I could change it I suppose, I just want to know what caused the error with write string and if it is possible to work with it, because I have to be doing something wrong with write string.

That is the only string I use which has a space in the middle "Heavy Cruiser" is that an issue?

Also it would help in case I get other issues with write string I'll know why, but so far I've never encountered this. Also after so many hours narrowing it down and checking and re-checking I was positive that the save file was good and the save/restore was in good working order and I was not making any mistakes there.

Which I why I concluded that the mistake had to be how I was using writestring with SHIPCLASS.

But sorry for the winded explanation, I said all that to say that I cannot understand what I have done wrong with writestring and shipclass.


col(Posted 2013) [#3]
Could it be the same scenario as this which is related to this post?


Brucey(Posted 2013) [#4]
You haven't really provided enough example to see where you might be going wrong.
Based on what you've said, one has to assume that your loader is not understanding where the end of a String is, and is perhaps reading in more data after that than it should be, but I can't replicate it myself...

This seems to work fine, as I would expect, with a mix of strings and numbers :



Derron(Posted 2013) [#5]
In this case it might be easier to use Bruceys TPersistence module for easy "save load".


bye
Ron


Cruis.In(Posted 2013) [#6]
Hi guys, I've found it does it with all strings, the reason I never encountered it before when testing was because, I would save, then load, to make sure everything had saved/loaded properly.

What I have found is that if I save, and continue on, even though the string adjusted, the game isn't reading it because of the Lset.

That is where the specific problem was.

So once I saved "Heavy Cruiser" with an LSET of 20, it adds 20 spaces after right?

So when the game is checking to see if shipclass is = to "Heavy Cruiser" or any other string, it can't match because right after the save, "Heavy Cruiser" is going to look like this "Heavy Cruiser "

Hence why it would work fine when I loaded, because it was being 'trimmed' back down from "Heavy Cruiser " to "Heavy Cruiser"

So even though the string looked proper in game it had many spaces after it, directly after I saved, and the game trying to read it would not work.

Using writeline instead of writestring solves this issue. No more Lset.

Is writeline cross platform? In the sense that I see the command makes a reference to windows.


Brucey(Posted 2013) [#7]
So even though the string looked proper in game it had many spaces after it

Indeed, and if you look at my example it is only padding during the save, not on the actual data in the type itself.

You don't need to use writeline at all. As my example shows, you can use WriteString without issue. Your problem was related to you changing the original strings - which generally you shouldn't do unless you have a better reason to.


Cruis.In(Posted 2013) [#8]
hi Brucey thanks, now that I have switched to writeline already is that an issue other than what I have done to replace the writestrings?