Game Development Community

Trying to spawn random shootable items?

by GxG6(Mark-Anthony) · in Torque X 2D · 04/20/2010 (8:12 am) · 8 replies

ok help me help myself lmao.
well I'm creating a projectile spawner.
it will spawn a different type of projectile only after the first projectile that was fired has
reach been destroyed. First thing i thought of was using a random number generator similar to the ones used to create "shuffle" properties for music players. anyway ok so i mapped it out as so:

in the torque X builder i added 3 different projectile sprites.
now over in xna i created a new comp named it SpawnSngleRan

heres what i basically came up with.
Random ranP = new Random();
int Projtype = randP.Next(2);
if (Projtype == 0)
{
   T2DSceneObject _projectile1
else if (Projtype == 1) 
   T2DSceneObject _projectile2
else if (projtype == 2)
   T2DSceneObject _projectile
}

this is jsut a mapped out code, haven't actually put it into the program yet, not sure if it is
even plausible
what do u guys think??

#1
04/20/2010 (8:42 am)
Someone Suggested to me I should make a manager for my projectiles. I'm thinking that would work too. I could create a new cs file for the projectiles only and link it to the fire button code i have in my player file. i'm not too confident about linking the files. but if my mind is correct it should be similar to how the "miscdelegates.cs" is linked to "Dealdamage.cs" in the gamestarter kit created by Henryshillings. (yea i've been tearing that code apart since i seen it and u rementioned it to me :p)
_sceneObject.Collision.OnCollision = StarterGame2D.miscDelegates.DoDamage;
#2
04/20/2010 (10:15 am)
I would define a number of projectiles in my component that point to (is that the correct terminology?) some projectile templates like this:

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

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

public T2DSceneObject Projectile1
{
    get { return _projectile1; }
    set { _projectile1 = value; }
}

public T2DSceneObject Projectile2
{
    get { return _projectile2; }
    set { _projectile2 = value; }
}

public T2DSceneObject Projectile3
{
    get { return _projectile3; }
    set { _projectile3 = value; }
}

Then in the editor you can set them to different projectiles for each thing that fires and such.

in my firing code I would do it like this:
T2DSceneObject _theProjectile;

int pickone = TorqueUtil.GetFastRandomInt(3); // built in torque random
switch(pickone)
{
    case 0:
        _theProjectile= _projectile1.Clone() as T2DSceneObject;
        break;
    case 1:
        _theProjectile= _projectile2.Clone() as T2DSceneObject;
        break;
    case 2:
        _theProjectile= _projectile3.Clone() as T2DSceneObject;
        break;
}
if(_theProjectile != null) // make certain it exists
{
    //do stuff here to fire
    _theProjectile .Position = _sceneObject.Position;
    //etc etc
    TorqueObjectDatabase.Instance.Register(_theProjectile );
}

This will randomly pick on of the three bullets you assigned to your gun in the TX2D editor. it then creates a clone of the projectile and depending on your code sends it on it's way.

#3
04/20/2010 (11:15 am)
I have never seen or known about "TorqueUtil.GetFastRandomInt"
maybe i'm entering something wrong?? I was trying to do it through the original code i posted above, maybe thats why?
#4
04/20/2010 (11:31 am)
ahhh i think i know what happened, none of my random codes were working and I couldn't figure it out then I noticed I never referenced the Torque Ulit or Mathhelper files :/ so i basically spent half a day and wasted forum space because i forgot a reference...good grief!
#5
04/20/2010 (1:38 pm)
hey henry i ran into a problem while sampling your code.

}   
if(_theProjectile != null) // make certain it exists   
{   
    //do stuff here to fire   
    _theProjectile .Position = _sceneObject.Position;   
    //etc etc   
    TorqueObjectDatabase.Instance.Register(_theProjectile );   
}

it says "use of unassigned local variable '_theProjectile'

but its clearly assigned why does this happen?
#6
04/20/2010 (4:57 pm)
I guess that using it in the case statement doesnt register with the compiler, change the first line to:

T2DSceneObject _theProjectile = null;

That should compile



#7
04/21/2010 (4:35 pm)
thank you for brain storming with me henry! i wish more people were active on the Tx2d forums. i had to moderately modify the code u provided to fit into the code i had already attempted but the end result came out great now i can press on and fix and modify it.
#8
04/22/2010 (12:42 pm)
N/A