Creating new datablocks on the fly in script?
by Keith Johnston · in Torque Game Engine · 11/21/2004 (5:08 pm) · 9 replies
Is it possible to create a new datablock on the server-side on the fly in script? Can someone give me an example? I've tried using "eval" to no avail, and I can't seem to get a script to compile with a datablock creation - the only thing that works so far is through an "exec". I could write out a temporary file and exec it, but seems messy.
Thanks,
Keith
Thanks,
Keith
#2
That is the correct solution to the problem you are trying to solve.
I am not sure how dynamically inserting datablocks would keep other datablocks from being in memory...perhaps you left something out of that description.
11/21/2004 (6:52 pm)
The textures for objects only load at that time if "preload" is set to true. If preload is set to false then the textures (and everything else) is not loaded until the object is actually used.That is the correct solution to the problem you are trying to solve.
I am not sure how dynamically inserting datablocks would keep other datablocks from being in memory...perhaps you left something out of that description.
#3
11/21/2004 (7:15 pm)
Hm, I tried that - I set preload=false in the datablocks and the textures are still loaded (I can see them when I run DumpTextureStats).
#4
11/21/2004 (7:19 pm)
The only things I see in scripts in the samples that have "preload" are AudioProfiles - I don't see any dts shapes with a preload setting.
#5
I have doen this, but yes, you have to write it to a cs file. I used the writeln function to write out a cs file in script, and then exec the scrip. That is the only way I have done it.
11/21/2004 (11:54 pm)
Keith, I have doen this, but yes, you have to write it to a cs file. I used the writeln function to write out a cs file in script, and then exec the scrip. That is the only way I have done it.
#6
11/22/2004 (3:07 am)
I did the same as Cameron.. I used FileObject to write a file and then ran eval on it.
#7
This did the trick!
11/24/2004 (9:40 am)
Well I found another workaround - I just delete the datablocks I don't need before the mission is downloaded:// Delete datablocks for rooms we don't need
%dataGroup = "DataBlockGroup";
for (%i = 0; %i < %dataGroup.getCount(); %i++) {
%obj = %dataGroup.getObject(%i);
if (%obj.dontNeed) {
echo ("Deleting: " @ %obj.getName());
%obj.delete();
}
}This did the trick!
#8
I have a console class ShipResource that is basically a combination of static data and baseline current status data for creating some type of FlyingVehicle. I have an in-game editing system so that users can create more types of ships on the fly, which will be saved and available next time. My game loads a bunch of these resources, and then it is supposed to create actual FlyingVehicleData datablocks based on those resources (mainly because I can't save datablocks correctly by doing myDatablock.save(filename); ).
I have this function which is supposed to do this:
It doesn't compile- of course, if this worked, I wouldn't be here ;) According to the thread above, it seems that you can't make a datablock except as part of a static file- not inside a function. Do I really have to go through all the trouble of making a temporary file, writing out code for a datablock or fifty, and exec'ing that file? Why is this the case? I don't get this, it seems really limiting, and it's not documented in the TorqueScript references or the 3DGPAI1 books. Thanks for any help!
03/16/2006 (2:51 pm)
Sorry to go gravedigging, but I'm having a problem similar to this.I have a console class ShipResource that is basically a combination of static data and baseline current status data for creating some type of FlyingVehicle. I have an in-game editing system so that users can create more types of ships on the fly, which will be saved and available next time. My game loads a bunch of these resources, and then it is supposed to create actual FlyingVehicleData datablocks based on those resources (mainly because I can't save datablocks correctly by doing myDatablock.save(filename); ).
I have this function which is supposed to do this:
function ShipResource::convertAndLoad(%this)
{
%datablockName = %this.getName() @ "Data";
// Create the datablock!
datablock SpaceVehicleData(%datablockName)
{
spawnOffset = "0 0 0";
emap = true;
category = "Ships";
shapeFile = %this.shapeFile;
....
};
}It doesn't compile- of course, if this worked, I wouldn't be here ;) According to the thread above, it seems that you can't make a datablock except as part of a static file- not inside a function. Do I really have to go through all the trouble of making a temporary file, writing out code for a datablock or fifty, and exec'ing that file? Why is this the case? I don't get this, it seems really limiting, and it's not documented in the TorqueScript references or the 3DGPAI1 books. Thanks for any help!
#9
Is there a significant difference between this and using the datablock keyword? Is it possible to fudge what "datablock" does using "new"?
03/18/2006 (10:05 am)
How exactly does the "datablock" keyword differ from the "new" keyword? Is it possible to do this:%ssdb = new SpaceVehicleData(SpaceData)
{
shapeFile = "someShape.dts";
...
};
datablockGroup.add(%ssdb);Is there a significant difference between this and using the datablock keyword? Is it possible to fudge what "datablock" does using "new"?
Torque 3D Owner Keith Johnston
I'm dynamically inserting objects into a mission from within the missionLoad.cs. This works fine if the datablocks are defined in a .cs file.
Now I've realized that all the datablocks that are in memory are sent to the client - which is causing resources (textures) for those datablocks to load, even though they are not used.
So I'm thinking that if I dynamically insert the datablock for the dynamic object that I am creating, then the other datablocks won't be in memory and therefore won't be downloaded to the client and trigger the loading of the textures for those datablocks.