Game Development Community

Mounting in code via a component...

by Alex Loret de Mola · in Torque X 2D · 01/21/2010 (11:49 pm) · 10 replies

Hi everyone! I'm new to the world of Torque, and I'm trying out the Torque X 2D engine.

I've created a component which I want to use to give various entities in the game the ability to have an "aiming recticle" that circles around them, and a projectile that shoots from that angle.

To that end, I've created a component (that I call "RotatingAimComponent") exposes the following properties:

public float MinAngle (the lower bounds of how far the recticle can aim in a circle around the entity)

public float MaxAngle (the upper bounds. If MinAngle = 0 and MaxAngle = 360, the recticle can spin around the entity with impunity)

public T2DSceneObject AimingRecticle (The scene object that represents the sprite to be mounted to the entity, which will be twirled around the entity's center to be the visual cue for aiming)

public T2DSceneObject ProjectileTemplate (The template scene object for the projectiles to be spawned)


Hooking it together with an "OrientationComponent" (for direction that the entity is facing) and a "RotatingAimStick" component (which controls the orientation of the attached entity using the analog sticks on a gamepad), I'm able to restrict the angle, fire the projectile at the currently aimed angle, and spin the aiming recticle exactly as desired.

The only problem is mounting. Though the aiming recticle spins properly, it will not mount to the entity.

In my _OnRegister method, I'm doing the following:

if (_aimingRecticle != null)
{
_aimingRecticle.Mount(_sceneObject, "", true);
}

_aimingRecticle is the attached scene object, and _sceneObject is the entity to which the RotatingAimComponent is attached. At runtime, they're both holding references to the exact objects I'd expect them to, and the mount method is called without incident, but the mounting does not occur.

... which leads me to wonder, is there some undocumented rule that mounting has to occur at another point in the loading process? Is _OnRegister too soon to perform mounting? And if so, where would be the proper place to do it that would still be encapsulated within the Component? I've searched for two evenings so far, and have been unable to find any documentation on the subject.

About the author

Hi! I'm Alex Loret de Mola, AKA Vendal Thornheart, of Medaverse Studios. We're trying out tech for our new game, and Torque X's rapid prototyping has caught our eye! I finally got off my butt and got a license! =)


#1
01/22/2010 (12:21 pm)
Technically _OnRegister() is not too early, BUT both objects do need to be in the same state as regards having themselves been registered - that is either both objects must be registered already or both objects must be currently unregistered. I don't think that is documented, but it's in the source code for the Mount() method.

So _OnRegister() isn't a very safe place to do mounting. Instead try putting your mounting code in _PostRegister(), which is called after all objects in the scene have been registered.
#2
01/22/2010 (1:28 pm)
Ah, brilliant! =) I'll try it out tonight! Thank you very much for this suggestion, it sounds like this is exactly what is happening to me... I'll post again here after I try it tonight when I get home from the day job. ;)

As far as getting access to the source code of the Mount() method itself, I assume I only get that after I've purchased the full version? I need to get on that... hopefully with an upcoming paycheck I can secure a full copy.
#3
01/22/2010 (2:09 pm)
Yes you need the full version for the source code. It is immeasurably useful to have though - you can apply engine fixes, mod the engine, and of course check out how stuff works (because as you already know, the official documentation is a bit on the incomplete side).
#4
01/22/2010 (7:30 pm)
Aye... I'll definitely have to pick that up as soon as I can! Thank you again for your help Duncan, it looks like that did the trick!
#5
01/24/2010 (6:07 pm)
Hmm, apparently I spoke too soon. The PostRegister method sounded so obvious when you mentioned it that I was sure it would work... but I didn't actually get the time to try it until just now. It looks like the aiming recticle is still not registered by that point even though the entity in question is registered.

However, this has still given me valuable information with which to move forward... I just have to figure out why that recticle isn't registered by the time _PostRegister is called on the parent. There must be a reason in there... I'll post for anyone else who runs into this once I figure it out (unless someone else already knows what might be happening, in which case post away! =) )
#6
01/24/2010 (6:22 pm)
I just double checked - turns out I was wrong. Doh! I could have sworn postregister was called after all objects have been loaded! But actually it's called after all *components* on that particular object have been registered, so it's completely possible for your other scene object not to have been registered by that point.

I can't see any point in the engine where it calls all the components after a scene has loaded I'm afraid.

An alternative approach is to have your reticle object be a torque template object and have the component create an instance and register that instance itself. It sounds like this might make more sense for you anyway as you seem to want to add the reticle to multiple game objects.
#7
01/25/2010 (9:11 am)
Ah, I like the template idea! The thought hadn't occurred to me, that's a very good idea. I'll give that a try when I'm in there next.
#8
01/25/2010 (5:41 pm)
Hey Alex, I've found the default mounting behaviour to be somewhat of a nuisance. You could try writing your own mounting logic into a separate component (or the same) to move the reticle with object you want to mount to.

Something like:
// Pseudo code, sorry I don't remember the syntax right now
ProcessTick(Move move, float dt)
{
    _reticle.Position = _objectMountedTo.Position;

    // Maybe add in an offset or some other behaviours       
    _reticle.Position += _offset; 
}
#9
01/25/2010 (7:00 pm)
iirc the mounting does have some lag in it even when you set it to zero, which is annoying if you want something to be completely locked to another object. Alex's code would sort that out if the lag is an issue for you.
#10
01/26/2010 (9:26 pm)
Ah, interesting! Thanks, I'll give it a look as well!