Game Development Community

Using component for multiple objects

by rwillis · in Torque X 2D · 05/12/2007 (9:18 am) · 3 replies

I have a player object and enemy object on screen, but when I attach the movement component to both of them only one of them move. When I put Debug.writeline(owner.Name) I get the debug message for both of them with both their names showing up. How do I make it work? Thanks ahead..

#1
05/12/2007 (4:01 pm)
Theres alot of ways to make that work...

Whats happening is this...

The movement component is "assigning" its owner object, as "player1" this is how your player1 object "recieves input"

in your scenario two objects have the same component... and are both trying to get player 1 input...

You hit a button...

Your input is routed to the input manager, and magically, the input manager finds an object which will handle your input! Now whether it is object 1 or object 2 that recieves the input, I can't tell you, because i don't know which the input manager found first....

However, I can tell you this, the second object will NOT recieve input for that same button push.. that button push has been handled. Read the TorqueX documentation, I'm pretty sure it covers this.

Oh.. as a side note...

Another reason your input isn't handled is because there can only be one "player1" object.

You have to find another way.. it shouldn't be that diffult, I hope that answers your question.

Heres one...

Remove the movement component from object 2

Rename object2 "yourObject"

write this code into you movement component processtick
T2DSceneObject _object2 = (T2DSceneObject)TorqueObjectDatabase.Instance.FindObject("yourObject");
_object2.Physics.Velocity = _sceneObject.Phyiscs.Velocity;
#2
05/12/2007 (5:03 pm)
If you just want the enemy to move as the player moves than Modern1s code will do just that, Im guessing though that this is just a starting point for more complex behaviours? If so:

As Modern1 says:

The movements are assigned to one particular TorqueObject, so for the Enemy to use the same inputs to decide its movements you should make another component (Eg EnemyAIComponent) that finds the Player and queries the various values in its Physics component to find what its doing, then act accordingly. You could also add fields to the MoveComponent to provide the enemy with more detail on what buttons are being pressed.
#3
05/12/2007 (6:27 pm)
Okay I got it. Thanks very much.