Game Development Community

Problem with invisible objects

by Mark Maynard · in Torque X 2D · 10/24/2010 (7:39 pm) · 4 replies

In my came I have enemies with different colored rocket boosters. You shoot different colored bullets to kil the corresponding enemy. Right now if I spam all one color of bullet(ex green) sometimes the green rocket packs on new enemies will be invisible. I print out when they're created, I have a default case that is never entered, so I'm sure they're created. They're just invisible. Maybe it's problem with cloning or pooling objects, not really sure.Any help would be greatly appreciated.
Object emitter:
public virtual void ProcessTick(Move move, float dt)
        {
            // todo: perform processing for component here
            _timeTillNextShot -= dt;
            if(_timeTillNextShot<=0.0f)//shoot!
            {

                Vector2 vel = new Vector2(0.0f,0.0f);
                //get velocity
                vel.X = _minXVel + (float)_random.NextDouble() * (_maxXVel - _minXVel);
                vel.Y = _minYVel + (float)_random.NextDouble() * (_maxYVel - _minYVel);
                //get color
                TargetColor target_color = (TargetColor)_random.Next(1, 6);
                T2DSceneObject emited_object = null;
                while (emited_object == null)
                {
                    switch (target_color)
                    {
                        case TargetColor.blue:
                            emited_object = _blueTarget.Clone() as T2DSceneObject;
                            break;
                        case TargetColor.green:
                            emited_object = _greenTarget.Clone() as T2DSceneObject;
                            break;
                        case TargetColor.orange:
                            emited_object = _orangeTarget.Clone() as T2DSceneObject;
                            break;
                        case TargetColor.red:
                            emited_object = _redTarget.Clone() as T2DSceneObject;
                            break;
                        case TargetColor.yellow:
                            emited_object = _yellowTarget.Clone() as T2DSceneObject;
                            break;
                        default:
                            emited_object = _redTarget.Clone() as T2DSceneObject;
                            break;
                    }
                }

                //get character animation
                int randVal=_random.Next(0,_targetSprites.Count);
                T2DAnimatedSprite targetAnimation = _targetSprites[randVal].Animation.Clone() as T2DAnimatedSprite;
                string animal = "donkey";
                Console.WriteLine("Animal: {0}", randVal);
                animal = targetAnimation.Name;
                //T2DAnimatedSprite targetAnimation = (T2DAnimatedSprite)TorqueObjectDatabase.Instance.FindObject("CatAnim");

                emited_object.Mount(targetAnimation, "RocketPoint", true);
                Console.WriteLine("Emited object info: {0}", emited_object);
                //targetAnimation.Mount(emited_object, "LinkPoint1", true);
                targetAnimation.CollisionsEnabled = true;
                string color = "none";
                switch (target_color)
                {
                    case TargetColor.blue:
                        targetAnimation.Collision.CollidesWith = TorqueObjectDatabase.Instance.GetObjectType("BlueBullet");
                        color = "blue";
                        //emited_object.Collision.CollidesWith = targetAnimation.Collision.CollidesWith;
                        break;
                    case TargetColor.green:
                        targetAnimation.Collision.CollidesWith = TorqueObjectDatabase.Instance.GetObjectType("GreenBullet");
                        color = "green";
                        break;
                    case TargetColor.orange:
                        targetAnimation.Collision.CollidesWith = TorqueObjectDatabase.Instance.GetObjectType("OrangeBullet");
                        color = "orange";
                        break;
                    case TargetColor.red:
                        targetAnimation.Collision.CollidesWith = TorqueObjectDatabase.Instance.GetObjectType("RedBullet");
                        color = "red";
                        break;
                    case TargetColor.yellow:
                        targetAnimation.Collision.CollidesWith = TorqueObjectDatabase.Instance.GetObjectType("YellowBullet");
                        color = "yellow";
                        break;
                    default:
                        targetAnimation.Collision.CollidesWith = TorqueObjectDatabase.Instance.GetObjectType("RedBullet");
                        color = "wtfmate!";
                        break;
                }
                


                //emited_object.Position = SceneObject.Position;
                //emited_object.Physics.Velocity = vel;
                //TorqueObjectDatabase.Instance.Register(emited_object);
                targetAnimation.Position = SceneObject.Position;
                targetAnimation.Physics.Velocity = vel;
                Console.WriteLine("Emmiting {0} {1} ", color, animal);
                TorqueObjectDatabase.Instance.Register(targetAnimation);
                TorqueObjectDatabase.Instance.Register(emited_object);
            
                //reset timer for next awesomeness
                _timeTillNextShot= _minLaunchTimer+(float)_random.NextDouble()*(_maxLaunchTimer-_minLaunchTimer);

            }

        }

