Need help with functions...
by Russell Buxton · in Torque Game Builder · 01/02/2008 (12:53 pm) · 20 replies
Hello...I need some help with functions and using them to pass values back and forth to objects of different classes.I have made a practice level that has three different shapes..."Triangle","Square",and"Octogon".I also have a small circle sprite to serve as a ball.These all have been set to different classes and have seperate ".cs"files.I am trying to get the ball to be passed from shape to shape..ie:when I click the square,if the ball is not already on the square,move to the square and wait for next click.I was able to get this working by placing the code on each shapes class script but I would like it setup differently.Like this maybe...When a shape is clicked have the shape call a function thats in the balls class script and pass it's x and y positions to it.Then have the ball use the function with the passed values to move.I thought this would be a good way to learn about functions and the scope of different variable types.Can someone please post a small snippet showing the proccess?Doesn't have to be exactly what I have here,just an example of a function of one class calling another function of a different class and passing some variables.Sorry so long winded...Thanx for any help...:)
#2
01/02/2008 (3:01 pm)
Is everyone on these forums so friendly?It's a post not an essay!Whats so hard to read?If you find it so hard to read then DON'T.I'm sure there are plenty of people that are willing to help begginers minus the condescending attitude.
#3
See? Isn't that easier? Just keep reading it until it makes sense.
01/02/2008 (6:04 pm)
Would you prefer that I give you nicely formatted examples or just long drawout formless stream-of-conciousness...because either would work but only one would be easily understood. the problem you were having is that you need to declare the function within the target class and then give the source class the ability to pass arguments to the function class.since you have multiple similar classes, it would be good to user inheritance to centralize the common features of the objects, thusly giving you the ability to make changes to multiple similar classes without much effort. Its useful for fixing multi-class bugs as well...which we all make...well some of us make. dont forget to remember the differences in the variable scops, because you may not be changing what you think you are changing.See? Isn't that easier? Just keep reading it until it makes sense.
#4
01/02/2008 (7:50 pm)
Maybe I should stick to modelling and leave the code to peeps like yourself.The more I read the more confused I am.I was never good at math so that doesn't bode well for coding which is mostly thinking in an algorithmic mode.I can model,texture,rigg,and animate a character in 2 to 3 days but this programming is a whole different animal.It's WAY hard...LOL.Sorry,not mad at you,just a bit frustrated that i'm not catching on faster than I am.Thanx again for your help.
#5
It'll be frustrating as all heck. That's the nature of the beast.
01/02/2008 (8:07 pm)
Seriously, you're starting at a very modest point, which is commendable. Many people actually think they'll just learn to program along the way as they make an MMO. Start with simple stuff, like echoing mouse clicks and other boring things. You'll quickly develop more skill, because you'll learn the fundamentals and actually understand them.It'll be frustrating as all heck. That's the nature of the beast.
#6
Also, I understand that the target class will contain the actual function.The problem i've been having is mostly syntax I think.I have'nt been able to pass a variable from somewhere else into the target function.I think you've already givin me the answer,it's just has'nt clicked yet.As an example:
I typed this from memory so bear with me...:)
Do I need to give the objects a name in the editor above where you define it's class?If so,how do I refer to it in script?No doubt this code is screwed up in many places,but maybe this will help clarify my questions.Using functions to exchange information and datablocks(<---I'll worry about those later)seem to be central to understanding and using "TS".Once I have grasped these concepts, I think i'll have a "small" foothold.Thanx again for your help and patience.
01/03/2008 (8:28 am)
Ok...I think I understand about the classes now.What your saying is I can make just one class script and let all of the shapes pull from it.I guess in my examples' case all that is different is their names and positions and the rest is inheretid <---ms?H'mm...how can I let the script know which shape has called it and its position?Wait a minute...will it automatically know because of the %this in the onLevelLoaded?Also, I understand that the target class will contain the actual function.The problem i've been having is mostly syntax I think.I have'nt been able to pass a variable from somewhere else into the target function.I think you've already givin me the answer,it's just has'nt clicked yet.As an example:
//This is the "Shape" class script
function shape_class::onLevelLoaded(%this,%scenegraph)
{
$whichever_shape=%this;//How can I let it know which shape this is refering to and its world Position?
%value_to_be_passed=2;//dummy value I want to pass to "balls" class script.
}
function shape_class::onMouseUp(%this,%mod,%worldPos,%clicks)
{
ball.dothis(%value_to_be_passed);//This is where i'm trying to pass to the other class.
}
//This is the "Ball" class script
function ball_class::onLevelLoaded(%this,%scenegraph)
{
$ball=%this;
}
function dothis(%value_to_be_passed)
{
echo(%value_to_be_passed);//I don't think the value is making it to this function.
}I typed this from memory so bear with me...:)
Do I need to give the objects a name in the editor above where you define it's class?If so,how do I refer to it in script?No doubt this code is screwed up in many places,but maybe this will help clarify my questions.Using functions to exchange information and datablocks(<---I'll worry about those later)seem to be central to understanding and using "TS".Once I have grasped these concepts, I think i'll have a "small" foothold.Thanx again for your help and patience.
#7
Your onLevelLoaded really doesn't need much.
In your onMouseUp, try this:
ball.dothis(%this.position);
Then make it ball::dothis(%position)
{
%this.setPosition(%position);
}
That means, when you have an onMouseUp event on a shape, it will pass to the object named 'ball' the command 'dothis' with an argument of the shape's position. Then, the ball's position will be set to the position of the shape. Also, make sure, in your level file, that the ball has a layer in front of the shapes.
01/03/2008 (9:58 am)
OK, to make an object a type of something, you'll need to give it that name in the level. There's no reason to declare a value to be passed since the object inherently has the value with its position.Your onLevelLoaded really doesn't need much.
In your onMouseUp, try this:
ball.dothis(%this.position);
Then make it ball::dothis(%position)
{
%this.setPosition(%position);
}
That means, when you have an onMouseUp event on a shape, it will pass to the object named 'ball' the command 'dothis' with an argument of the shape's position. Then, the ball's position will be set to the position of the shape. Also, make sure, in your level file, that the ball has a layer in front of the shapes.
#8
This is the error the console gives me: game/gamescripts/Ball.cs (8): unable to find object:' ' attempting to call setPosition.
Also my echo is giving me the mouses %worldPosition and not the clicked shapes position.I named the ball object "ball" in the editor and I tried refering to it with that as well as "ball_class" and I get the same error.
Almost there I think...LOL.
01/03/2008 (12:42 pm)
I'm still missing something.Heres what I have://Shape.cs file...
function shape_class::onLevelLoaded(%this,%scenegraph)
{
%this.setUseMouseEvents(true);
%this.setLayer(1);
}
function shape_class::onMouseUp(%this,%modifier,%worldPosition,%clicks)
{
%pos=%this.getPosition();
ball.dothis(%pos);//also tried ball_class.dothis(%pos)
echo(%pos);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Ball.cs file...
function ball_class::onLevelLoaded(%this,%scenegraph)
{
%this.setLayer(0);
}
function ball_class::dothis(%pos)
{
%this.setPosition(%pos);
}This is the error the console gives me: game/gamescripts/Ball.cs (8): unable to find object:' ' attempting to call setPosition.
Also my echo is giving me the mouses %worldPosition and not the clicked shapes position.I named the ball object "ball" in the editor and I tried refering to it with that as well as "ball_class" and I get the same error.
Almost there I think...LOL.
#9
01/03/2008 (1:14 pm)
Grab the ball object from level file, please, and post that paragraph here.
#10
01/03/2008 (1:51 pm)
I don't follow you?I see your face turning red about now...LOL.You mean the balls class script ie: Ball.cs?
#11
01/03/2008 (1:59 pm)
There is a file, its extension is .t2d. That's the actual level file. In that file, you invoke these new objects. Make sure you have a ball named "ball".
#12
levelContent = new t2dSceneGraph() {
canSaveDynamicFields = "1";
UseLayerSorting = "1";
layerSortMode0 = "Normal";
layerSortMode1 = "Normal";
layerSortMode2 = "Normal";
layerSortMode3 = "Normal";
layerSortMode4 = "Normal";
layerSortMode5 = "Normal";
layerSortMode6 = "Normal";
layerSortMode7 = "Normal";
layerSortMode8 = "Normal";
layerSortMode9 = "Normal";
layerSortMode10 = "Normal";
layerSortMode11 = "Normal";
layerSortMode12 = "Normal";
layerSortMode13 = "Normal";
layerSortMode14 = "Normal";
layerSortMode15 = "Normal";
layerSortMode16 = "Normal";
layerSortMode17 = "Normal";
layerSortMode18 = "Normal";
layerSortMode19 = "Normal";
layerSortMode20 = "Normal";
layerSortMode21 = "Normal";
layerSortMode22 = "Normal";
layerSortMode23 = "Normal";
layerSortMode24 = "Normal";
layerSortMode25 = "Normal";
layerSortMode26 = "Normal";
layerSortMode27 = "Normal";
layerSortMode28 = "Normal";
layerSortMode29 = "Normal";
layerSortMode30 = "Normal";
layerSortMode31 = "Normal";
cameraPosition = "0 0";
cameraSize = "100 75";
new t2dStaticSprite(triangle) {
imageMap = "TriangleImageMap";
frame = "0";
canSaveDynamicFields = "1";
class = "shape_class";
Position = "0.430 -15.774";
size = "8.000 8.000";
mountID = "2";
triangle_value = "5";
};
new t2dStaticSprite(square) {
imageMap = "SquareImageMap";
frame = "0";
canSaveDynamicFields = "1";
class = "shape_class";
Position = "-25.225 9.682";
size = "8.000 8.000";
mountID = "3";
square_value = "10";
};
new t2dStaticSprite(octogon) {
imageMap = "OctogonImageMap";
frame = "0";
canSaveDynamicFields = "1";
class = "shape_class";
Position = "24.939 10.043";
size = "8.000 8.000";
mountID = "4";
octogon_value = "15";
};
new t2dStaticSprite(ball) {
imageMap = "BallImageMap";
frame = "0";
canSaveDynamicFields = "1";
class = "ball_class";
Position = "27.174 -17.504";
size = "2.000 2.000";
mountID = "5";
move_state = "0";
};
};
01/03/2008 (2:05 pm)
Ok gotcha.I see it at the bottom.Funny thing, there are still some variables in there that I have removed.Man I hope its not that I have to completely shut down TGB after script changes because i've just been reloading the project.levelContent = new t2dSceneGraph() {
canSaveDynamicFields = "1";
UseLayerSorting = "1";
layerSortMode0 = "Normal";
layerSortMode1 = "Normal";
layerSortMode2 = "Normal";
layerSortMode3 = "Normal";
layerSortMode4 = "Normal";
layerSortMode5 = "Normal";
layerSortMode6 = "Normal";
layerSortMode7 = "Normal";
layerSortMode8 = "Normal";
layerSortMode9 = "Normal";
layerSortMode10 = "Normal";
layerSortMode11 = "Normal";
layerSortMode12 = "Normal";
layerSortMode13 = "Normal";
layerSortMode14 = "Normal";
layerSortMode15 = "Normal";
layerSortMode16 = "Normal";
layerSortMode17 = "Normal";
layerSortMode18 = "Normal";
layerSortMode19 = "Normal";
layerSortMode20 = "Normal";
layerSortMode21 = "Normal";
layerSortMode22 = "Normal";
layerSortMode23 = "Normal";
layerSortMode24 = "Normal";
layerSortMode25 = "Normal";
layerSortMode26 = "Normal";
layerSortMode27 = "Normal";
layerSortMode28 = "Normal";
layerSortMode29 = "Normal";
layerSortMode30 = "Normal";
layerSortMode31 = "Normal";
cameraPosition = "0 0";
cameraSize = "100 75";
new t2dStaticSprite(triangle) {
imageMap = "TriangleImageMap";
frame = "0";
canSaveDynamicFields = "1";
class = "shape_class";
Position = "0.430 -15.774";
size = "8.000 8.000";
mountID = "2";
triangle_value = "5";
};
new t2dStaticSprite(square) {
imageMap = "SquareImageMap";
frame = "0";
canSaveDynamicFields = "1";
class = "shape_class";
Position = "-25.225 9.682";
size = "8.000 8.000";
mountID = "3";
square_value = "10";
};
new t2dStaticSprite(octogon) {
imageMap = "OctogonImageMap";
frame = "0";
canSaveDynamicFields = "1";
class = "shape_class";
Position = "24.939 10.043";
size = "8.000 8.000";
mountID = "4";
octogon_value = "15";
};
new t2dStaticSprite(ball) {
imageMap = "BallImageMap";
frame = "0";
canSaveDynamicFields = "1";
class = "ball_class";
Position = "27.174 -17.504";
size = "2.000 2.000";
mountID = "5";
move_state = "0";
};
};
#13
01/03/2008 (2:19 pm)
Ok, I had left an echo mouse position in my game.cs so thats why I was'nt getting the correct coordinace from my clicks.Now its giving me the clicked objects position like it should.Still says can't find ball object though.
#14
ball.cs
shape.cs
01/03/2008 (2:51 pm)
I'm pretty busy at the moment, but this is what I'd recommend:ball.cs
function ball_class::onLevelLoaded(%this,%scenegraph)
{
$ball = %this;
%this.setLayer(0);
}
function ball_class::dothis(%this, %target)
{
echo("Ball echoing" SPC %target.position);
ball.setPosition(%target.position);
}shape.cs
function shape_class::onLevelLoaded(%this,%scenegraph)
{
%this.setUseMouseEvents(true);
%this.setLayer(1);
}
function shape_class::onMouseDown(%this, %modifier, %worldPosition,%clicks)
{
echo("Shape echoing" SPC %this.position);
ball.dothis(%this);
}
#15
This does'nt help me with learning to pass arguments though...bugs me.
01/03/2008 (2:54 pm)
Well this works but it's not what i'm after.function shape_class::onMouseUp(%this,%modifier,%worldPosition,%clicks)
{
%pos=%this.getPosition();
ball.setPosition(%pos);
echo(%pos);
}So i guess I really don't need ANY script for the ball...just have it in the scenegraph right?This does'nt help me with learning to pass arguments though...bugs me.
#16
01/03/2008 (3:04 pm)
Did you post that about the same time as I was typing? The example I just gave should work and it uses the ball.dothis() function to do it.
#17
01/03/2008 (3:05 pm)
Doing it now.
#18
I also had to delete all of the .dso files and shut down TGB to get the console to shutup.Weird?Guess it had to recompile them.I'm going to look at what you have and see if I can follow it.Thanx for taking the time to help me out.
01/03/2008 (3:36 pm)
Works great.I'm getting some strange coordinaces though.Things like .0925?I'm running the level @ 800x600.I also had to delete all of the .dso files and shut down TGB to get the console to shutup.Weird?Guess it had to recompile them.I'm going to look at what you have and see if I can follow it.Thanx for taking the time to help me out.
#19
01/03/2008 (4:03 pm)
The coordinate system isn't in pixels, that comes from your camera size in the .t2d file.
#20
01/03/2008 (4:16 pm)
Oh...ok.I think I understand what you have done.Also I have'nt seen anywhere in the reference doc that says you can get an objects position parameter with just its name.position.But it works so thats fine I suppose.Oh, and this "ball.dothis(%this);" works the same as "ball.dothis(%this,%this);.So the first %this in the function is always a reference to the class instance that called it right?Is that why (%this) will work the same as (%this,%this) because %this passes all the built-in parameters of the object?Anyway,this has helped me ALOT.Thanx Jason.
Torque Owner Russell Buxton