Redefining variable types

BlitzMax Forums/BlitzMax Programming/Redefining variable types

Retimer(Posted 2008) [#1]
Possible?

In one of my projects, the way a structure is saved may change in updated versions, so I may require that a file be loaded differently depending on the format.

What i'm looking to do, is something like this:


Type tone
	Field name:Int
End Type

Type ttwo
	Field name:Int
	Field count:Int
End Type

Type tthree
	Field name:Int
	Field info:String
End Type


If Format = format_One
	Global hah:tone 
ElseIf Format = format_Two
	Global hah:ttwo
Else
	Global hah:tthree
End If


There's obviously many problems with that example, but I figured it was the best to explain what i'm trying to do. Is there any way to redefine a variable during runtime?

Thanks


Scaremonger(Posted 2008) [#2]
There are various ways you could approach this.

1) Try to create a generic save file structure at the beginning.
2) Have an identifier in your save file that identifies the file version, and have a loader for each version the application supports (you can drop old file versions after a couple of revisions).
3) Load the file and default missing values.
4) A combination of the above.

If you think your datatypes may change, you could always save them as a string, and typecast them at load time for example:
Local data$ = "1.6"

Local nInt% = Int(data)
Local sStr$ = String(data) 
Local nFlt# = Float(data) 

Print "Integer: " +nInt
Print "String: " +sStr
Print "FLoat: " +nFlt


A simple "extends" might be suitable too:
type TTwo extends Tone
field x$
end type

global hah:Tone



Retimer(Posted 2008) [#3]
Yeah, i'm ahead with #2. The problem is that the structure is saved into a stream, so any minor changes (which there will be very large changes at some points) cause a pretty big fuss with the difference in stream-reads.

I never thought of extends though, i'll sledge hammer my head on that one for a bit.

If that doesn't work, I i'll just have to create a converter and make new releases every time I update, and keep the 'structured' files server-side.

Thanks for the advice.


Dreamora(Posted 2008) [#4]
You might as well just write stream wrappers for the different versions.

So if the stuff written changes, give the file a different version number (written as an int as first into the file), you create a new stream wrapper for that version.

that allows you to keep them side a side.

That store blocks that are "blackboxes" to the reading mechanism and are parsed by a different class later.
Those blocks have their bytesize written first (in short or int) and then the data ... that way you can create a file of blackboxes that is immune to changes in the reading process.

Most likely simpler because the changes normally would be doen to the end of the writes of the old written data, so the reader could look if version >= needed version and then continue parsing or just set default data if < needed version.


Retimer(Posted 2008) [#5]
Most of the changes will be at the end, although there might be removed features, or altered types.

You've given me the idea to setup the wrappers so that they could automatically convert older versions to new even clientside, then to have a single (frequently updated) method of reading it all, rather than 20 different methods over time. Perfect

I really appreciate the help as I don't believe this is a popular issue/request amongst the Blitz community lol.

resolved, cheers :-)