Do I have to build the T2DTriggerComponent from scratch? - RESOLVED
by Randy Lutcavich · in Torque X 2D · 08/10/2009 (6:26 am) · 16 replies
So I figured out that I can add a blank scene object and then assign it the T2Dtriggercomponent but how do I access it from within my code? I noticed the MovementComponent.cs is in the project files from the begininng but the TriggerComponent.cs file never gets added. I can create a new component and possibly search for the sceneobject I made and work it out that way but doesn't that kind of defeat the purpose of components?
How do I load an basic T2DTriggerComponent with the OnEnter, OnStay, and OnLeave?
How do I load an basic T2DTriggerComponent with the OnEnter, OnStay, and OnLeave?
About the author
Recent Threads
#2
How would I set this up myself? I just don't see in your code where it is grabbing the sceneobject with the triggercomponent attatched. I guess I need to look in to delegates again... that's where I got stuck in John K's book.
Thanks for helping out. I'll post more when I give this a try at home.
08/10/2009 (12:06 pm)
So your code above will call the shootHim method when the player enters the trigger?How would I set this up myself? I just don't see in your code where it is grabbing the sceneobject with the triggercomponent attatched. I guess I need to look in to delegates again... that's where I got stuck in John K's book.
Thanks for helping out. I'll post more when I give this a try at home.
#3
08/10/2009 (1:55 pm)
Put Henry's code in any of your own components. Build your project in VS then when you go back to the TorqueX Builder it should ask you if you want to reload the component schema. Now, when you go to your T2DTiggerComponent, you should see your new OnEnter, OnStay, OnLeave delegates available in the drop down list.
#4
08/10/2009 (7:22 pm)
Hmm, Henry's code appears to be calling the shootHim method on boot. If I pop the player's inputMap in this method like I had planned, the player will lose all functionality on boot.
#5
I'll add the code to the next comment...
08/10/2009 (11:20 pm)
I started a the movementComponent file all over again and built the T2DtriggerComponent in this fresh CS file to make sure everything is nice and neat. I'm using the code from page 98 of John K's book. I'm assuming the delegate signature (at the top of page 98) is already somewhere deep within the engine so I am not using that line but everything else is in this CS file.I'll add the code to the next comment...
#6
08/10/2009 (11:21 pm)
namespace StarterGame2D
{
[TorqueXmlSchemaType]
[TorqueXmlSchemaDependency(Type = typeof(T2DPhysicsComponent))]
public class MovementComponent : TorqueComponent, ITickObject
{
//======================================================
#region Constructors
#endregion
//======================================================
#region Public properties, operators, constants, and enums
public int PlayerNumber
{
get { return _playerNumber; }
set { _playerNumber = value; }
}
#endregion
//======================================================
#region Public Methods
public void InterpolateTick(float k)
{
}
public void ProcessTick(Move move, float elapsed)
{
if (move != null)
{
// set our test object's Velocity based on stick/keyboard input
_sceneObject.Physics.VelocityX = move.Sticks[0].X * 20.0f;
_sceneObject.Physics.VelocityY = -move.Sticks[0].Y * 20.0f;
}
}
#endregion
//======================================================
#region Private, protected, internal methods
protected override bool _OnRegister(TorqueObject owner)
{
if (!base._OnRegister(owner) || !(Owner is T2DSceneObject))
return false;
// retain a reference to this component's owner object
_sceneObject = owner as T2DSceneObject;
_SetupInputMap(_sceneObject, _playerNumber, "gamepad" + _playerNumber, "keyboard");
// tell the process list to notifiy us with ProcessTick and InterpolateTick events
ProcessList.Instance.AddTickCallback(Owner, this);
return true;
}
void _OnBackButton(float val)
{
if (val > 0.0f)
Game.Instance.Exit();
}
private void _SetupInputMap(TorqueObject player, int playerIndex, String gamePad, String keyboard)
{
// Set player as the controllable object
PlayerManager.Instance.GetPlayer(playerIndex).ControlObject = player;
// Get input map for this player and configure it
InputMap inputMap = PlayerManager.Instance.GetPlayer(playerIndex).InputMap;
int gamepadId = InputManager.Instance.FindDevice(gamePad);
if (gamepadId >= 0)
{
inputMap.BindMove(gamepadId, (int)XGamePadDevice.GamePadObjects.LeftThumbX, MoveMapTypes.StickAnalogHorizontal, 0);
inputMap.BindMove(gamepadId, (int)XGamePadDevice.GamePadObjects.LeftThumbY, MoveMapTypes.StickAnalogVertical, 0);
inputMap.BindAction(gamepadId, (int)XGamePadDevice.GamePadObjects.Back, _OnBackButton);
}
// keyboard controls
int keyboardId = InputManager.Instance.FindDevice(keyboard);
if (keyboardId >= 0)
{
inputMap.BindMove(keyboardId, (int)Keys.Right, MoveMapTypes.StickDigitalRight, 0);
inputMap.BindMove(keyboardId, (int)Keys.Left, MoveMapTypes.StickDigitalLeft, 0);
inputMap.BindMove(keyboardId, (int)Keys.Up, MoveMapTypes.StickDigitalUp, 0);
inputMap.BindMove(keyboardId, (int)Keys.Down, MoveMapTypes.StickDigitalDown, 0);
// WASD
inputMap.BindMove(keyboardId, (int)Keys.D, MoveMapTypes.StickDigitalRight, 0);
inputMap.BindMove(keyboardId, (int)Keys.A, MoveMapTypes.StickDigitalLeft, 0);
inputMap.BindMove(keyboardId, (int)Keys.W, MoveMapTypes.StickDigitalUp, 0);
inputMap.BindMove(keyboardId, (int)Keys.S, MoveMapTypes.StickDigitalDown, 0);
T2DTriggerComponentOnEnterDelegate enterHandler = new T2DTriggerComponentOnEnterDelegate(popTileMap.tileMap1);
//call the delegate method
enterHandler(_towerBase, _playerObj);
}
}
#endregion
//======================================================
#region Private, protected, internal fields
T2DSceneObject _sceneObject;
int _playerNumber = 0;
T2DSceneObject _towerBase = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("towerBase");
T2DSceneObject _playerObj = TorqueObjectDatabase.Instance.FindObject<T2DStaticSprite>("player");
#endregion
}
class popTileMap
{
//implement the delegate handler
public static void tileMap1(T2DSceneObject a, T2DSceneObject b)
{
Console.WriteLine("Player has Entered");
return;
}
}
}
#7
and
and
I add a flag to the 'Console.WriteLine("Player has Entered");' line but it never pops when debugging and then running the player into the trigger.
I am I just going about this all wrong? Shouldn't this code from the book work? Isn't this how delegates work??
08/10/2009 (11:24 pm)
The code in the above file that I added was:T2DTriggerComponentOnEnterDelegate enterHandler = new T2DTriggerComponentOnEnterDelegate(popTileMap.tileMap1);
//call the delegate method
enterHandler(_towerBase, _playerObj);
}and
T2DSceneObject _towerBase = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("towerBase");
T2DSceneObject _playerObj = TorqueObjectDatabase.Instance.FindObject<T2DStaticSprite>("player");
#endregionand
class popTileMap
{
//implement the delegate handler
public static void tileMap1(T2DSceneObject a, T2DSceneObject b)
{
Console.WriteLine("Player has Entered");
return;
}
}
}I add a flag to the 'Console.WriteLine("Player has Entered");' line but it never pops when debugging and then running the player into the trigger.
I am I just going about this all wrong? Shouldn't this code from the book work? Isn't this how delegates work??
#8
I haven't tried to implement the trigger delegate like you have, and John shows in the book. I've done the same thing as the code that Henry provided. Put them in your component. Then in the builder add the trigger component to one of your objects. Set it to collide with the type of object that you want. Set the OnEnter method to call in the drop down list (this will be the delegate you exposed with the [TorqueXmlSchemaType] attribute). Now, run your game and when the object collides with your object with the trigger it will then call your delegate and underlying method.
I don't know what you mean by "on boot" in your previous posts.
08/10/2009 (11:31 pm)
Rudy,I haven't tried to implement the trigger delegate like you have, and John shows in the book. I've done the same thing as the code that Henry provided. Put them in your component. Then in the builder add the trigger component to one of your objects. Set it to collide with the type of object that you want. Set the OnEnter method to call in the drop down list (this will be the delegate you exposed with the [TorqueXmlSchemaType] attribute). Now, run your game and when the object collides with your object with the trigger it will then call your delegate and underlying method.
I don't know what you mean by "on boot" in your previous posts.
#9
Unfortunately I'm still not getting it to work but atleast now I feel like I'm on the same page as you two.
I'll try again tomorrow after some sleep.
Please disregard the post about the "on boot". That issue is no longer occurring.
If either one of you has the full component code for when you used a T2DTriggerComponent delegate, that would be very helpful to me. Right now, I feel like I'm just making a simple mistake somewhere along the line but I can't find out where.
08/11/2009 (1:24 am)
Thank you both... I read the next chapter and discovered the method you two were telling me to use.Unfortunately I'm still not getting it to work but atleast now I feel like I'm on the same page as you two.
I'll try again tomorrow after some sleep.
Please disregard the post about the "on boot". That issue is no longer occurring.
If either one of you has the full component code for when you used a T2DTriggerComponent delegate, that would be very helpful to me. Right now, I feel like I'm just making a simple mistake somewhere along the line but I can't find out where.
#10
I have the same problem with the tutorial as I do with my custom code that I am making to create a trigger. The game just doesn't do anything onEnter or onLeave. It's like the objects aren't registering when they enter the trigger.
Is this a problem with the latest XNA or am I missing something?
08/14/2009 (8:17 pm)
So I thought my questions were answered when I found the Rainy Day Tutorial for TX2D but nope. I have the same problem with the tutorial as I do with my custom code that I am making to create a trigger. The game just doesn't do anything onEnter or onLeave. It's like the objects aren't registering when they enter the trigger.
Is this a problem with the latest XNA or am I missing something?
#11
In your trigger component in builder, set the OnEnter to your delegate method that should be listed in the drop down list. Also, be sure to create "object types" for your two objects. Set your trigger object to collide with the opposite object that the trigger is not attached to.
This is as simple as I can explain it as this works for me with no problems.
08/14/2009 (9:10 pm)
I would suggest starting with a brand new project. Add two images to your project and then drop them in your scene. Add a trigger component to one of them. Add your OnEnter delegate and method to the default movement component (copy Henry's code from beginning of this thread). In the method have it do nothing - simply set a break point on it so you can confirm if it was fired or not.In your trigger component in builder, set the OnEnter to your delegate method that should be listed in the drop down list. Also, be sure to create "object types" for your two objects. Set your trigger object to collide with the opposite object that the trigger is not attached to.
This is as simple as I can explain it as this works for me with no problems.
#12
08/14/2009 (10:27 pm)
I tried as you suggested and succeeded in getting the trigger to fire with two sprites but I'm still unable to get this working in the RainyDay tutorial or my code. I understand that this should be very simple but for some reason it just isn't working for me.
#13
Here's my code:
The shootHim method is used to pop the player's inputmap. But this method is being run as soon as I start debugging. This causes the player to lose all functionality as soon as the game starts (aka on boot). Is there anyway around this or does every method get run when starting the game?? Why is the method running if it hasn't been called yet?? This makes no sense to me.
08/16/2009 (7:54 pm)
Here's the deal. I've started over and cleaned up all my code. I'm using Henry's code exactly and it works with one exception... the ShootHim method is being called once on boot.Here's my code:
[TorqueXmlSchemaType]
public static T2DTriggerComponentOnEnterDelegate NME_Enter
{
get { return shootHim; }
}
public static void shootHim(T2DSceneObject ourObject, T2DSceneObject theirObject)
{
InputMap inputMap = PlayerManager.Instance.GetPlayer(0).InputMap;
InputManager.Instance.PopInputMap(inputMap);
}The shootHim method is used to pop the player's inputmap. But this method is being run as soon as I start debugging. This causes the player to lose all functionality as soon as the game starts (aka on boot). Is there anyway around this or does every method get run when starting the game?? Why is the method running if it hasn't been called yet?? This makes no sense to me.
#14
If so, how is this supposed to work? Triggers do not seem to be the way to go. The whole point of a trigger is for it to do an action when the player trigger it not when the game loads up. :(
08/17/2009 (12:18 am)
Is there anyway I can get you to try this and add a break point to see if your method is also getting called on boot?If so, how is this supposed to work? Triggers do not seem to be the way to go. The whole point of a trigger is for it to do an action when the player trigger it not when the game loads up. :(
#15
08/17/2009 (12:49 am)
Randy, no my break point only hits when the trigger is fired as I set it up...not when the game is started. Either you are calling the method explicitly someplace in your code or your trigger is firing as soon as you start because of the location of what ever objects yo uhave set up with it.
#16
Thank you!
Thank you!
Thank you!
08/17/2009 (1:18 am)
I just had to specify which objects trigger the onEnter call.Thank you!
Thank you!
Thank you!
Torque 3D Owner Henry Shilling
Smokin Skull
You need to expose the delegate:
[TorqueXmlSchemaType] public static T2DTriggerComponentOnEnterDelegate NME_Enter { get { return shootHim; } }then create the delegate code:
public static void shootHim(T2DSceneObject ourObject, T2DSceneObject theirObject) { float angle = T2DVectorUtil.AngleFromTarget(ourObject.Position, theirObject.Position); ourObject.doTheShooting(withBigGuns); }This is very similar to creating a collision delegate, however it has different arguments.
Hope that helps.