Need help with a simple example.
by Russell Buxton · in General Discussion · 12/31/2007 (3:47 pm) · 2 replies
Hello...I am new to TS and i'm trying to learn some very basic concepts.
I have gone through some of the tutorials"Fish Game" and "Shooter".I got alot out of them but i'm still unsure about some things.I have made a new project to practice on learning how functions and variables work and how to get and pass them between functions.My level has three shapes that are simple static sprites I made in PS.I have enabled mouse events in the "game.cs" and on all three of the objects in their class scripts.These are my class script files...
function triangle_class::onLevelLoaded(%this)
{
%this.setUseMouseEvents(true);
$value=0;
}
function triangle_class::onMouseUP(%this)
{
if($value>=10)
{
$value=0;
echo("You clicked the triangle!");
echo($value);
%TV = %this.triangle_value;
echo(%TV);
}
else if($value<10)
{
$value+=2;
echo("You clicked the triangle!");
echo($value);
}
}
This script is the same for all three objects except for the lines about %TV and $value initialized to 0.The triangle_value is a dynamic field I added to the triangle in the editor but it's not getting processed for some reason.Everything else is working.You can click on objects and see $value get 2 added to it each time until 10 where it will reset to 0.
Ok...some questions:(1)Are all variables with the prefix "%" local to this class script?If not, whats the method for passing them to other class member functions?(2)How could I pass the $value to the other classes or objects in the scenegraph?As an example:when I click the triangle have it pass $value to square class,then square class to octogon class so that "echo($value) would return 6, all with just one click?(3)I would like to add a "text" object in the editor and pass variables to it instead of the console so I don't have to keep opening and closing it.(4)I noticed in the tutorials that functions are done differently sometimes.As an example...
"function class_name::onLevelLoaded(%this)"...as oppossed to just "function name()".Whats the difference between the two?Ok...enough for now...:).I'll keep at it in the meantime.Thanx for any help.
I have gone through some of the tutorials"Fish Game" and "Shooter".I got alot out of them but i'm still unsure about some things.I have made a new project to practice on learning how functions and variables work and how to get and pass them between functions.My level has three shapes that are simple static sprites I made in PS.I have enabled mouse events in the "game.cs" and on all three of the objects in their class scripts.These are my class script files...
function triangle_class::onLevelLoaded(%this)
{
%this.setUseMouseEvents(true);
$value=0;
}
function triangle_class::onMouseUP(%this)
{
if($value>=10)
{
$value=0;
echo("You clicked the triangle!");
echo($value);
%TV = %this.triangle_value;
echo(%TV);
}
else if($value<10)
{
$value+=2;
echo("You clicked the triangle!");
echo($value);
}
}
This script is the same for all three objects except for the lines about %TV and $value initialized to 0.The triangle_value is a dynamic field I added to the triangle in the editor but it's not getting processed for some reason.Everything else is working.You can click on objects and see $value get 2 added to it each time until 10 where it will reset to 0.
Ok...some questions:(1)Are all variables with the prefix "%" local to this class script?If not, whats the method for passing them to other class member functions?(2)How could I pass the $value to the other classes or objects in the scenegraph?As an example:when I click the triangle have it pass $value to square class,then square class to octogon class so that "echo($value) would return 6, all with just one click?(3)I would like to add a "text" object in the editor and pass variables to it instead of the console so I don't have to keep opening and closing it.(4)I noticed in the tutorials that functions are done differently sometimes.As an example...
"function class_name::onLevelLoaded(%this)"...as oppossed to just "function name()".Whats the difference between the two?Ok...enough for now...:).I'll keep at it in the meantime.Thanx for any help.
#2
01/02/2008 (12:13 pm)
Thanx Nikos...to be honest I still don't completely understand...but if this stuff was easy I suppose everyone would be doing it...LOL.
Torque Owner Nikos Beck
Firstly, "class_name::foo(%this)" is a member function, it's a function that belongs to a class. Only objects of that class can call that function. The "%this" parameter is the object itself. So, calling "an_object.foo()" will automatically pass "%this" into the function.
You can have "%this.value = 3;" within the "triangle_class:onLevelLoaded" function. That way, every triangle knows it's value. When clicking on a triangle you can have the line "$value += %this.value" where "%this.value = 3;" for triangles and "%this.value = 4;" for squares. If It use "%this.blah" then the variable is local to the object, it will be accessible from that object no matter which function you are in. Using "%blah" makes the variable local to the function only. Using "$blah" makes the variable visible everywhere; every function, every object, every script file. It's global.
You probably also want to put the line "$value = 0;" into the scene graph's "onLevelLoaded" function so that it's initialized once for the entire scene rather than every time a triangle is created.
Does that answer some of your questions?