Game Development Community

Attaching scripts to objects

by arteria3d · in Torque Game Engine · 12/13/2004 (4:01 pm) · 14 replies

My question is this....

Say, i make a rock object and place it in a relvant folder so it apears within the editor - if i want to attach specific details to the rock so that everytime i insert that object in the mission editor - the details pertain to that object - do i just need to make 1 script..... and one datablock for the object - and that script attaches itself to as many of the objects that i insert???? manually.

So therefore if i have a script for a rock, and within it is emap=1 to turn on environment mapping - will the environment mapping get added to the rock object each time i insert it????? - or does the script only work for one object? Note here please - that i used EMAP just as an example.

Please Help

Thanks

Steve

About the author

Owner of uk based Ltd company ArteriaMediaLtd. with a trading name of Arteria3d Website;arteria3d.com


#1
12/14/2004 (2:53 am)
It works for all rock Objects .
Do a dump in a onadd function and look in the console then you see all parameters for every rock you add.
#2
12/14/2004 (4:54 am)
So in affect the ONadd function means....... that On Adding this object, whateveris contained withiin its bracets is accosiated with that item?
#3
12/14/2004 (5:18 am)
Yes
if you want some special function for 1 of the objects i would make a new scripts for that object.
But it really depends on what function or behavior you are after.
#4
12/14/2004 (5:21 am)
To do this would i insert the object actually by way of the script and if so can u explain how i actually load an object via the script way please - so it appears on screen when the script loads. This part has kinda eluded me

By the way Billy - thanks for your help
#5
12/14/2004 (5:29 am)
Tell exactly what you want todo with that rock or what ever .
Basicly you dont need a script at all for a rock ,if it only should be a rock .
The most simple script is

datablock StaticShapeData(Rock)
{
      category = "Rocks";
      shapefile = "path to the rock dts file";
};
#6
12/14/2004 (5:39 am)
Yeah i know - the rock was only an example.

Okay

I want to add a tree, and sway it slightly side to side. Okay not the best of animations - but for every few trees or so - it does look okay as long as they are all swaying side to side on different movement paths. I thought of using COS to use with the rotatation of the X axis to sway it gently. I have done this in other platforms and got a good result. Initally swaying the whole tree sounds awful - buts its not - if u look at that wondefrful game Farcry most of the palms in there are swaying as awhole unit - not just the branches.

Anyways - i cant seem to understand how to actually add the tree so it apears on screen straight away from a script - so then i can in the same script use a call function to repeat the rotation movement - but how do i get the model to display from script on game start??? Such a simple quesstion it sounds i know - but it doesnt elude me.

Steve
#7
12/14/2004 (5:46 am)
Take a look at this Steve if you not get it to work ask moore .
Tutorial
Its basicly the same thing as the tree animation .
#8
12/14/2004 (6:00 am)
Hi mate,

Just check that out - yeah i have read that - but that is actually for using animations within an object. I dont have animation data - i just wnat to sway the tree. In simple terms - as i know in affect how i need to tackle the swaying i wish the tree to do - but how do i actually get an object from script to appear on screen. is it by making the datablock - and then using the return as in return(%obj) and putting a position in the sctipt to place it?

Cheers Bud

Steve
#9
12/14/2004 (6:22 am)
Why dont you use the editor ?
Do you want to spawn trees or place them in the world only ?
Think we walk around each other :)
#10
12/14/2004 (6:24 am)
I need to place them via script - cos certain ones i place - i wish to move side to side as if they are swaying - so i need to place the tree via a script dont i - so i can attach the movement to the tree

Cheers

Steve
#11
12/14/2004 (6:33 am)
Ye but if you make a script like the flag script without any animation
you can place the trees with the editor.
After that you can start to figure out how to make the sway function.
But why dont you make the animation when you export the shape ?
Then you only need a tiny script like that flagpole !
If you have moore then 1 animation on the trees then you can play them
randomly.
#12
12/14/2004 (10:23 am)
Hi,
Again Billy - thanks for the help.


I have written the below script, which should when a tree is inserted in the mission editor, it automatically rotates that tree.

Not this is not the finished function - and obviously just rotating the object - doesnt do much! but i just wanna first see whether it does actually move the item - but it doesnt. Any ideas why?


Steve
#13
12/14/2004 (10:26 am)
Steve - You forgot to post the script.
#14
12/14/2004 (10:28 am)
Ha !!! so i did




datablock StaticShapeData(Palmsc)
{

category = "Palms";
shapeFile = "~/data/palms/PalmC.dts";
isPlaying = 1;
emap = true;
};

function StaticShapeData::create(%block)
{

%obj = new StaticShape()
{
dataBlock = %block;
};
return(%obj);
}








%xfrm = %obj.getTransform();
$lx = getword(%xfrm,0);
$ly = getword(%xfrm,1);
$lz = getword(%xfrm,2);
$rx = getword(%xfrm,3);
$ry = getword(%xfrm,4);
$rz = getword(%xfrm,5);





function emappalm::onAdd(%this,%obj,%cx)
{

%obj.setTransform($lx SPC $ly SPC $lz SPC %cx SPC $ry SPC $rz);

while (%cx < 300)
{
%cx++;
}

schedule(200,0,emappalm,%this,%obj,%cx);

}



emappalm();