Game Development Community

Calling object from function

by Mirko Topalski · in Torque Game Builder · 04/11/2007 (11:25 am) · 12 replies

Ok, so i write:

function t2dSceneWindow::onMouseDown(%this, %modifier, %worldPosition, %clicks){
moveObject(objectName);
}

function moveObject(%this){
%this.setLinearVelocityX(10);
}

And it doesnt work.
I tried many similar things also, but nothing works when i pass a object name to a function, even when it is in a form of string.

#1
04/11/2007 (2:01 pm)
You have to call the object by name or class. If don't reference the object, the variable %this doesn't make any sense. Your function should be function object::moveObject ( %this ).

EDIT: clarification
#2
04/11/2007 (2:07 pm)
@Ricardo: No, he can use %this if he wants. A bit confusing, but it should work. %this in this case would be the objectName he passed to the function. So, if that object exists, it should work.

@Mirko:
Is the code you posted above exactly the same as in your code? I mean "objectName", where is that coming from? Are you passing a string? Or?
If you do echo(objectName), or echo(%this) in the moveObject function, do you see a id number appear in the console?
#3
04/11/2007 (2:17 pm)
No, i pass : playerSword as objectName

I know it works fine in macromedia flash :)

i even tried with :
eval(%this.setLinearVelocityX(10));

I managed to make it work when i used global variables, but globals are out of the question here.
#4
04/11/2007 (2:34 pm)
And you are sure those 2 functions are called? Did you try putting and echo(); in them to make sure?
#5
04/11/2007 (3:07 pm)
Of corse
#6
04/11/2007 (3:47 pm)
I had no idea that it would work, but it makes sense. Still, I believe that simply give it a class and call it would make things easier and neat.
#7
04/11/2007 (4:44 pm)
Might just be a typo from when you cut and paste but
moveObject(objectName);
should be
moveObject($objectName);  // assuming it's a global
I know you say you're not using globals, but I'm not sure what else "objectName" could be.
#8
04/12/2007 (8:17 am)
Could you post your actual source scripts?
#9
04/12/2007 (10:33 pm)
The point is... my code is WAST and complicate. I want to make a function that makes objects move on some complicated algoritam (beside the object name, you pass to a function 6 parameters more)

Want i am trying to acheive, is to make a function that will make objects move, and all i have to do, is to pass object's ID or name to a function (+6 other parameters) to make them be recognised in the function. The function is repetable every X miliseconds by Y times. So, many instances of the same function can be running @ the same time. Thats why i dont want to use globals.
#10
04/13/2007 (1:08 am)
Ok! I have a similar thing in my current project, let's see if I can explain it.

Every object has a superclass "object". It can be a class in my case all objects use superclass because diferente class functions for object type are needed. Somewhere around the code the objects are created. So I have a function that creates the object:

function createObject ()
{
     %object = new t2StaticSprite {
          // construction stuff here

          class = "object"; // If you have the objects with a class, use a superclass
     };

     %object.updateEventID = %object.schedule ( 100, "update", "%six, %arguments, %here" );
}

And the update function

function object::update ( %this, %six, %arguments, %here )
{
     // Do whatever weird stuff you want

     %this.updateEventID = %this.schedule ( 100, "update", "@six, @arguments, @here" );
}

I wrote the code directly so there may be some sintax errors, but I use this to update up to 90 objects in the game and it works sweet.

Hope this helps.

V

EDIT: missed arguments in function
#11
04/13/2007 (1:09 am)
Mirko: .. yah. so we're trying to figure out why the code you posted at the top isn't working. it looks like it should work, but something's not right. you say you've added debug prints to the code and Mat Langley has asked if you can post the exact source code in question, and if you did that we could probably help you out a bit more.

as with any debugging process, if you can reduce the problem down to the minimal set of issues, then it's easier for other folks to help figure out what's going wrong.
#12
04/14/2007 (9:16 am)
Here is some simplified code of my function. On my comp it doesn't work, but on my friend's comp it does. (??) You only nead one object called "sword" to try this code.


t2dSceneWindow::setUseMouseEvents(true);

function t2dSceneWindow::onMouseDown(%this, %modifier, %worldPosition, %clicks){
MotionTween(sword, 10, 1, 100);
//or MotionTween("sword", 10, 1, 100);
}

function MotionTween(%this, %speed, %faktor, %interval){
%this.setLinearVelocityX(%speed);
if (%speed > 0){
%speed -= %faktor;
schedule(%interval, %this, "MotionTween", %this, %speed, %faktor, %interval);
}
else {
%this.setLinearVelocityX(0);
}
}