Game Development Community

this.TargetElapsedTime in Torque

by John Bura · in Torque X 2D · 09/18/2010 (3:15 am) · 7 replies

A while back I coded something that worked in the standard Xna frame work. What the code allowed me to do is to play musical sounds to a specific time. It all works except for this line of code.

this.TargetElapsedTime = new TimeSpan(interval);

I know exactly what the problem is, I just don't know how to fix it.

Here is what the original had.
public class Game1 : Microsoft.Xna.Framework.Game

And here is what is in torque
public class SoundEngineComponent : TorqueComponent, ITickObject

I don't think I can extend two frameworks. I tried and failed. Does anybody know how I can access this.TargetElapsedTime. Or does anybody know the equivalent in torque?


#1
09/18/2010 (4:09 am)
Your Game class inherits from TorqueGame, which inherits from Microsoft.Xna.Framework.Game, so you can use

Game.Instance.TargetElapsedTime
#2
09/18/2010 (4:10 am)
Haha thanks. I knew it was something simple :)
#3
09/20/2010 (3:38 pm)
I solved 1 problem to open another. I have all of the logic figured out, but I think Im having a small problem of code placement. I had this working in the regular XNA game. As far as I know the process tick is like the update method in XNA.

This is in my static methods. This is supposed to set the time to musical time.
void SetTempo(int bpm, int ticksPerBeat)
        {
            long interval = TimeSpan.TicksPerMinute / (bpm * ticksPerBeat);
            Game.Instance.TargetElapsedTime = new TimeSpan(interval);
        }

This is in my process tick. This creates a track for all of my notes to go on.
for (int i = 0; i < 10000; i++)
                track1.Add(i * quaterNote);

And this is in my on register. This plays the selected sound on time.
SetTempo(128, quaterNote);
            if (track1.Contains(tick))
                PlaySound();

I spent several hours on this. I feel that I am really close but then again so far. Thanks in advance for your help :)
#4
09/20/2010 (11:53 pm)
I may be misunderstanding what you are doing, correct me if I'm wrong, if you are trying to play a sound on a specific tick through the entirety of a list of ticks, then;


public void ProcessTick(Move move, float dt)
{
     // accumulate time
     tick += dt;

     // check if the current time is in the track
     if (track1.Contains(tick))
          PlaySound();
}

and add the notes for the track when the object is registered

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

     SetTempo(128, quarterNote);
     for(int i = 0; i < 10000; i++)
          track1.Add(i * quarterNote);

     ProcessList.Instance.AddTickCallback(Owner, this);

     return true;
}
#5
09/21/2010 (2:44 pm)
So the way it works is that every X amount of ticks I add something to a track. In this case its 96.

SetTempo(128, 96);
            for (int i = 0; i < 10000; i++)
                track1.Add(i * 96);

Track one is an Int list. But dt is a float. For some reason when I change to a float it doesn't work. I also can't change dt to an int. Is there someway to access the gameloop's tick cycle in torque?

#6
09/21/2010 (7:43 pm)
to get the time since the last tick as an integer you can use,

tick += (int)(dt * 1000f);
#7
09/22/2010 (2:26 pm)
So this for the most part works except the sounds are not played evenly. I think the On _OnRegeister Method isn't being called evenly. So I attached a counting number to the sounds playing and it looks like this

1,2,3..... 4,5,6. The numbers move up then wait instead of moving up evenly: 1,2,3,4,5,6 etc.

I have no idea why the _OnRegister Method would be called like this.

** EDIT ** it is because I am using an int instead of a float....