Creating Objects Without Creating Them
by Frank Carney · 12/24/2007 (11:08 pm) · 4 comments
I am in the process of creating a simple routine to make a simple dynamic mission. The only thing static is the terrain file. I think I have come up with a really sneaky way of getting the terrain widgets into strings. First I am using some methods to serialize my objects to strings. I have combined a couple of things in these resouces:
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=8529
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=12492
I have combined the two to get serialization for all objects. The first one is the serialization to string. Then I stole the dynamic string allocation from cryptainer. This allows me to store the definition into a variable rather than just to file.
Okay, here is where I get really sneaky. Take the following code:
Can you say "random dungeons"?
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=8529
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=12492
I have combined the two to get serialization for all objects. The first one is the serialization to string. Then I stole the dynamic string allocation from cryptainer. This allows me to store the definition into a variable rather than just to file.
Okay, here is where I get really sneaky. Take the following code:
function createSky(%type)
{
%obj = new Sky(Sky) {
position = "336 136 0";
rotation = "1 0 0 0";
scale = "1 1 1";
materialList = "~/data/skies/sky_day.dml";
cloudHeightPer[0] = "0.349971";
cloudHeightPer[1] = "0.25";
cloudHeightPer[2] = "0.199973";
cloudSpeed1 = "0.0001";
cloudSpeed2 = "0.0002";
cloudSpeed3 = "0.0003";
visibleDistance = "500";
fogDistance = "300";
fogColor = "0.82 0.828 0.844 1";
fogStorm1 = "0";
fogStorm2 = "0";
fogStorm3 = "0";
fogVolume1 = "300 0 71";
fogVolume2 = "0 0 0";
fogVolume3 = "0 0 0";
fogVolumeColor1 = "128 128 128 -2.22768e+038";
fogVolumeColor2 = "128 128 128 0";
fogVolumeColor3 = "128 128 128 -1.70699e+038";
windVelocity = "1 1 0";
windEffectPrecipitation = "1";
SkySolidColor = "0.547 0.641 0.789 0";
useSkyTextures = "1";
renderBottomTexture = "0";
noRenderBans = "0";
locked = "true";
};
%retval = %obj.saveToString();
%obj.delete();
return %retval;
}This works fine and I can serialize the data this way with the "Sky" object. However, what happens when it is an object that needs the scenegraph? Take the next bit of code:function createTerrain(%terrainFile)
{
%obj = new TerrainBlock(Terrain) {
rotation = "1 0 0 0";
scale = "1 1 1";
terrainFile = %terrainFile;
squareSize = "8";
emptySquares = "";
bumpScale = "1";
bumpOffset = "0.01";
zeroBumpScale = "8";
tile = "1";
position = "-1024 -1024 0";
locked = "true";
};
%retval = %obj.saveToString();
%obj.delete();
return %retval;
}This won't work. The TerrainBlock needs the scenegraph to instantiate itself. So what to do. Well, you could lie to it by doing this:function createTerrain(%terrainFile)
{
%obj = new ScriptObject(Terrain) {
rotation = "1 0 0 0";
scale = "1 1 1";
terrainFile = %terrainFile;
squareSize = "8";
emptySquares = "";
bumpScale = "1";
bumpOffset = "0.01";
zeroBumpScale = "8";
tile = "1";
position = "-1024 -1024 0";
locked = "true";
};
%retval = %obj.saveToString();
%retval = strreplace(%retval, "ScriptObject", "TerrainBlock");
%obj.delete();
return %retval;
}Now I can create any object that the game has regardless of dependencies. Then I can just change the object type with a new type. In this instance I changed the object type to "TerrainBlock". The nice thing is I get the ability to use the code itself to create the object definition and can create any object at any time. This would allow things like generating a new mission while already in a mission that will not disturb the current mission.Can you say "random dungeons"?
About the author
#2
12/25/2007 (2:05 am)
So is it a real terrain block or simply a placeholder one in the mission. I mean complete with collisions and all because what if I want a presistent geographic and just load up the different ter files on the fly.not completely random. I mean I too think there is a ton of possiblities you hiding heh with this LOL, amazing progress.
#3
This post is just about how I can create a ScriptObject and generate all the script for a terrain object without creating a terrain object. Then I substitute the type with the Terrain type. This allows me to create a serialized version of the Terrain object easily. I could do it by hand but it would be a pain. This is then passed to another routine that builds the mission file for launching a mission. So yes, this is about the wonderful world of Torque scripting. I just thought it was rather sneaky. :)
Johnny,
When I substitute the TerrainBlock value back in and then eval the %retval it will try and create a real terrain block. However, I feed this data into another function that actually builds the mission file and saves to disk. Then I launch that file as the mission.
12/25/2007 (3:22 am)
eb,This post is just about how I can create a ScriptObject and generate all the script for a terrain object without creating a terrain object. Then I substitute the type with the Terrain type. This allows me to create a serialized version of the Terrain object easily. I could do it by hand but it would be a pain. This is then passed to another routine that builds the mission file for launching a mission. So yes, this is about the wonderful world of Torque scripting. I just thought it was rather sneaky. :)
Johnny,
When I substitute the TerrainBlock value back in and then eval the %retval it will try and create a real terrain block. However, I feed this data into another function that actually builds the mission file and saves to disk. Then I launch that file as the mission.
#4
05/04/2008 (1:48 am)
Genius use of serialization, thanks man!
Torque 3D Owner eb
I don't think you made this post on the realization of the power of torque-scripting...so I think there is more to 'meets the eye/in your mind' here. ...maybe 1 end-summary paragraph ?
- If I assumed too much then just ignore me!
Merry Xmas either way!