Extend FileObject to support writing SimObjects
by Justin DuJardin · 06/23/2005 (4:52 pm) · 4 comments
While SimObjects all have their own .save methods that will write them out to a file, there is no current functionality that allows you to have a file opened for write and to dump a SimObjects script representation into that file stream. This small resource will allow you to do this.
For this resource we will be modifying two files
Core/fileObject.cc
and
Core/fileObject.h
First we need to open up fileObject.h and around line 37 you'll want to add this function prototype.
Core/fileObject.h
Next we need to add this function as well as it's ConsoleMethod counterpart into fileObject.cc. So open up your fileObject.cc and let's just drop this in at the very bottom of the file for simplicity's sake.
Core/fileObject.cc
That is it and you should be good to go!
I hope some of you find this as useful as I have. Below to illustrate it's use, I have included a small script snippit.
Note that $editObject.config is a pointer to the objects datablock which is subsequently written to the open file stream!
-justin'
For this resource we will be modifying two files
Core/fileObject.cc
and
Core/fileObject.h
First we need to open up fileObject.h and around line 37 you'll want to add this function prototype.
Core/fileObject.h
void writeObject( SimObject* object );
Next we need to add this function as well as it's ConsoleMethod counterpart into fileObject.cc. So open up your fileObject.cc and let's just drop this in at the very bottom of the file for simplicity's sake.
Core/fileObject.cc
ConsoleMethod( FileObject, writeObject, void, 3, 3, "FileObject.write(SimObject)" )
{
SimObject* obj = Sim::findObject( argv[2] );
if( !obj )
{
Con::printf("FileObject::writeObject - Invalid Object!");
return;
}
object->writeObject( obj );
}
void FileObject::writeObject( SimObject* object )
{
object->write( stream, 0 );
}That is it and you should be good to go!
I hope some of you find this as useful as I have. Below to illustrate it's use, I have included a small script snippit.
// Create File Object. %file = new FileObject(); // Open The File %file.openForWrite( ExpandFilename(%filename) ); // Write Header. %file.writeLine( $ObjectFileHeader ); // Write Datablock Header %file.writeLine( $ObjectDataBlockHeader ); // Write Datablock %file.writeObject( $editObject.config );
Note that $editObject.config is a pointer to the objects datablock which is subsequently written to the open file stream!
-justin'
#2
the file? Is there a readObject() function? I guess I should look at the code before I post stupid
questions =)
06/24/2005 (1:40 pm)
Just out of curiosity, what would you need to do to load this information back into a datablock fromthe file? Is there a readObject() function? I guess I should look at the code before I post stupid
questions =)
#3
@Midhir: you'll want to write it out to either a script file that you can exec() in or parse it with your own parser.
thx Justin!
06/24/2005 (1:56 pm)
this will make it immensely easier to write out object property when you don't want to use the .save() functions... previously you'd have to make some sort of iteration loop or keep track of the data some other way... this will be great for writing out dynamic .cs files :)@Midhir: you'll want to write it out to either a script file that you can exec() in or parse it with your own parser.
thx Justin!
#4
Looks like I have yet another resource to assimilate =)
(edit)
Heh, another silly question; would this work to save out (for example, individual player's
scripts) on the server side only upon quitting, while still allowing the script to be used by the player
when he/she logs in?
I'm sure this will take alot more work than just this resource. I just want to verify that this would
work before I start working on it =)
06/30/2005 (12:05 am)
Oh, nice...this would work for all kinds of cool stuff. Thank you for releasing this.Looks like I have yet another resource to assimilate =)
(edit)
Heh, another silly question; would this work to save out (for example, individual player's
scripts) on the server side only upon quitting, while still allowing the script to be used by the player
when he/she logs in?
I'm sure this will take alot more work than just this resource. I just want to verify that this would
work before I start working on it =)

Torque Owner Jason Farmer