Frictional Games Forum (read-only)

Full Version: Timer for StartPlayerLookAt?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there anyone out there that can help me get a script to time just how long my player looks at a specific thing?

After that, if someone is feeling generous, could one of you maybe provide me with the formula on how to use AddTimer for just about anything so I don't have to ask so many questions? Wink

Thanks
AddTimer is very simple, AddTimer("NameOfTimer", 1, "NameOfFunction");

So this means that 1 second after you trigger the AddTimer, you will execute what is in NameOfFunction.

Assuming you want the player to look at something when going into an area it would be something like:

Code:
void CollidePlayerArea(string &in asParent, string &in asChild, int alState)
{
    StartPlayerLookAt("LooktAtArea", 2, 2, "");    

    AddTimer("Scared", 2, "TimerLookAtEvents");  //After 2 seconds he is scared
    AddTimer("StopLookAt", 4, "TimerLookAtEvents");  //After 4 seconds he stops to look
}

void TimerLookAtEvents(string &in asTimer)
{
     if(asTimer == "Scared")
    {
        PlayGuiSound("react_scare.snt", 1);
    }
    else if (asTimer == "StopLookAt")
    {
        StopPlayerLookAt();
    }
}
AddTimer is pretty easy :

let's say for example you want to kill the player 2 seconds after he walked on a trigger area :

you will first create the AddEntityCollideCallback inside OnEnter

Code:
void OnEnter()
{
      AddEntityCollideCallback("Player", "AreaKillPlayer", "TimerKillplayer", true, 1);
  }

Then you create the timer function "TimerKillPlayer" outside OnEnter
Code:
void TimerKillPlayer(string &in asParent, string &in asChild, int alState)
{
AddTimer("NameYourTimer", 1.0f, "KillPlayer");
}

Then you create the function that will actually kill the player, outside OnEnter
Code:
void KillPlayer(string &in asTimer)
{
   GivePlayerDamage(....)
}

edit : jens was too fast : - )
(09-21-2010, 07:36 AM)theDARKW0LF Wrote: [ -> ]Is there anyone out there that can help me get a script to time just how long my player looks at a specific thing?

Well... To answer that specific question. I don't think there's an easy way to do that.

Here's an example where I made my own GetTickCount() function. I don't know if there's an easier way.

I didn't test this, as I just wrote it off the top of my brain.

Code:
float GetTickCount()
{
    float tick = 10000.0f - GetTimerTimeLeft("TickCount");
    return tick;
}

void OnStart()
{
    SetEntityPlayerLookAtCallback("NameOfEntityToLookAt", "CallbackFunctionName", false);
    
    //Setup tick count
    AddTimer("TickCount", 10000.0f, "NoFunction");
    
    SetLocalVarFloat("StartedLookingAt", 0);
    SetLocalVarFloat("StoppedLookingAt", 0);
}

CallbackFunctionName(string &in entity, int alState)
{
    if(alState >= 1) //If looking
    {
        if(GetLocalVarFloat("StartedLookingAt") == 0)
            SetLocalVarFloat("StartedLookingAt", GetTickCount());
    }
    else
    {
        if(GetLocalVarFloat("StartedLookingAt") != 0 && GetLocalVarFloat("StoppedLookingAt") == 0)
        {
            SetLocalVarFloat("StoppedLookingAt", GetTickCount());
            SetLocalVarFloat("TimeLookedAtEntity", GetLocalVarFloat("StoppedLookingAt") - GetLocalVarFloat("StartedLookingAt"));
            
            SetLocalVarFloat("StartedLookingAt", 0);
            SetLocalVarFloat("StoppedLookingAt", 0);
        }
    }
}


Then the time the player looked at the entity is stored in the variable TimeLookedAtEntity.

Example to get the time:
Code:
GetLocalVarFloat("TimeLookedAtEntity");

Note that you can't get the time before the player stops looking.

If you want it to count while the player is looking you'll have to change some things.


Of course you could also just add a timer that calls itself each 0.25 second and adds 0.25 to a variable, or something. Which probably would be the easiest.