Game Development Community

Mounting a particle to a template

by Alienforce · in Torque X 2D · 05/28/2010 (3:16 pm) · 5 replies

When i mount a particle to template object in TX2D
i get "cannot register an object that is mounted to a template" when running the project..
If i unblock the template box in the "template" object and then run the project all my spawned
object has the particle mounted as i wanted .... :) but i breaks the template functionality for my base object. :(

Anyone knows anything about this?

#1
05/28/2010 (5:33 pm)
In the TX editor i usually make a particle effect a template.

Then in my code, when i register the object the particle should be mounted to, i find the template by name, clone it, register and then mount.

heres an example
//assuming SceneObject is the object to be mounted to
T2DParticleEffect temp = 
(T2DParticleEffect)TorqueObjectDatabase.Instance.FindObject<T2DParticleEffect>("YourParticleEffectName").Clone();

TorqueObjectDatabase.Instance.Register(temp);

temp.Mount(SceneObject, "YourLinkPointName", true);

hope this helps
#2
05/28/2010 (7:56 pm)
Here is a mount component you can throw on your "template" so that you can choose the effect you want to mount in Torque X Builder. Pretty much the same as Raymond, but I like having the component.

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;

using GarageGames.Torque.Core;
using GarageGames.Torque.Util;
using GarageGames.Torque.Sim;
using GarageGames.Torque.T2D;
using GarageGames.Torque.SceneGraph;
using GarageGames.Torque.MathUtil;

namespace StarterGame2D
{
    [TorqueXmlSchemaType]
    public class MountComponent : TorqueComponent, ITickObject
    {
        //======================================================
        #region Static methods, fields, constructors
        #endregion

        //======================================================
        #region Constructors
        #endregion

        //======================================================
        #region Public properties, operators, constants, and enums

        public T2DSceneObject SceneObject
        {
            get { return Owner as T2DSceneObject; }
        }

        public T2DSceneObject MountObject1
        {
            get { return _mountObject1; }
            set { _mountObject1 = value; }
        }

        public string Obj1LinkPoint
        {
            get { return _object1LinkPoint; }
            set { _object1LinkPoint = value; }
        }

        public bool Obj1OwnedByMount
        {
            get { return _object1OwnedByMount; }
            set { _object1OwnedByMount = value; }
        }

        #endregion

        //======================================================
        #region Public methods

        public virtual void ProcessTick(Move move, float dt)
        {
            // todo: perform processing for component here
        }

        public virtual void InterpolateTick(float k)
        {
            // todo: interpolate between ticks as needed here
        }

        public override void CopyTo(TorqueComponent obj)
        {
            base.CopyTo(obj);

            MountComponent obj2 = (MountComponent)obj;
            obj2._mountObject1 = _mountObject1;
            obj2._object1LinkPoint = _object1LinkPoint;
            obj2._object1OwnedByMount = _object1OwnedByMount;
            //obj2._obj1Clone = _obj1Clone;

        }

        #endregion

        //======================================================
        #region Private, protected, internal methods

        protected override bool _OnRegister(TorqueObject owner)
        {
            if (!base._OnRegister(owner) || !(owner is T2DSceneObject))
                return false;



            //if (_mountObject1 != null)
            //{
                _obj1Clone = (T2DSceneObject)_mountObject1.Clone();
                TorqueObjectDatabase.Instance.Register(_obj1Clone);

                _obj1Clone.Mount((T2DSceneObject)Owner, _object1LinkPoint, _object1OwnedByMount);
            //}
            
            // todo: perform initialization for the component

            // todo: look up interfaces exposed by other components
            // E.g., 
            // _theirInterface = Owner.Components.GetInterface<ValueInterface<float>>("float", "their interface name");            

            return true;
        }

        protected override void _OnUnregister()
        {
            // todo: perform de-initialization for the component

            base._OnUnregister();
        }

        protected override void _RegisterInterfaces(TorqueObject owner)
        {
            base._RegisterInterfaces(owner);

            // todo: register interfaces to be accessed by other components
            // E.g.,
            // Owner.RegisterCachedInterface("float", "interface name", this, _ourInterface);
        }


        #endregion

        //======================================================
        #region Private, protected, internal fields

        T2DSceneObject _mountObject1;
        T2DSceneObject _obj1Clone;
        string _object1LinkPoint;
        bool _object1OwnedByMount;




        #endregion
    }
}
#3
05/30/2010 (10:20 am)
Thanks! works perfect!
#4
05/30/2010 (11:24 am)
hey would this work with mounting..lets say...an animation to a static material when spawned? I keep running into situation where I end up spawning a sprite missing its mounted animation.... (-_-)
#5
05/30/2010 (9:06 pm)
Awesome! You should put this on the component section of the Torque X section on TDN