moveTo not working
by rennie moffat · in Torque Game Builder · 02/03/2010 (11:47 am) · 16 replies
HI guys I am using the moveTo in a behavior. but it is not working.
I have this...
I have named the player, player in the editor name area.
:?
I have this...
if(!isObject(tile))
{
%template = new BehaviorTemplate(tile);
%template.friendlyName = "tile";
%template.description = "tells player to move to tile on mouse up";
%template.behaviorType = "input";
%template.addBehaviorField(player, "object which moves to selected tile", object, "", t2dSceneObject);
%template.addBehaviorField(playerSpeed, "speed of player", float, 30);
}
function tile::onBehaviorAdd(%this)
{
%this.owner.setUseMouseEvents(true);
}
function tile::onMouseUp(%this, %mod, %worldPos, %mouseClicks)
{
%this.player.moveTo(getWord(%this.owner.getPosition, 0), getWord(%this.owner.getPosition, 1), %this.playerSpeed, true, true, true, 0.1);
}I have named the player, player in the editor name area.
:?
About the author
My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.
#2
02/03/2010 (3:26 pm)
well the idea is, onMouseUp, over the %this.owner, will trigger %this.player to moveTo %this.owner.
#3
Note you can pass the X and Y values together with getPosition(), but if you need the X or Y values on their own you can use getPositionX() and getPositionY() rather than getWord(). Finally, you've put a snap of 0.1 on the end of your move, are you sure you want that?
I recommend you read the torque reference documentation so that you get a better vocabulary of functions - you're making things hard for yourself by not using simpler functions.
02/07/2010 (2:12 am)
This should work:player.moveTo(%this.owner.getPosition(), player.speed, true, true, true, 0.1);
Note you can pass the X and Y values together with getPosition(), but if you need the X or Y values on their own you can use getPositionX() and getPositionY() rather than getWord(). Finally, you've put a snap of 0.1 on the end of your move, are you sure you want that?
I recommend you read the torque reference documentation so that you get a better vocabulary of functions - you're making things hard for yourself by not using simpler functions.
#4
NO?
A simple ex
behaviorField(ThisThing, "", object, "", t2dSceneObject);
behaviorField(velocity, "", float, 0);
can be called later like this...
thisThingy.setLinearVelocityX(velocity)
02/07/2010 (5:58 pm)
My understanding going into this was that anything declared as a behaviorField was called by %this.NO?
A simple ex
behaviorField(ThisThing, "", object, "", t2dSceneObject);
behaviorField(velocity, "", float, 0);
can be called later like this...
thisThingy.setLinearVelocityX(velocity)
#5
%this refers to the behavior.
%this.owner refers to the object that the behavior is attached to.
So, in a behavior, if you call %this,setLinearVelocity(%xVel) it's not going to work because the behavior doesn't occupy a location in the scenegraph like the object that the behavior is attached to does.
So why use%this?
Using your example, you would do something like:
Do you see the difference? The object you want to move is the thingy that your behavior is attached to, so we call %this.owner.setLinearVelocityX().
How fast do you want it to move? You set it as a behavior field so you use %this.velocity.
Capiche?
02/08/2010 (10:10 am)
When using behaviors, there's one rule that you need to be clear on:%this refers to the behavior.
%this.owner refers to the object that the behavior is attached to.
So, in a behavior, if you call %this,setLinearVelocity(%xVel) it's not going to work because the behavior doesn't occupy a location in the scenegraph like the object that the behavior is attached to does.
So why use%this?
Using your example, you would do something like:
behaviorField(ThisThing, "", object, "", t2dSceneObject); behaviorField(velocity, "", float, 0); %this.owner.setLinearVelocityX(%this.velocity)
Do you see the difference? The object you want to move is the thingy that your behavior is attached to, so we call %this.owner.setLinearVelocityX().
How fast do you want it to move? You set it as a behavior field so you use %this.velocity.
Capiche?
#6
I get that full on.
where I am doubting myself now is that above, @Connor said that %this was not needed to call a behaviorfield (player).
So in order to call that object (which it is) I did not need to use %this.player, just player.
Do You see what I am saying? I was actually just about to post on this so thanks.
02/08/2010 (10:30 am)
Yah for sure. I get that full on.
where I am doubting myself now is that above, @Connor said that %this was not needed to call a behaviorfield (player).
So in order to call that object (which it is) I did not need to use %this.player, just player.
Do You see what I am saying? I was actually just about to post on this so thanks.
#7
02/08/2010 (10:47 am)
Looks to me like Connor is assuming you named the object 'player', in which case %this.owner would not be required but also breaks the behavior if you attach it to an object that is not named 'player'.
#8
This whole %this is not nesessacary is what is gettng me.
02/08/2010 (10:50 am)
I understand that fully.This whole %this is not nesessacary is what is gettng me.
#9
If you have an object named 'player' and you have a behavior attached to that object then:
player.setLinearVelocityX(5.0);
and
%this.owner.setLinearVelocityX(5.0);
will do the same thing.
02/08/2010 (11:08 am)
%this is not necessary when you refer to an object by name.If you have an object named 'player' and you have a behavior attached to that object then:
player.setLinearVelocityX(5.0);
and
%this.owner.setLinearVelocityX(5.0);
will do the same thing.
#10
i can just forego %this OR just don't use %this at all when referring to an object.
02/08/2010 (11:17 am)
ok so lets just say i have multiple objects which i have named in the editor.i can just forego %this OR just don't use %this at all when referring to an object.
#11
so when referring to an object in a behavior (and in script) a name, the name written in the editor can be used straight up with no %this. and that is because %this refers to the class. and if the object is in the behavior it is already known to belong to that class, or behavior?
02/08/2010 (11:29 am)
cause it just seemed i was being drilled with use %this.so when referring to an object in a behavior (and in script) a name, the name written in the editor can be used straight up with no %this. and that is because %this refers to the class. and if the object is in the behavior it is already known to belong to that class, or behavior?
#12
Yes, you can use it that way but you shouldn't. There's no point in having a behavior if it's tied to a single object by name. Look at it this way... You have a behavior on an object and you have a built in reference to that object by using %this.owner. It's there because it will always execute code on the object the behavior is attached to. If you put code in there that executes from the object name and add the behavior to another object in your game, it will have an effect on the named object and you'll get unexpected results or an error.
No, %this does not refer to the class. %this refers to the owner of the code being executed. Again, it refers to either the object (non-behavior) or the behavior.
02/08/2010 (2:14 pm)
Quote:
so when referring to an object in a behavior (and in script) a name, the name written in the editor can be used straight up with no %this.
Yes, you can use it that way but you shouldn't. There's no point in having a behavior if it's tied to a single object by name. Look at it this way... You have a behavior on an object and you have a built in reference to that object by using %this.owner. It's there because it will always execute code on the object the behavior is attached to. If you put code in there that executes from the object name and add the behavior to another object in your game, it will have an effect on the named object and you'll get unexpected results or an error.
Quote:
and that is because %this refers to the class
No, %this does not refer to the class. %this refers to the owner of the code being executed. Again, it refers to either the object (non-behavior) or the behavior.
#13
I know this.owner etc. I have seen countless examples using this. Even I have used %this with great results on an object. I believe I have no problems with this.
02/08/2010 (2:29 pm)
this is stupid. I know this.owner etc. I have seen countless examples using this. Even I have used %this with great results on an object. I believe I have no problems with this.
#14
thanks
02/08/2010 (2:30 pm)
if you can, look at this and tell me why it is not working.thanks
if(!isObject(starRejenerator))
{
%template = new BehaviorTemplate(starRejenerator);
%template.friendlyName = "star rejenerator";
%template.description = "adds new star to scene when one is dropped";
%template.behaviorType = "AI";
%template.addBehaviorField(starsStar1, "object behavior must clone", object, "", t2dSceneObject);
%template.addBehaviorField(starsStar2, "object behavior must clone", object, "", t2dSceneObject);
%template.addBehaviorField(starsStarLoaderPosX, "", float, 15);
%template.addBehaviorField(starsStarLoaderPosY, "", float, 15);
}
function starRejenerator::onBehaviorAdd(%this)
{
%this.owner.enableUpdateCallback();
}
function starRejenerator::onUpdate(%this)
{
%this.updateLoader();
}
function starRejenerator::updateLoader(%this)
{
%starsStar1Pos = starsStar1.getPosition();
%starsStar2Pos = starsStar2.getPosition();
if(%starsStar1Pos != %this.owner.getPosition())
starsStar2.setPosition(%this.starsStarLoaderPosX, %this.starsStarLoaderPosY);
if(%starsStar2Pos != %this.owner.getPosition())
starsStar2.setPosition(%this.starsStarLoaderPosX, %this.starsStarLoaderPosY);
}
#15
stupid?
02/08/2010 (6:29 pm)
Although I don't agree with the way you coded that behavior, it should work in this case if you use the vector comparison method instead of the != here:if(%starsStar1Pos != %this.owner.getPosition())
starsStar2.setPosition(%this.starsStarLoaderPosX, %this.starsStarLoaderPosY);
if(%starsStar2Pos != %this.owner.getPosition())
{
starsStar2.setPosition(%this.starsStarLoaderPosX, %this.starsStarLoaderPosY);
}to:if(!t2dVectorCompare(starsStar1.getPosition() , starsStar2.getPosition() , 0.01)
{
starsStar2.setPosition(%this.starsStarLoaderPosX, %this.starsStarLoaderPosY);
}
if(!t2dVectorCompare(starsStar2.getPosition() , %this.owner.getPosition() , 0.01)
starsStar2.setPosition(%this.starsStarLoaderPosX, %this.starsStarLoaderPosY);stupid?
#16
Hmm.
ok.
thanks
02/08/2010 (7:26 pm)
so basically you are saying if this vetocr does not equal that vector place X here.Hmm.
ok.
thanks
Torque 3D Owner Marc Dreamora Schaerer
Gayasoft