Frictional Games Forum (read-only)

Full Version: How to remove a Timer Loop which opens a door using AddPropForce?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I wanted to make a door open by itself slowly behind the player and used Frictional's code in their .hps of the first level (Rainy Hall) as reference.
The door opens nicely and behaves as expected, but I can't figure out how to remove the loop so the player can close the door without it wanting to open again Sad

Here's the code:

Code:
void OpenDoor(string &in asParent, string &in asChild, int alState)
{
    SetSwingDoorClosed("prison_section_2", false, false);
    SetSwingDoorDisableAutoClose("prison_section_2", true);
    AddTimer("swing_door", 0.01f, "TimerDoor");
    AddTimer("stop_creak", 1.5f, "TimerCreak");
}

void TimerDoor(string &in asTimer)
{
    if(asTimer == "swing_door")
    {
        AddPropForce("prison_section_2", 0.0f, 0.0f, 25.0f, "");
    }
    else
    {
        AddPropForce("prison_section_2", 0.0f, 0.0f, -95.0f, "");
    }
    AddTimer(asTimer, 0.03, "TimerDoor");
}

void TimerCreak(string &in asTimer)
{
    RemoveTimer("swing_door");
    PlaySoundAtEntity("pant", "react_scare.snt", "Player", 0.0f, false);
}

I think it has something to do with the devs using the same string for the timer and door name, but I can't really figure out how.


DEV VERSION
Spoiler below!
Code:
/*When in hall4 gallery door swings open slowly
*/
void CollideAreaGalleryDoor(string &in asParent, string &in asChild, int alState)
{
    SetSwingDoorClosed("door_gallery", false, false);
    SetSwingDoorDisableAutoClose("door_gallery", true);
    
    PlaySoundAtEntity("creaking_door", "joint_door_move_special.snt", "door_gallery", 1.0 / 0.7f, false);
    
    AddTimer("door_gallery", 0.01f, "TimerSwingDoor");
    
    AddDebugMessage("Boho, the gallery door creaks open.", false);    
    
    AddTimer("stopcreak", 2.0f, "TimerStopCreak");
}
void TimerSwingDoor(string &in asTimer)
{
    if(GetLocalVarInt("SwingDoor") == 10){
        SetLocalVarInt("SwingDoor", 0);
        return;
    }
    
    if(asTimer == "door_gallery") AddPropForce(asTimer, 70.0f, 0, 0, "World");
    else AddPropForce(asTimer, -95.0f, 0, 0, "World");
    
    AddLocalVarInt("SwingDoor", 1);
    
    AddTimer(asTimer, 0.03f, "TimerSwingDoor");
    
    AddDebugMessage("Swing: "+GetLocalVarInt("SwingDoor"), false);
}
void TimerStopCreak(string &in asTimer)
{
    if(asTimer == "scare2"){
        PlaySoundAtEntity("scare", "react_scare.snt", "Player", 1.0 / 1, false);
        return;
    }
    if(asTimer == "scare"){
        PlaySoundAtEntity("scare", "react_scare.snt", "Player", 0, false);
        //PlaySoundAtEntity("laugh", "00_laugh.snt", "door_gallery", 1.0 / 1, false);
        PlaySoundAtEntity("loop", "00_loop.snt", "Player", 1.0 / 0.1f, false);
        return;
    }
    if(asTimer == "breath1"){
        PlaySoundAtEntity("breath", "react_breath.snt", "Player", 1.0 / 1, false);
        return;
    }
    if(asTimer == "breath2"){
        PlaySoundAtEntity("breath", "react_breath.snt", "Player", 1.0 / 0.9f, false);
        return;
    }
    if(asTimer == "breath3"){
        //SetSwingDoorLocked("door_gallery", false, true);
        PlaySoundAtEntity("breath", "react_breath.snt", "Player", 1.0 / 0.8f, false);
        AddTimer("lookloop", RandFloat(2.0f,3.0f), "TimerRandomLook");    //Re-activate the spinning head
        FadePlayerFOVMulTo(1.0f, 0.1f);
        StopSound("loop", 1);
        StartScreenShake(0.01f,0.25f, 0.01f,0.5f);
        return;
    }
    if(asTimer == "lightsout"){
        SetLampLit("chandelier_nice_1", false, true);
        SetLampLit("candlestick_floor_2", false, true);
        FadeLightTo("PointLight_25", 0, 0, 0, 0, 0, 1);
        FadePlayerFOVMulTo(1.5f, 0.25f);
        return;
    }
    
    if(asTimer == "sanity"){
        //SetSwingDoorLocked("door_gallery", true, false);
        GiveSanityDamage(10, true);
        return;
    }
    
    StopSound("creaking_door", 0.4f);
}

