Game Development Community

Functions and classes

by Richard Pettyjohn · in Torque Game Builder · 08/15/2006 (5:32 pm) · 20 replies

How do I call a function in a different class I'm calling it from?

#1
08/15/2006 (5:36 pm)
You should be able to call the function automatically. Just make sure you run the file containing the function in "game.cs". (ie exec("./myFunctionFile.cs");)
#2
08/15/2006 (9:21 pm)
It doesn't seem to work if I do that. It works for functions with no class but if the function I'm calling has a class I get an error in the console telling me it was unable to find the function I was trying to call. Here is an example of what I want:

function exampleClassOne::exampleOne()
{
//code to call exampleTwo
}

function exampleClassTwo::exampleTwo()
{
//what I want done
}



By the way is it calling the function what I'm doing here or should I have used a different word like execute?
#3
08/15/2006 (10:53 pm)
How are you calling "exampleTwo()"?

To have it work, you either have to use "exampleClassTwo::exampleTwo()" or create an object that defines its class to be "exampleClassOne" and its superClass to be "exampleClassTwo" or vice versa.

This is because "exampleTwo()" is defined under the namespace of "exampleClassTwo" only. If you don't define a namespace (class), then it'd be in the global space where you can call it from any class.
#4
08/16/2006 (5:41 am)
When using functions with classes remember to include %this in the parameters.

EX:

function Class1::DoAction(%this)
{
      %this.mount(. . . . . .
}

%this refers to the instance that is calling the function. Also when calling a function with a object in a specific class you'll want to do something like so:

EX:

%this.DoAction();

Hope this helps!
#5
08/16/2006 (8:57 am)
I appritiate the help but I still sort of confused.


DooGoG:

So does this mean its not possible to call a function from a different class? Or did I miss underestand your post (sorry it was a little confusing)?

Kevin:

Is %this only for calling functions in the same class? Could I use something like: %exampleClassTwo.ExampleTwo
#6
08/16/2006 (10:16 am)
Richard:
To call a class function, there has to be an instance of that class in the level. From that point, it's easy enough to give that instance a name (either in the ::onLevelLoaded function or in the Level Builder), then call that instance's functions with name.function()

From your example, let's say you have an exampleClassTwo object in your scene named "bob":

function exampleClassOne::exampleOne()
{
bob.exampleTwo();
}

function exampleClassTwo::exampleTwo()
{
//what I want done
}


If you don't want to deal with naming, another way to do this is to assign a global variable to that object's instance in the onLevelLoaded function:

function exampleClassTwo::onLevelLoaded(blah blah blah)
{
$bob = %this;
}

From that point you can call that object's class functions with $bob.exampleTwo() and so forth.

If the function doesn't do anything specifically with the class you're assigning it to, consider making it a general function and not a class function.
#7
08/16/2006 (11:33 am)
Thanks for the help!

It makes much more sence to me now.
#8
08/16/2006 (2:16 pm)
If I have, say, bob1, bob2 and bob3 on the same class (exampleClassTwo), and I want to call a function that will work with the three of them ? not just for one object ?

(sorry for being so noobish, but the documentation is not very clear on this)

I tried this :

function exampleClassOne::exampleOne()
{
bob1.exampleTwo();
bob2.exampleTwo();
bob3.exampleTwo();
}

function exampleClassTwo::exampleTwo()
{
//what I want done
}

But it didnt work for any of them.
#9
08/16/2006 (2:43 pm)
How did you define the bob's class, and also how are you referencing the bob's? Are they globals?
#10
08/16/2006 (2:53 pm)
The Bobs are the names of each individual scene object. I simply defined their names and class in the level builder. Its surely not enough to get them working :/



I tried

function exampleClassTwo::onLevelLoaded(blah blah blah)
{
$bob1 = %this;
$bob2 = %this;
$bob3 = %this;
}

It works only with one of them (as i through), but i want my callback tu works with all of them.
#11
08/16/2006 (3:08 pm)
What you're doing right there is saying everytime an instance of "exampleClassTwo" gets loaded, define all the bob's to be that instance.

If you want to allow the bob's call functions from the namespace of "exampleClassTwo", you'd just have to assign the bob's class to be "exampleClassTwo".
#12
08/16/2006 (3:26 pm)
Quote:you'd just have to assign the bob's class to be "exampleClassTwo".

Well... I already did (in the level builder, bobs' class = "exampleClassTwo").


But when i try to run the code like that :
function exampleClassOne::exampleOne()
{
bob1.exampleTwo();
bob2.exampleTwo();
bob3.exampleTwo();
}

function exampleClassTwo::exampleTwo()
{
//what I want done
}

or
function exampleClassOne::exampleOne()
{
exampleClasseTwo.exampleTwo();
}

function exampleClassTwo::exampleTwo()
{
//what I want done
}

I get something like this on the console :
" unable to find object : ' ' attempting to call function 'exampletwo' "
#13
08/16/2006 (3:34 pm)
Oh, what are you doing in exampleTwo(). If you are referencing the object itself, you have to declare exampleTwo like so:

function exampleClassTwo::exampleTwo([b]%this[/b])
{
//what I want done
}

In the second code snippet, you can't just do "exampleClasseTwo.exampleTwo();" unless there's an object named exampleClassTwo. If you want to call it as a namespace, you have to use "exampleClassTwo::exampleTwo()"
#14
08/16/2006 (3:37 pm)
Well, the problem is that "exampleClasseTwo::exampleTwo" is not even called (I already done the (%this) thing).
#15
08/16/2006 (3:41 pm)
Could you paste the exact relevant code and console commands you're using?
#16
08/16/2006 (3:57 pm)
There is two objects (for now, I want to do something that allow me to add more objects easily), para1 and para2 who are under the class "parallax".

Here is the relevent code (its for multiple parallax backgrounds) :

In UpdateScreen.cs :
function t2dscenegraph::onUpdateScene(%this)
{
	$para1.update();
	$para2.update();
// or $parallax.update();
}
In parallax.cs :
// here is the onlevelloaded, which allow only one of the parallax to work if i activate it.
//function parallax::onlevelloaded(%this)
//{
//	$para1=%this;
//}


function parallax::update(%this)
{
	blahblah (working code, with %this everywhere and nothing else)
}

The matter is that parallax::update is not called at all if I dont do the "$para1(or 2)=%this" thing (which will make only one objects works).
#17
08/16/2006 (4:17 pm)
When you define object names in the levelbuilder, they are not really defined as global variables, so try using "para1.update(); para2.update()" instead.
#18
08/16/2006 (4:24 pm)
Ah ! I finally found how to get this working :

function t2dscenegraph::onUpdateScene(%this)
{
	$para1.update();
	$para2.update();
}



function para1::onlevelloaded(%this)
{
	$para1=%this;
}

function para2::onlevelloaded(%this)
{
	$para2=%this;
}




function parallax::update(%this)
{
       Blahblah
}

The solution is that
"
function para1::onlevelloaded(%this)
{
$para1=%this;
}
"
Not obvious at all to find out.
I found it while doing doing random code tests everywhere to see if its getting better or not.
It get magically the objects to be reconized (not only as objects, but as part of the parallax class too) XD

Thanks DooGoG for the help :)
#19
08/16/2006 (4:46 pm)
I'm pretty sure you don't have to do that, but if it's cool, then great!

Have you tried using just "para1.update()" and "para2.update()" instead of "$para" because object names defined in level builder are not really global variables.
#20
08/16/2006 (4:49 pm)
(Oups, just saw your post after)

Yes, it works, and its way better than my little useless "solution".

Thanks ^^