Announcement

Collapse
No announcement yet.

Control Statement not working as expected, help please.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Control Statement not working as expected, help please.

    I have this bit of code to display 'jet thrusters' on my bike (Rhino) when it goes over a certain speed, and I wanted a sound to play when it does. I came up with this so far:

    Code:
    simulated function Tick(float DeltaTime)
    {
        local int i;
    
        if (VSize(Velocity) > 1000.0)
        {
            for(i=0; i<TrailEffects.Length; i++)
            {
                TrailEffects[i].SetThrustEnabled(True);
                TrailEffects[i].SetThrust(0.001);
                PlaySound(sound'WeaponSounds.RocketLauncher.RocketLauncherFire',,2.5*TransientSoundVolume);
            }
        }
        else
        {
            for(i=0; i<TrailEffects.Length; i++)
            {
                TrailEffects[i].SetThrustEnabled(False);
            }
        }
        log(ThrustFired);
        Super.Tick(DeltaTime);
    }
    The displaying of the jets works fine (at last!) but the sound plays continually.

    So I added some IF statements, to check if the sound has played:

    Code:
    simulated function Tick(float DeltaTime)
    {
        local int i;
        local int ThrustFired;
    
        if (VSize(Velocity) > 1000.0)
        {
            for(i=0; i<TrailEffects.Length; i++)
            {
                TrailEffects[i].SetThrustEnabled(True);
                TrailEffects[i].SetThrust(0.001);
            }
            if (ThrustFired == 0)
            {
                PlaySound(sound'WeaponSounds.RocketLauncher.RocketLauncherFire',,2.5*TransientSoundVolume);
            }
            ThrustFired = 1;
        }
        else
        {
            for(i=0; i<TrailEffects.Length; i++)
            {
                TrailEffects[i].SetThrustEnabled(False);
                ThrustFired = 0;
            }
        }
        log(ThrustFired);
        Super.Tick(DeltaTime);
    }
    But it does exactly the same thing . I can tell by the log that it's changing 'ThrustFired' correctly. So, to simplify, I want the sound to play once when the right speed is reached.

    #2
    Well, that section looks good pyro (btw, HI), however from what you are describing is that the sound you are playing, once reaching the trigger speed, gets played in an endless loop?

    Perhaps there is a StopSound function (not much on sound coding) that will end the loop (if it is a loop). Or perhaps there is a play once function...

    Anyways to make sure, I would try this:

    Code:
    simulated function Tick(float DeltaTime)
    {
        local int i;
        local int ThrustFired;
    
        if (VSize(Velocity) > 1000.0)
        {
            for(i=0; i<TrailEffects.Length; i++)
            {
                TrailEffects[i].SetThrustEnabled(True);
                TrailEffects[i].SetThrust(0.001);
            }
            if (ThrustFired == 0)
            {
                PlaySound(sound'WeaponSounds.RocketLauncher.RocketLauncherFire',,2.5*TransientSoundVolume);
                Log("Sound Triggered");
            }
            ThrustFired = 1;
        }
        else
        {
            for(i=0; i<TrailEffects.Length; i++)
            {
                TrailEffects[i].SetThrustEnabled(False);
                ThrustFired = 0;
            }
        }
        log(ThrustFired);
        Super.Tick(DeltaTime);
    }
    This should tell you if the sound is getting triggered more than once. Also, you might want to log the ThrustFired var out in a few more places, but it should be working fine.

    Comment


      #3
      Hi there CMan :up:.

      I tried that, and it is looping, but I can't see why, before you reach the speed the log output all 0, for ThrustFired. When you go over the speed, the log outputs only '1' for ThrustFired yet still plays the sound (and outputs 'Sound Triggered in the log) over and over again. This was definately wrong, as the sound should only play if ThustFired is '0'. So, I added another ThrustFired log before the IF TF=0 playsound statement, guess what? ThrustFired was 0, so, technically, it's working, because ThrustFired is 0 going into the IF. But the only thing setting it back to zero is in the ELSE statement, when it drops below the speed, it's should skip this part as long as the bike is still above 1000, which it is according to 'showdebug'. Somehow it's running the ELSE statement when it shouldn't be :weird:.

      Comment


        #4
        Uh? :bulb: It looks like its working perfectly fine to me. You're both just bonkers

        Let me elaborate. ThrustFired is LOCAL. Tick() occours every tick (duh), which is about every 0.04 seconds or so (DeltaTime is the time that has elapsed since the last call). So, since you are playing your sound in tick, it gets played every 0.04 seconds or so. Since ThrustFired is LOCAL, it doesnt exist between ticks. Once tick gets called again, ThrustFired is reinitialized to 0, which is why it appears to be 0 every tick (because it is!).

        Try making it global. Better yet, make it a bool, and toggle it.

        Comment


          #5
          yeah, what Bonehed said.....

          Code:
          simulated function outputnumber1(pawn instigator) {
              local int number;
              number++;
              log(number);
          }
          this will ALWAYS log the number 1. Every time you call the function, the local variables are started from scratch.

          Code:
          var int number;
          
          simulated function outputnumber1(pawn instigator) {
              number++;
              log(number);
          }
          This will log "1" then "2" then "3" and keep going up cause it's global.

          Why don't you try something like this......
          Code:
          var bool ThrustFired;
          
          simulated function Tick(float DeltaTime)
          {
              local int i;
          
              if (VSize(Velocity) > 1000.0)
              {
                  for(i=0; i<TrailEffects.Length; i++)
                  {
                      TrailEffects[i].SetThrustEnabled(True);
                      TrailEffects[i].SetThrust(0.001);
                  }
                  if (!ThrustFired)
                  {
                      PlaySound(sound'WeaponSounds.RocketLauncher.RocketLauncherFire',,2.5*TransientSoundVolume);
                  ThrustFired = True;
                  }
              }
              else
              {
                  for(i=0; i<TrailEffects.Length; i++)
                  {
                      TrailEffects[i].SetThrustEnabled(False);
                      ThrustFired = False;
                  }
              }
              Super.Tick(DeltaTime);
          }

          Comment


            #6
            Eh, I would do it more like this (commented on why):

            Code:
            var bool bThrustFired; 
            
            simulated function Tick(float DeltaTime) 
            { 
                local int i; 
            
                if (VSize(Velocity) > 1000.0) 
                { 
                    if (!bThrustFired) 
                    { 
                        // moved here because you only need to enable the emitters once (I think)
                        for(i=0; i<TrailEffects.Length; i++) 
                        { 
                            TrailEffects[i].SetThrustEnabled(True); 
                            TrailEffects[i].SetThrust(0.001); 
                        } 
                        PlaySound(sound'WeaponSounds.RocketLauncher.RocketLauncherFire',,2.5*TransientSoundVolume); 
                    bThrustFired = True; 
                    } 
                } 
                else if (bThrustFired)   // only need to disable the emitters once
                { 
                    for(i=0; i<TrailEffects.Length; i++) 
                    { 
                        TrailEffects[i].SetThrustEnabled(False); 
                        bThrustFired = False; 
                    } 
                } 
                Super.Tick(DeltaTime);
            Only thing to be aware of is when the player's velocity hits 1000, then quickly back to 999 (player let off throttle), then back to up 1000, over and over and over in a short period will mean the sound plays too often (it could happen, you might not want it to). So you might want to set up a float for the level time to know when the last sound was played, and not play it again if it was played less than 1 second ago, or whatever. Just an idea, though.

            Comment


              #7
              Yea, that was what I was thinking was going on, I just have been away from programming for a while.
              Code:
              var bool bThrustFired; 
              
              simulated function Tick(float DeltaTime) 
              { 
                  local int i; 
              
                  if (VSize(Velocity) > 1000.0) 
                  { 
                      if (!bThrustFired) 
                      { 
                          // moved here because you only need to enable the emitters once (I think)
                          for(i=0; i<TrailEffects.Length; i++) 
                          { 
                              TrailEffects[i].SetThrustEnabled(True); 
                              TrailEffects[i].SetThrust(0.001); 
                          } 
                          PlaySound(sound'WeaponSounds.RocketLauncher.RocketLauncherFire',,2.5*TransientSoundVolume); 
                      bThrustFired = True; 
                      } 
                  } 
                  else if (bThrustFired)   // only need to disable the emitters once
                  { 
                      for(i=0; i<TrailEffects.Length; i++) 
                      { 
                          TrailEffects[i].SetThrustEnabled(False); 
                          bThrustFired = False; 
                      } 
                  }
                  else  //If you want to be safe and aviod problems.
                  }
                      Log("we have a problem")
                      bThrustFired = False;
                  }
                  Super.Tick(DeltaTime);

              Comment


                #8
                Cheers guys, will try it later. I haven't really done much coding for UT, I just know about coding in general. I had ThrustFired and a bool to begin with, I changed it incase it had something to do with it.

                This is the final code:

                Code:
                simulated function Tick(float DeltaTime) 
                { 
                    local int i; 
                
                    if (VSize(Velocity) > 1000.0)
                    { 
                        if (!bThrustFired)
                        {
                            for(i=0; i<TrailEffects.Length; i++)
                            { 
                                TrailEffects[i].SetThrustEnabled(True);
                                TrailEffects[i].SetThrust(0.001); 
                            } 
                            PlaySound(sound'WeaponSounds.RocketLauncher.RocketLauncherFire',,2.5*TransientSoundVolume); 
                            bThrustFired = True;
                        } 
                    } 
                    else
                    { 
                        for(i=0; i<TrailEffects.Length; i++)
                        {
                            TrailEffects[i].SetThrustEnabled(False);
                            bThrustFired = False;
                        } 
                    } 
                    Super.Tick(DeltaTime);
                }
                (with the var declaration at the top too)

                I changed the ELSE IF to an ELSE because I'm guessing bThrust Fired is NULL to start with? It meant the jets cam on when you got on the bike. The sound does play over and over if you go 999, 1000, 999, 1000 and so on, but you can't really do it fast enough.

                Thanks again :up:.

                Comment


                  #9
                  The else if is safe because bools are initialized to FALSE automatically by the engine. Try it out, it should work.

                  Comment


                    #10
                    I tried it as ELSE if, and the jets came on when you got on the bike. You had to go over 1000 then below then they worked fine. When I changed it to a plain ELSE, they worked fine all the time.

                    Comment


                      #11
                      It should be else if (bThrustFired), and the jets would only come on if bThrustFired was set to true in the default properties.

                      Comment


                        #12
                        It should, but they came on as soon as you got on the bike.

                        Comment

                        Working...
                        X