Game Development Community

Oddness with ALX Music handles...

by Orion the Hunter · in Torque Game Builder · 07/27/2013 (9:20 am) · 7 replies

Hello,

I'm having trouble (again) with my trigger. It allows the player to enter into swimming-mode.

//-----------------------------------------------------------------------------  
    //-----------------------------------------------------------------------------  
    // Torque Game Builder  
    // Copyright (C) GarageGames.com, Inc.  
    //   
    // A Swimming-Zone Behavior written by JackRabbit  
    //-----------------------------------------------------------------------------  
    /*
      1) Add a global to point to your player, called $player.
      2) Create a sceneObject, named MyTrigger and drop this behavior on it.
      3) Turn off all collisions for this.
    */  
    if (!isObject(SZBehavior))  
    {  
       %template = new BehaviorTemplate(SZBehavior);  
         
       %template.friendlyName = "Swimming Zone Trigger";  
       %template.behaviorType = "System";  
       %template.description  = "Preform actions when the player is in the water.";  
       %template.addBehaviorField( PlayerOnly, "Collide with the player, not other actors",       BOOL,  true );

    }  
        
function waterMusicOn()
{
stopMusic();
$musicHandle1 = alxPlay(NormalMusic); 
alxSourcef($musicHandle1, "AL_GAIN", 0);
$musicHandle2 = alxPlay(WaterMusic); 
alxSourcef($musicHandle2, "AL_GAIN", 0);

alxSourcef($musicHandle2, "AL_GAIN", 1);
alxSourcef($musicHandle1, "AL_GAIN", 1);
}

function turnonWM()
{
alxSourcef($musicHandle2, "AL_GAIN", 1);
}

function turnOffWM()
{
alxSourcef($musicHandle2, "AL_GAIN", 0);
}

function turnonNorm()
{
alxSourcef($musicHandle1, "AL_GAIN", 1);
}

function turnOffNorm()
{
alxSourcef($musicHandle1, "AL_GAIN", 0);
}

          

    function WMKickstart()
    {
    //Placeholder!
    }

    function SZBehavior::onAddToScene(%this, %scenegraph)
    {

    //if ( %this.PlayerOnly )
    //{
        %this.Owner.setObjectType( "PlayerTrigger" );
        %this.Owner.setCollidesWith( "PlayerObject" );
    //}
    //else
    //{
    //    %this.Owner.setObjectType( "ActorTrigger" );
    //    %this.Owner.setCollidesWith( "ActorObject" );
    //}

    waterMusicOn();
    alxSourcef($musicHandle2, "AL_GAIN", 1);
    schedule(1, 0, "turnOffWM");
    }
    
    function SZBehavior::onEnter(%this, %theirObject)
    {  
       // get the extents of this scene object
		  startSwimming();  

      %aura = new t2dParticleEffect()
  {
      scenegraph = sceneWindow2D.getSceneGraph();
      layer      = 0;
      effectFile = "~/data/particles/WaterStuff.eff";
      useEffectCollisions = "1";
      class = "WaterParticles";
      effectMode = "kill";
      effectTime = "0.2";
      canSaveDynamicFields = "1";
      Position = %theirObject.Position;
      size = "30.400 30.293";
      SortPoint = "-0.037 -0.295";
      CollisionMaxIterations = "1";
  };
    %aura.playEffect(true);
  %aura.isPlaying = true;

%theirObject.maxMoveSpeed = 40;
$Game::player.AirAccel = 30;
$Game::player.AirDecel = 12.5;
$Game::player.GroundAccel = 45;
$Game::player.GroundDecel = 125;
    }

