generic type assignment?
BlitzMax Forums/BlitzMax Beginners Area/generic type assignment?
| ||
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? |
| ||
Anyone know how to resolve this? At the risk of repeating myself. Runtime Reflection. There is no other (practical) way to do it. |
| ||
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 |
| ||
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. |
| ||
Yeah, I see where your going with this. Maybe a generic method is not the best. |