Bullet creation
Vector2 shot_direction = new Vector2(1.0f, 0.0f);
                _tankColor = Color.empty;
                //Decide which button collor was pressed, like in a guitar prefer buttons further down the neck
                if (move.Buttons[0].Pushed)
                    _tankColor = Color.green;
                if (move.Buttons[1].Pushed)
                    _tankColor = Color.red;
                if (move.Buttons[2].Pushed)
                    _tankColor = Color.yellow;
                if (move.Buttons[3].Pushed)
                    _tankColor = Color.blue;
                if (move.Buttons[4].Pushed)
                    _tankColor = Color.orange;
                //fire appropriate bullet
                if (move.Buttons[5].Pushed && (_timeSinceLastShot > _shotDelay))
                {
                    if (_tankColor == Color.green)
                    {
                        T2DSceneObject projectile = _greenBullet.Clone() as T2DSceneObject;
                        Vector2 pos = SceneObject.Position;
                        pos.X = pos.X + _bulletOffset.X;
                        pos.Y = pos.Y + _bulletOffset.Y + (SceneObject.Physics.Velocity.Y * 12.0f * dt);
                        projectile.Position = pos;
                        //projectile.Collision.CollidesWith = _enemyType;
                        projectile.Physics.Velocity = shot_direction * _shotSpeed;
                        BulletComponent bullet = projectile.Components.FindComponent<BulletComponent>();
                        bullet.playerNumber = (BulletComponent.PlayerNumber) _playerNumber;
                        TorqueObjectDatabase.Instance.Register(projectile);
                        _timeSinceLastShot = 0.0f;
                    }
                    else if (_tankColor == Color.red)
                    {
                        T2DSceneObject projectile = _redBullet.Clone() as T2DSceneObject;
                        Vector2 pos = SceneObject.Position;
                        pos.X = pos.X + _bulletOffset.X;
                        pos.Y = pos.Y + _bulletOffset.Y;
                        projectile.Position = pos;
                        //projectile.Collision.CollidesWith = _enemyType;
                        TorqueObjectDatabase.Instance.Register(projectile);
                        projectile.Physics.Velocity = shot_direction * _shotSpeed;
                        BulletComponent bullet = projectile.Components.FindComponent<BulletComponent>();
                        bullet.playerNumber = (BulletComponent.PlayerNumber)_playerNumber;
                        _timeSinceLastShot = 0.0f;
                        _tankColor = Color.red;
                    }
                    else if (_tankColor == Color.yellow)
                    {
                        T2DSceneObject projectile = _yellowBullet.Clone() as T2DSceneObject;
                        Vector2 pos = SceneObject.Position;
                        pos.X = pos.X + _bulletOffset.X;
                        pos.Y = pos.Y + _bulletOffset.Y;
                        projectile.Position = pos;
                        //projectile.Collision.CollidesWith = _enemyType;
                        projectile.Physics.Velocity = shot_direction * _shotSpeed;
                        BulletComponent bullet = projectile.Components.FindComponent<BulletComponent>();
                        bullet.playerNumber = (BulletComponent.PlayerNumber)_playerNumber;
                        TorqueObjectDatabase.Instance.Register(projectile);
                        _timeSinceLastShot = 0.0f;
                    }
                    else if (_tankColor == Color.blue)
                    {
                        T2DSceneObject projectile = _blueBullet.Clone() as T2DSceneObject;
                        Vector2 pos = SceneObject.Position;
                        pos.X = pos.X + _bulletOffset.X;
                        pos.Y = pos.Y + _bulletOffset.Y;
                        projectile.Position = pos;
                        //projectile.Collision.CollidesWith = _enemyType;
                        projectile.Physics.Velocity = shot_direction * _shotSpeed;
                        BulletComponent bullet = projectile.Components.FindComponent<BulletComponent>();
                        bullet.playerNumber = (BulletComponent.PlayerNumber)_playerNumber;
                        TorqueObjectDatabase.Instance.Register(projectile);
                        _timeSinceLastShot = 0.0f;
                    }
                    else if (_tankColor == Color.orange)
                    {
                        T2DSceneObject projectile = _orangeBullet.Clone() as T2DSceneObject;
                        Vector2 pos = SceneObject.Position;
                        pos.X = pos.X + _bulletOffset.X;
                        pos.Y = pos.Y + _bulletOffset.Y;
                        projectile.Position = pos;
                        //projectile.Collision.CollidesWith = _enemyType;
                        projectile.Physics.Velocity = shot_direction * _shotSpeed;
                        BulletComponent bullet = projectile.Components.FindComponent<BulletComponent>();
                        bullet.playerNumber = (BulletComponent.PlayerNumber)(int)_playerNumber;
                        TorqueObjectDatabase.Instance.Register(projectile);
                        _timeSinceLastShot = 0.0f;
                    }
                }

