Generate XML "on the fly"?
by Henry Shilling · in Torque X 2D · 01/13/2010 (11:54 pm) · 2 replies
OK this may sound odd or not, I am working on this kind of auto generated level thing and I would like to create an XML scene in memory then load it. It seems the only way I can use SceneLoader.Load is with a file. Or maybe there is a way I can auto generate a file and write it out to be read in on load?
The reason I am trying to create xml is so I can predefine chunks of a scene, and then stitch them together into a new larger scene. make sense?
The reason I am trying to create xml is so I can predefine chunks of a scene, and then stitch them together into a new larger scene. make sense?
About the author
http://twitter.com/theBigDaddio
#2
Another option that comes to mind is to pass an xml string as the string parameter to SceneLoader.Load() and then mod TorqueXmlDeserializer.Process() to check if it's xml or a filename and act appropriately. You'd also want to mod SceneLoader.Load() as it stores the filename for later reference and obviously you don't want it storing a whole xml file in it's dictionary of loaded scenes :-) You might get around it by prepending the xml string with a 'filename' to call the in-memory scene by, and then in SceneLoader you just use that bit and in TorqueXmlDeserializer you use the xml bit.
Alternatively, you can of course simply save the xml to disk and then you can load it in the normal way. You can either handle the xml with plain text strings directly, or use the XmlDocument class in .NET (I prefer the latter usually). For example, you might load a 'template' scene file into an XmlDocument in memory, add stuff to it and then write it back to disk (or mod the engine as discussed above and avoid writing to disk at all).
Just a few thoughts - hope some of it gives you a few useful ideas to follow up.
01/14/2010 (11:31 am)
If you want to do it dynamically in memory then the place TX loads the xml into memory is in TorqueXmlDeserializer.cs (lines 144-145). You could either mod it so it can pick up an in-memory xml document at that point instead of a named file on disk, or you could ghost the scene loading process - [from SceneLoader.Load() through to TorqueXmlDeserializer.Process()] - with copied versions of the functions that pass an xml document instead of a string.Another option that comes to mind is to pass an xml string as the string parameter to SceneLoader.Load() and then mod TorqueXmlDeserializer.Process() to check if it's xml or a filename and act appropriately. You'd also want to mod SceneLoader.Load() as it stores the filename for later reference and obviously you don't want it storing a whole xml file in it's dictionary of loaded scenes :-) You might get around it by prepending the xml string with a 'filename' to call the in-memory scene by, and then in SceneLoader you just use that bit and in TorqueXmlDeserializer you use the xml bit.
Alternatively, you can of course simply save the xml to disk and then you can load it in the normal way. You can either handle the xml with plain text strings directly, or use the XmlDocument class in .NET (I prefer the latter usually). For example, you might load a 'template' scene file into an XmlDocument in memory, add stuff to it and then write it back to disk (or mod the engine as discussed above and avoid writing to disk at all).
Just a few thoughts - hope some of it gives you a few useful ideas to follow up.
Torque Owner Trent