Previous Blog Next Blog
Prev/Next Blog
by date

Creating Objects Without Creating Them

Creating Objects Without Creating Them
Name:Frank Carney
Date Posted:Dec 24, 2007
Rating:4.5 out of 5
Public:YES
Comments:YES
RSS Feed:GarageGames Blog feedor Subscribe with .
Profile Page:View profile page for Frank Carney

Blog post
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=1249...

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"?

Recent Blog Posts
List:12/25/07 - Milestone Reached: Mission Launched from Database
12/24/07 - Creating Objects Without Creating Them
12/24/07 - Another Sqlite Blog
09/03/07 - SQLite is Fun!
08/20/07 - Playing House
07/05/07 - Getting burned out and coming back, again...
04/07/07 - Inputs, Inputs everywhere...
04/01/07 - RPG, what does it mean?

Submit ResourceSubmit your own resources!

eb   (Dec 25, 2007 at 01:38 GMT)
Perhaps you can elaborate a little about this blog post because I think there is more in your head that you are not writing out here. I could be wrong though!
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!

Johnny Hill   (Dec 25, 2007 at 02:05 GMT)
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.

Frank Carney   (Dec 25, 2007 at 03:22 GMT)
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.

BrokeAss Games   (May 04, 2008 at 01:48 GMT)   Resource Rating: 5
Genius use of serialization, thanks man!

You must be a member and be logged in to either append comments or rate this resource.