Call a function from another class.
by StandardFace · in Torque Game Builder · 06/01/2009 (6:28 pm) · 7 replies
Hi,
I want to call a function in script and have everything that is a certain class do that action. Like this, even though I know this doesn't work (that's the problem).
function t2dSceneGraph::onUpdateScene( %this )
{
if($timer == 0)
{
echo(" Time is up!");
%this.enemyShip.spawn(); <-- this is where i want to call it. Have the enemy ship run a function!
}
}
Like for instance, I want an object to check to see if a global variable is zero (a timer) and when the timer is up, I want all bricks to explode. Would I use onSceneUpdate() for something like that?
-SF
I want to call a function in script and have everything that is a certain class do that action. Like this, even though I know this doesn't work (that's the problem).
function t2dSceneGraph::onUpdateScene( %this )
{
if($timer == 0)
{
echo(" Time is up!");
%this.enemyShip.spawn(); <-- this is where i want to call it. Have the enemy ship run a function!
}
}
Like for instance, I want an object to check to see if a global variable is zero (a timer) and when the timer is up, I want all bricks to explode. Would I use onSceneUpdate() for something like that?
-SF
About the author
#2
-SF
06/01/2009 (6:49 pm)
Edit: I found information in schedule() I will look into it. Thank you, I'm sure I'll have questions soon. -SF
#3
i see a problem in your code...
in that code, the line invoking the spawn function is not really referencing an object of the enemyShip class.
it should be something like:
i say this, cuz i think that since you're mixing different objects there, it will raise a runtime error, that might be hard to track down when you expect your enemy to show up but it doesnt.
06/01/2009 (9:54 pm)
helloi see a problem in your code...
function t2dSceneGraph::onUpdateScene( %this )
{
if($timer == 0)
{
echo(" Time is up!");
%this.enemyShip.spawn();
}
}in that code, the line invoking the spawn function is not really referencing an object of the enemyShip class.
it should be something like:
%enemyObj_from_enemyShip_class.spawn();
i say this, cuz i think that since you're mixing different objects there, it will raise a runtime error, that might be hard to track down when you expect your enemy to show up but it doesnt.
#4
If my code looks exactly like this:
I have objects of the class 'brick' in my level. They have a function called 'moveBrick' but I keep getting "Unable to find object '' attempting to call function moveBrick"
I just want to make one function (onUpdateScene) call a function that uses another object and I can't seem to do that. I want to apply this elsewhere in my code to other things, which is why schedule() is useful but not in all cases.
Thank you all for the replies.
EDIT: I know that %brick would be a local variable not in the scope of this function, but since I have 40 bricks, I need to call this function for each brick, not just one global brick variable.
EDIT2: Ok, so here:
This code moves my brick but it only moves one. I have made about 40 or so of them in the level builder. I basically want to name all of the bricks and have them move. Any help would be appreciated.
06/02/2009 (5:37 pm)
Thanks for the help but I still can't seem to get it working.If my code looks exactly like this:
$timer = 0;
function t2dSceneGraph::onUpdateScene( %this )
{
if($timer == 0)
{
echo(" Time is up!");
%brick.moveBrick();
}
}I have objects of the class 'brick' in my level. They have a function called 'moveBrick' but I keep getting "Unable to find object '' attempting to call function moveBrick"
I just want to make one function (onUpdateScene) call a function that uses another object and I can't seem to do that. I want to apply this elsewhere in my code to other things, which is why schedule() is useful but not in all cases.
Thank you all for the replies.
EDIT: I know that %brick would be a local variable not in the scope of this function, but since I have 40 bricks, I need to call this function for each brick, not just one global brick variable.
EDIT2: Ok, so here:
function brick::onLevelLoaded(%this)
{
%this.setName(redBrick);
}
function t2dSceneGraph::onUpdateScene( %this )
{
redBrick.moveBrick();
}This code moves my brick but it only moves one. I have made about 40 or so of them in the level builder. I basically want to name all of the bricks and have them move. Any help would be appreciated.
#5
06/04/2009 (12:41 am)
Something like this might work.function brick::onLevelLoaded(%this)
{
%this.setName(redBrick); //All bricks will be named redBrick
%this.moveBrick(); //Calls moveBrick() for each object with the class of 'brick'
}
function brick::moveBrick(%this)
{
//Code that moves the bricks
}
#6
%this.setName(redBrick) is that it will only work with the 1st object that gets named that way... since you are not using unique names here.
i think the way to go here, is by passing parameters or something, so you have the reuntime ID of the object you wanna work on, and that usually wont fail to invoque the right function or whatever that can use that object's class.
06/05/2009 (10:24 pm)
the problem with this:%this.setName(redBrick) is that it will only work with the 1st object that gets named that way... since you are not using unique names here.
i think the way to go here, is by passing parameters or something, so you have the reuntime ID of the object you wanna work on, and that usually wont fail to invoque the right function or whatever that can use that object's class.
#7
Hope that helps a little.
06/07/2009 (10:14 pm)
You could try to while on creation of the redbricks add the particular object to a simObject list and when the timer is up, run a function that goes down the list and does the method you want to each object down the list. Make sure you remove the object from the list once you run the method. Hope that helps a little.
Torque Owner Netwyrm
Canopic Games