On hit code
public static void DamagingCollision(T2DSceneObject ourObject, T2DSceneObject theirObject, T2DCollisionInfo info, ref T2DResolveCollisionDelegate resolve, ref T2DCollisionMaterial physicsMaterial)
        {


            if (theirObject.LinkPoints.HasLinkPoint("RocketPoint"))
            {
                BulletComponent bullet = ourObject.Components.FindComponent<BulletComponent>();

                T2DSceneObject tallyBoard;

                switch (bullet.playerNumber)
                {
                    case PlayerNumber.one:
                        tallyBoard = Game.Instance.TallyBoard1;
                        break;
                    case PlayerNumber.two:
                        tallyBoard = Game.Instance.TallyBoard2;
                        break;
                    default:
                        tallyBoard = null;
                        break;
                }


                TallyComponent tally = tallyBoard.Components.FindComponent<TallyComponent>();

                switch (bullet.bulletColor)
                {
                    case BulletColor.blue:
                        tally.BlueIncrementScore();
                        break;
                    case BulletColor.green:
                        tally.GreenIncrementScore();
                        break;
                    case BulletColor.orange:
                        tally.OrangeIncrementScore();
                        break;
                    case BulletColor.red:
                        tally.RedIncrementScore();
                        break;
                    case BulletColor.yellow:
                        tally.YellowIncrementScore();
                        break;
                }
            }
        }

#1
10/24/2010 (10:34 pm)
are the created clones on the correct layer?
#2
10/25/2010 (1:43 am)
I was under the impression that they stayed on the same layer as the object they were cloned from. The layers seem right to me. It also seems odd that the color of rocket that fails is the same color as the one I have recently hit. I'm thinking it may have something to do with the object pool trying to grab an object that is in the middle of being killed so it gets into a weird state.
#3
10/25/2010 (5:22 pm)
When you get an object that is from the pool then it will not be in the state that a brand new object would be in. You should make sure to setup any stuff that needs setting, such as visibility (e.g. if you make the objects invisible when they 'die' you will need to make them visible again when you get them from the pool).

iirc there is a method called Reset() that is specifically for the purpose of allowing you to reset stuff on objects taken from the pool.
#4
11/07/2010 (6:17 pm)
Turning off pooling fixed the problem for now, I will check into the reset option at some point in the future. It looks like a race condition, because even explicitly setting visibility after cloning an object didn't work. Thanks for the help.