News:

One Minute Game Review by The Happy Friar: https://ugetube.com/@OneMinteGameReviews
Also on Rumble: https://rumble.com/c/c-1115371

idTech 4 (aka Doom 3 tech) Discord Server! https://discord.gg/9wtCGHa

Main Menu

Zooming by run loop script

Started by bitterman, September 02, 2017, 01:09:06 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

bitterman

I trying to achieve a sniper zooming effect via 'g_fov' CVar.

It's realized via level script but as I think same algorithm can be used in weapon script.

The example below is works but from time to time g_fov is not set correct (stays in previous state or sets several times in one pass).

I thing it's because cycle checks is too fast.

Has anyone idea how to fix it?

Thanks.


void    debug_print()
        {
        sys.println( "^3g_fov set to: " + sys.getcvar( "g_fov" ) + " ^5at:" + sys.getTime() + "sec" );
        }

void    pressed_fov()
        {
        float userbuttons, btn2;                      // inspired by modwiki

        boolean already_set = ( false );              // each second "Z"-press will reverse this flag and reset g_fov

            while(1)
            {
            userbuttons = $player1.getButtons();
            btn2 = 4 & userbuttons;                   // 0000 0100 & 0000 0100 = true

              if(btn2)                                // 'Zoom' was pressed
                {
                     if( already_set )                // is it second press?
                     {
                        sys.setcvar( "g_fov", "90" );
                        already_set = !already_set;
                        debug_print();
                        sys.waitFrame();
                     }
                     else
                     {
                        sys.setcvar( "g_fov", "10" );
                        already_set = !already_set;
                        debug_print();
                     }
                }
            sys.waitFrame();
            }
        }

void main()
        {
        thread pressed_fov();       // always run when level is loaded
        }



The Happy Friar

Doom 3 has a built in zoom feature for each weapon. 
http://idtechforums.fuzzylogicinc.com/index.php?topic=260.0

"zoomFOV" in the weapon's def.

Would that make things easier?

argoon

Quote from: bitterman on September 02, 2017, 01:09:06 PM
I trying to achieve a sniper zooming effect via 'g_fov' CVar.

It's realized via level script but as I think same algorithm can be used in weapon script.

The example below is works but from time to time g_fov is not set correct (stays in previous state or sets several times in one pass).

I thing it's because cycle checks is too fast.

Has anyone idea how to fix it?

Thanks.


void    debug_print()
        {
        sys.println( "^3g_fov set to: " + sys.getcvar( "g_fov" ) + " ^5at:" + sys.getTime() + "sec" );
        }

void    pressed_fov()
        {
        float userbuttons, btn2;                      // inspired by modwiki

        boolean already_set = ( false );              // each second "Z"-press will reverse this flag and reset g_fov

            while(1)
            {
            userbuttons = $player1.getButtons();
            btn2 = 4 & userbuttons;                   // 0000 0100 & 0000 0100 = true

              if(btn2)                                // 'Zoom' was pressed
                {
                     if( already_set )                // is it second press?
                     {
                        sys.setcvar( "g_fov", "90" );
                        already_set = !already_set;
                        debug_print();
                        sys.waitFrame();
                     }
                     else
                     {
                        sys.setcvar( "g_fov", "10" );
                        already_set = !already_set;
                        debug_print();
                     }
                }
            sys.waitFrame();
            }
        }

void main()
        {
        thread pressed_fov();       // always run when level is loaded
        }


I don't know but it could be because getbuttons runs more than one time at button press(was made for continuous pressing like shooting), no matter how fast you press it, so that perhaps is messing things up, before i add full access to impulses (they run only one time) this also caused many problems to me, you need to find a way to make the code run only one time at button press, unfortunately that is easier said than done.

bitterman

#3
Thanks guys.

Founds some interesting things:

idInterpolate<float> zoomFov;
/*
   82 ====================
   83 idInterpolate::Init
   84 ====================
   85 */
   86 template< class type >
   87 ID_INLINE void idInterpolate<type>::Init( const float startTime, const float duration, const type &startValue, const type &endValue ) {
   88         this->startTime = startTime;
   89         this->duration = duration;
   90         this->startValue = startValue;
   91         this->endValue = endValue;
   92         this->currentTime = startTime - 1;
   93         this->currentValue = startValue;
   94 }

if ( IsLocallyControlled() ) {
// zooming
if ( ( usercmd.buttons ^ oldCmd.buttons ) & BUTTON_ZOOM ) {
if ( ( usercmd.buttons & BUTTON_ZOOM ) && weapon.GetEntity() ) {
zoomFov.Init( gameLocal.time, 200.0f, CalcFov( false ), weapon.GetEntity()->GetZoomFov() );
} else {
zoomFov.Init( gameLocal.time, 200.0f, zoomFov.GetCurrentValue( gameLocal.time ), DefaultFov() );
}
}
}


Theoretically in my script  I can check delay with sys.getTime or just insert sys.wait(1) in cycle.
Is sys.wait is bad for performance in this case (it will be run in infinite loop)?

argoon

A while (1) loop is in it self a infinite loop unless you break from it, so be free to use sys.wait(1), btw a frame takes less than a second to run, if you open the doom_defs.script you will see that id software defined a frame time as 0.016 seconds (16 milliseconds), and i seam to recall that if you make a sys.wait() with less than 0.016 your loop will stop working.

About performance by using "thread" keyword before the function name you are making sure the loop doesn't clog the pipeline, also unless your loop is made of very complex chain's of if else's and many entity spawn arguments queries, like using many getkey() etc, than performance eating imo is not that bad, plus you should try to break from a while(1) loop ( break; ) if possible, unless it is required to run for the entirety of the game or map.

bitterman

Thanks for useful info.

As say THF we can just add "zoomFov" "x" into appropriate weapon.def and it works just fine via Code (IMHO it set FOV from default to "x" and back to default interpolated between 200 ms).

And I see startFxFov native func and StopFxFov script event but it looks like usrealized feature.

And also InfluenceFov - don't know what is it yet.


The Happy Friar

influenceFov is for when the influence trigger is done, one of the options is changing the player FOV.  It's all setup via the influence entity.