generic type assignment?

BlitzMax Forums/BlitzMax Beginners Area/generic type assignment?

Craig H. Nisbet(Posted 2007) [#1]
Anyone know if this is possible? I'm trying to write a load and save system for my types. I got the save part to work, but the loading is giving me some issues. My goal is to make the system complete generic so it will save any type you throw at it.

here's my save function...
Function saveType(instance:Object,file:TStream)
	Local id:TTypeId=TTypeId.ForObject(instance)
	Local fList:TList = id.EnumFields()
	WriteLine  file,"new " + id.name()
	For Local myField:TField = EachIn fList
		WriteLine file,myField.name() + " " + myField.getString(instance)
	Next
End Function


Here's my non working save function...
This is a string in my file that says "new agent". "agent" is the name of my type. I threw the "new" in there so it knew it was creating a new type and not assigning a field.

Function loadTypes(filename:String)
	Local file:TStream = ReadFile(filename)
	Local myLine:String
	myLine = ReadLine(file)
	Local myCom:String[] = myLine.split(" ")
	Select myCom[0]
		Case "new"
			Local id:TTypeId=TTypeId.ForName(myCom[1])
			Local myItem:Object = id.newObject()
	End Select
	CloseFile file
End Function


I'm probably not using the "Object" part incorrectly. Anyone know how to resolve this?


FlameDuck(Posted 2007) [#2]
Anyone know how to resolve this?
At the risk of repeating myself. Runtime Reflection. There is no other (practical) way to do it.


Derron(Posted 2007) [#3]
What if something is depending on another object?

like:
Type TMyParent
field MyChildren:TMyChildren
End Type

Type TMyChildren
End Type


You would have to save them in the right order ... which doesn't make it generic. If not paying attention to the right order, you would have to store parent-links within the children and so on.

If all objects are independent from all others, a generic save/load would be nice, but in most times you have some connections between which may be lost when not acting in special ways on them.


bye
MB


TaskMaster(Posted 2007) [#4]
You wouldn't have to save them in order. But, you would need to know which objects need other objects. If you load an object that needs another object, then if the second object hasn't been loaded yet, you just have to remember to create that connection after you do load the new object. It is possible, it would take quite a bit of work though.


Craig H. Nisbet(Posted 2007) [#5]
Yeah, I see where your going with this. Maybe a generic method is not the best.