Game Development Community

My firts script not working... please could someone look at this

by arteria3d · in Torque Game Engine · 12/02/2004 (12:11 pm) · 9 replies

Hi i have just written my first script which attempts to do very simple psuedo tree animation, by simplying using cos to sway the tree. Thing is i am looking at the code, debuggng it to see why on the console it generates errors with the syntax. Can anyone help please: The code is as fololows:



datablock itemdata(palms)

{

shapefile = "~/palm1.dts";
mass = 1;
friction = 1;

};



function insertpalms()

{

%shape = new item();

datablock = palms;
rotation = "0 0 1 0";

};

missioncleanup.add(%shape);

%shape.setTransform ("-500 500 20 0 0 1 0");

}



function movetree(%shape,%trx)
{

%xfrm = %shape.getTransform();
%rx = getword(%xfrom,3);

%sway++;

%swaytree = cos(%sway);

%shape.setTransform(500 500 20 %swaytree 0 1 0);



schedule(200,0,movetree,%shape,%trx);

}






and the errors i get are:

//-------------------------- 12/2/2004 -- 21:50:10 -----
animatetree.cs
Compiling CH2/animatetree.cs...
CH2/animatetree.cs Line: 21 - Syntax error.
>>> Advanced script error report. Line 41.
>>> Some error context, with ## on sides of error halt:
function insertpalms()



{



%shape = new item();



datablock = ##p##alms;

rotation = "0 0 1 0";



};



missioncleanup.add(%shape);
>>> Error report complete.

Executing CH2/animatetree.cs.
CH2/animatetree.cs Line: 21 - Syntax error.
>>> Advanced script error report. Line 41.
>>> Some error context, with ## on sides of error halt:
function insertpalms()



{



%shape = new item();



datablock = ##p##alms;

rotation = "0 0 1 0";



};



missioncleanup.add(%shape);
>>> Error report complete.


I would be most apreciative if someone could check this for me please.

Thanks

Steve

About the author

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


#1
12/02/2004 (12:27 pm)
Hmm first problem you need to include the datablock inside the constructor also since later you set the transform no need to set the position
also I changed the type to static shape, they are lighter than items
datablock staticshapedata(palms)
{
   shapefile = "~/palm1.dts";
   category= "Tree"; //now it will appear in the editor for manual insertion
};

function insertpalms()
{
   %shape = new staticShape()[b]{ [/b]
   datablock = palms;
   [b]};[/b]

   MissionGroup.add(%shape);
   %shape.setTransform ("-500 500 20 0 0 1 0");
}

next, typo on xfrm remove the o but more importantly this code make the tree move not sway instead make a sway animation in the dts file. If the object contained an animation called "sway" it would be like this, we will have the object start swaying when it is added to the scene

function palms::onAdd(%this,%obj)
{
   %obj.playThread(0, "sway");
}
#2
12/03/2004 (2:31 am)
Hi,

I am really confsed... The code is as follows:



datablock staticshapedata(palms)
{
shapefile = "~/palm1.dts";
category= "Tree"; //now it will appear in the editor for manual

insertion


};

function insertpalms()
{
%shape = new staticShape(){
datablock = palms;
};
MissionGroup.add(%shape);
%shape.setTransform ("500 500 20 0 0 1 0");

}


function main()

{

%xfrm = %shape.getTransform();

}




On the above simple code i get the following error - why?



Loading compiled script CH2/one.cs.
CH2/one.cs (26): Unable to find object: '' attempting to call function 'getTransform'
CH2/one.cs (0): Unable to find object: '' attempting to call function 'setTransform'


i would be grateful if anyone could tell me why? as simple codeing tasks that i understand the fundamentals - i am getting stuck at the herdle of syntax....

Thanks

Steve
#3
12/03/2004 (6:29 am)
You need to learn how to fix syntax errors, telling me the error does not help, without the file, and NO do not post your file here. Instead let's get YOU bug fixing.

The error says the problem is on line 26 of the page one.cs Open up the file and do a search for that line ( Ctrl + G in notepad).

If that error is in the main(), which I presume it is you need to analyze the code.

Unable to find object: '' attempting to call function 'getTransform'
is saying whatever variable is calling getTransform() does not exist.

Your main function, for instance has the line
%xfrm = %shape.getTransform();
well %shape has never been defined. You can't use a variable until it is initalized.

I presume you are learning from Ken Finley's book, which is great, but before you experiment too much besure you understand the fundamentals to programming and bug checking.

Things that are essential, understanding :
Variables, declaring and initalizing them and utilizing their scope
Functions, how to pass and return variables into custom functions
Control Structures, if/else/for/while what these keywords mean and how to use them.
Arrays, how to initalize and output various elements
Objects, how create and initalize Object properties and how to impliment Object methods
#4
12/03/2004 (6:48 am)
Everything Anthony says is very important--it's much better to learn from the beginning how to troubleshoot--it's one of the most critical tasks a developer can learn!

That being said, the problem you are having is the path in your datablock definition, specifically the shapeFile line.

shapefile = "~/palm1.dts";

it is shapeFile (F capitalized), and in addition I would bet that "~/palm1.dts" is not going to find your .dts file, but I cannot confirm that without knowing your exact environment paths.
#5
12/03/2004 (12:53 pm)
@Anthony

Hi mate - i have initialised the shape file in the line :%shape = new staticShape(){


That defines it ?

Steve
#6
12/03/2004 (1:27 pm)
@Stevie--only if the call -works-. And it isn't. See my post above.
#7
12/03/2004 (1:50 pm)
You might initalize A shape with that call but because it is a local variable the variable you have to hold it %shape dissapears after the function has been executed . . . use a Global variable if you want to refrance it outside the function it was created in.
#8
12/03/2004 (2:18 pm)
Great point as well Anthony, I got caught up in the typos and missed the scope problem.

Instead of using %shape, use $shape (which means that it is a global script variable) so the rest of your functions can see it as well. Either that, or pass it back and forth through the parameters, but one step at a time is a good thing!
#9
12/03/2004 (7:21 pm)
Thanks for everybodys help. i got the following to work - but why doesnt it display the object made directly on the screen at the position given? I have exagerated the Z coord just so i could see whether the object apeared.




datablock staticshapedata(palmtree)
{
category = "misc";
shapefile = "~/data/shapes/naturepack/palm1.dts";
};





function staticshapedata::create(%block)
{

%obj = new StaticShape()

{
datablock = %block;
};
return(%obj);
}


function addpalm::onadd(%this,%obj)
{

%obj.setTransform("500 230 230 0 0 0 0");


}



Any help would be very much apreciated - as i feel it is really benefiting me!

Thanks

Steve