function SZBehavior::onLeave(%this, %theirObject)
{
echo("Called \"SZBehavior::OnLeave,\" checking if we have a good velocity.");

if($game::player.linearVelocity.Y < 0)
{
echo($game::player.LinearVelocity.Y @ " IS A GOOD VELOCITY!!! - SZBehavior::OnLeave");
alxSourcef($musicHandle1, "AL_GAIN", 1);
alxSourcef($musicHandle2, "AL_GAIN", 0);

		  stopSwimming();
    %theirObject.setAnimationState( "Jump" );

%theirObject.maxMoveSpeed = 80;
$Game::player.AirAccel = 60;
$Game::player.AirDecel = 25;
$Game::player.GroundAccel = 90;
$Game::player.GroundDecel = 250;


      %aura = new t2dParticleEffect()
  {
      scenegraph = sceneWindow2D.getSceneGraph();
      layer      = 0;
      effectFile = "~/data/particles/WaterStuff.eff";
      useEffectCollisions = "1";
      class = "WaterParticles";
      effectMode = "kill";
      effectTime = "0.2";
      canSaveDynamicFields = "1";
      Position = %theirObject.Position;
      size = "30.400 30.293";
      SortPoint = "-0.037 -0.295";
      CollisionMaxIterations = "1";
  };
    %aura.playEffect(true);
  %aura.isPlaying = true;
}
else
{
error($game::player.LinearVelocity.Y @ " IS A BAD VELOCITY!!! - SZBehavior::OnLeave");
}
}

	function startSwimming()
	{
    alxSourcef($musicHandle2, "AL_GAIN", 1);
  
    alxSourcef($musicHandle1, "AL_GAIN", 0);

    echo("Swimming!");
    $game::player.isSwimming = 1;
    %theirObject.isSwimming = 1;
    $game::player.gravity = "0 -10";
    echo("StartSwimming method gravity... " @ %theirObject.gravity);
    echo("StartSwimming Method: isSwimming = " @ %theirobject.isSwimming);
    

    }

    function stopSwimming()
	{
    alxSourcef($musicHandle2, "AL_GAIN", 0);

    alxSourcef($musicHandle1, "AL_GAIN", 1);

    warn("Not Swimming!");
    $game::player.isSwimming = 0;
    %theirObject.isSwimming = 0;
    echo("StopSwimming Method: isSwimming = " @ %theirobject.isSwimming);
    $game::player.gravity = "0 200";
    
    }

The problem is musicHandle2 isn't playing silently in the background while we listen to musicHandle1. When you enter the SZ trigger, musicHandle2's volume will change from 0 to 1. It's not the same as starting and stopping it because it has been playing in the background ever since I start the level, but unfortunately, it seems to be disagreeing with that. Any help?

#1
07/28/2013 (10:01 am)
Perhaps set the datablocks to play them on different channels and flop the channel volumes instead? Just thinking out loud here - I did a bit of work on the sound system but I didn't fiddle with gain at all, just the stop/start/pause functionality.
#2
07/29/2013 (9:40 pm)
Gah, I keep meaning to look at this. Been a little busy finally releasing my game :o

I know I did something with this before too, and Richard's suggestion sounds like it might be on the right track. Have you tried putting the other sound track in a separate channel?
#3
07/30/2013 (7:29 pm)
When I saw the title of the post I was expecting a question about negative handle values when the docs clearly state that they will be positive integers. That one's easy - there is a state mask that can be applied to a handle that occasionally causes it and the engine takes care of it so there is no problem. It just looks like a problem....

But that wasn't what the post was about after all....
#4
08/04/2013 (2:49 pm)
Thanks! So how exactly do you play music through a different channel? I don't have too much experience with ALX stuff.
#5
08/05/2013 (4:23 pm)
Look at this resource. You'll see that I created audio descriptions for various "channels" - these can be controlled with alxSetChannelVolume(channel, vol) where channel is an integer representing the sound channel and vol is 0.0 ~ 1.0.

For more detail, look at /common/gameScripts/audio.cs You'll see it is set by default to handle 8 sound channels.
#6
08/07/2013 (7:55 am)
Ah, I get it.

So, I tried to assign the two songs to separate channels but still no luck. What's weird is if I have the water music playing from the beginning and I turn off its sound manually, it's smooth sailing. I tried to make it so that I have it playing as soon as you load the level and then immediately, it turns off, but that didn't work either. maybe if I pop a schedule in there so that it stops it 1 millisecond after it starts, so as to not get the engine confused?
#7
08/07/2013 (1:51 pm)
Not 1 - always use multiples of 32 so that the schedule actually fires on the next available tick. Using numbers less than 32 can cause an assert because the engine thinks the schedule was supposed to happen earlier than "now".

I'll try to find some time to tinker with this before the weekend....