Plastic Gem #20: Dynamic Creation
by Paul Dana · 07/07/2008 (9:52 am) · 1 comments
Download Code File

Plastic Gem # 20 : Dynamic Creation
Difficulty: Easy
Hi Paul Dana again from Plastic Games. This gem is short but loveable. I will show you a small bit if code that I use when creating objects dynamically, in other words, from script code rather than with the Mission editor.
1) Unzipping the files
Unzip the pg20_dynamicCreation.zip. In there you will find a script file called StaticShape.cs.
2) Executing the script
If you have followed Gem #5 on portability then you might want to put this script into your plastic folder and exectue it from there. I keep it in a folder called plastic/server/core. You can put it there and edit the init.cs in the server folder to execute it. Alternately you can just copy this code to the bottom of a file you already have called ~/server/scripts/staticShape.cs. Copy the single method defined in the file with this gem to the end of that file. That file is already executed so there is nothing more to do.
3) Checking it out
The truth is I have already shared a function like this with you already in Gem #17 on managed triggers, I just never took the time to explain it. When you create objects dynamically from in-game there is a rule that you should put those objects in a group called MissionCleanup, which is deleted after each mission is finished. You can see that this method will do that for you.
Here is how you use this method. Let's say you have a static shape with a datablock you have defined called GrandfatherClock, such as we defined in Gem #1, Gem #2, and Gem #3. Let's say you want to create a shape of this kind and call it "Clock". here is what you would do:
Simple yes? Plus the code reads well. The name is optional. If you don't need the object to have a name just don't pass one in.
The Next Gem
That's all for this gem. Next gem I will share some math utility functions that are useful for aligning an object between two points. This is useful for all sorts of things, from auto alignging fence rails with fence posts to makingn lighting strike the vitctim of your choice! :-)

Plastic Gem # 20 : Dynamic Creation
Difficulty: Easy
Hi Paul Dana again from Plastic Games. This gem is short but loveable. I will show you a small bit if code that I use when creating objects dynamically, in other words, from script code rather than with the Mission editor.
1) Unzipping the files
Unzip the pg20_dynamicCreation.zip. In there you will find a script file called StaticShape.cs.
2) Executing the script
If you have followed Gem #5 on portability then you might want to put this script into your plastic folder and exectue it from there. I keep it in a folder called plastic/server/core. You can put it there and edit the init.cs in the server folder to execute it. Alternately you can just copy this code to the bottom of a file you already have called ~/server/scripts/staticShape.cs. Copy the single method defined in the file with this gem to the end of that file. That file is already executed so there is nothing more to do.
3) Checking it out
function StaticShapeData::createStaticShape(%this, %name)
{
%obj = new StaticShape(%name)
{
dataBlock = %this;
};
if (isObject(MissionCleanup))
MissionCleanup.add(%obj);
return %obj;
}The truth is I have already shared a function like this with you already in Gem #17 on managed triggers, I just never took the time to explain it. When you create objects dynamically from in-game there is a rule that you should put those objects in a group called MissionCleanup, which is deleted after each mission is finished. You can see that this method will do that for you.
Here is how you use this method. Let's say you have a static shape with a datablock you have defined called GrandfatherClock, such as we defined in Gem #1, Gem #2, and Gem #3. Let's say you want to create a shape of this kind and call it "Clock". here is what you would do:
%obj = GrandfatherClock.createStaticShape("Clock");Simple yes? Plus the code reads well. The name is optional. If you don't need the object to have a name just don't pass one in.
The Next Gem
That's all for this gem. Next gem I will share some math utility functions that are useful for aligning an object between two points. This is useful for all sorts of things, from auto alignging fence rails with fence posts to makingn lighting strike the vitctim of your choice! :-)
Torque 3D Owner Danni