Do you mean it keeps getting pushed open?
Yes. The player can close the door, but when you let go the door creaks open again. My RemoveTimer showns no effect.
Code:
void lanterndoorclose(string &in asParent, string &in asChild, int alState)
{
     AddPropImpulse("lanterndoortrapdoor", 0, 150, 0, "world");
     AddTimer("lanterndoortrapdoortimer", 0.3f, "lanterndoortrapdoorlock");
}
void lanterndoortrapdoorlock(string &in asTimer)
{
    SetSwingDoorLocked("lanterndoortrapdoor", true, true);
    PlayGuiSound("door_level_wood_locked", 1);
}

Well, that's what I use.
But in my case, it closes, not opens...
An impulse with 150.0f force would SLAM it open very hard, not so subtle Big Grin

And using the timer loop creates the very problem which is the cause of this thread..
When is "opendoor" activated?
Could you show us that part of the script?
It's a CollideCallback in OnStart: the bottom one here to be precise:

Code:
    AddEntityCollideCallback("Player", "CollideEnterTorture", "EnterTorture", true, 1);
    AddEntityCollideCallback("Player", "CollideOpenDoor", "OpenDoor", true, 1); <-- this one
Mmm... that doesnt seem to be the problem.

Oh, I think I see it now.

Code:
void TimerDoor(string &in asTimer)
{
    if(asTimer == "swing_door")
    {
        AddPropForce("prison_section_2", 0.0f, 0.0f, 25.0f, "");
    }
    else
    {
        AddPropForce("prison_section_2", 0.0f, 0.0f, -95.0f, "");
    }
    AddTimer(asTimer, 0.03, "TimerDoor");
}
What it does, is that the timer isn't 0.01, so it keeps doing the second AddPropForce.
And thus, making it close/open all the time.
I think that this is the problem, when thinking logically.

EDIT: And so after reading your post again, I see this isn't what your looking for...
Maybe try making timer last longer, make the it push the door open until the time runs out, and have nothing on the other side of the "else".
That would make it stop looping...
For some reason, this worked:

Code:
void OpenDoor(string &in asParent, string &in asChild, int alState)
{
    SetSwingDoorClosed("prison_section_2", false, false);
    SetSwingDoorDisableAutoClose("prison_section_2", true);
    AddTimer("swing_door", 0.01f, "TimerDoor");
    SetEntityPlayerLookAtCallback("prison_section_2", "RemoveTimerCreak", true);
}

void TimerDoor(string &in asTimer)
{
    if(asTimer == "swing_door")
    {
        AddPropForce("prison_section_2", 0.0f, 0.0f, 25.0f, "");
    }
    else
    {
        AddPropForce("prison_section_2", 0.0f, 0.0f, -95.0f, "");
    }
    AddTimer(asTimer, 0.03, "TimerDoor");
}

void RemoveTimerCreak(string &in asEntity, int alState)
{
    RemoveTimer("swing_door");
    PlaySoundAtEntity("pant", "react_scare.snt", "Player", 0.0f, false);
}

Also, it's way more precise because the 'react_scare'-sound will now always be played at the right time Cool